Repository navigation
Expand file tree
/
Copy pathModuleReflector.php
More file actions
84 lines (72 loc) · 2.4 KB
/
Copy pathModuleReflector.php
File metadata and controls
84 lines (72 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace NyonCode\WireBoost\Support;
use NyonCode\WireCore\Core\Modules\Module;
use NyonCode\WireCore\Core\Plugin\Contracts\HasDependencies;
use NyonCode\WireCore\Core\Plugin\PluginManager;
/**
* What an application's domain modules declare.
*
* The domain axis is the one thing about an application that no other tool can
* show: `describe-resource` lists resources and knows nothing about which
* business area they belong to, and a module is exactly that grouping. Reading
* it is cheap — a module is a declaration, so this asks it what it names and
* never builds anything.
*
* Modules come from {@see PluginManager} because a module *is* a plugin; there
* is no module registry to read, deliberately (V2.6 step 5).
*/
class ModuleReflector
{
public function __construct(private PluginManager $plugins) {}
/**
* Every registered module, in registration order.
*
* @return array<int, array<string, mixed>>
*/
public function all(): array
{
$described = [];
foreach ($this->plugins->all() as $plugin) {
if ($plugin instanceof Module) {
$described[] = $this->describeModule($plugin);
}
}
return $described;
}
/**
* One module by id, or null when nothing is registered under it.
*
* @return array<string, mixed>|null
*/
public function describe(string $id): ?array
{
$plugin = $this->plugins->get($id);
return $plugin instanceof Module ? $this->describeModule($plugin) : null;
}
/** @return array<int, string> */
public function ids(): array
{
return array_column($this->all(), 'id');
}
/**
* @return array<string, mixed>
*/
private function describeModule(Module $module): array
{
$group = $module->navigation();
return [
'id' => $module->getId(),
'class' => $module::class,
'dependencies' => $module instanceof HasDependencies ? $module->dependencies() : [],
'resources' => $module->resources(),
'dashboards' => $module->dashboards(),
'navigationGroup' => $group === null ? null : [
'key' => $group->getKey(),
'label' => $group->getLabel(),
'icon' => $group->getIcon(),
'sort' => $group->getSort(),
],
];
}
}