|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Resource\Registry; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
6
|
|
|
use LAG\AdminBundle\Resource\AdminResource; |
|
7
|
|
|
use LAG\AdminBundle\Resource\Loader\ResourceLoader; |
|
8
|
|
|
|
|
9
|
|
|
class ResourceRegistry implements ResourceRegistryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var AdminResource[] |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $items = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $resourcesPath; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(string $resourcesPath) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->resourcesPath = $resourcesPath; |
|
24
|
|
|
$this->load(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function add(AdminResource $resource): void |
|
28
|
|
|
{ |
|
29
|
|
|
$this->items[$resource->getName()] = $resource; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function has(string $resourceName): bool |
|
33
|
|
|
{ |
|
34
|
|
|
return array_key_exists($resourceName, $this->items); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function get($resourceName): AdminResource |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$this->has($resourceName)) { |
|
40
|
|
|
throw new Exception('Resource with name "'.$resourceName.'" not found'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->items[$resourceName]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function all(): array |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->items; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function keys(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return array_keys($this->items); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function remove(string $resourceName): void |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->has($resourceName)) { |
|
59
|
|
|
throw new Exception('Resource with name "'.$resourceName.'" not found'); |
|
60
|
|
|
} |
|
61
|
|
|
unset($this->items[$resourceName]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function load(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$loader = new ResourceLoader(); |
|
67
|
|
|
$data = $loader->load($this->resourcesPath); |
|
68
|
|
|
|
|
69
|
|
|
foreach ($data as $name => $admin) { |
|
70
|
|
|
$this->add(new AdminResource($name, $admin)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|