1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace WebComplete\core\cube; |
4
|
|
|
|
5
|
|
|
use WebComplete\core\utils\cache\CacheService; |
6
|
|
|
use WebComplete\core\utils\container\ContainerInterface; |
7
|
|
|
use WebComplete\core\utils\helpers\ClassHelper; |
8
|
|
|
|
9
|
|
|
class CubeManager |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
const FILENAME = 'Cube.php'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var ClassHelper |
16
|
|
|
*/ |
17
|
|
|
protected $classHelper; |
18
|
|
|
/** |
19
|
|
|
* @var CacheService |
20
|
|
|
*/ |
21
|
|
|
protected $cacheService; |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $registered = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ClassHelper $classHelper |
29
|
|
|
* @param CacheService $cacheService |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ClassHelper $classHelper, CacheService $cacheService) |
32
|
|
|
{ |
33
|
|
|
$this->classHelper = $classHelper; |
34
|
|
|
$this->cacheService = $cacheService; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $cubeClassName |
39
|
|
|
* |
40
|
|
|
* @return AbstractCube |
41
|
|
|
* @throws CubeException |
42
|
|
|
*/ |
43
|
|
|
public function getCube($cubeClassName): AbstractCube |
44
|
|
|
{ |
45
|
|
|
if (!isset($this->registered[$cubeClassName])) { |
46
|
|
|
throw new CubeException($cubeClassName . ' is not registered'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->registered[$cubeClassName]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return AbstractCube[] |
54
|
|
|
*/ |
55
|
|
|
public function getCubes(): array |
56
|
|
|
{ |
57
|
|
|
return $this->registered; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $cubeClassName |
62
|
|
|
* @param array $definitions |
63
|
|
|
* |
64
|
|
|
* @throws CubeException |
65
|
|
|
*/ |
66
|
|
|
public function register($cubeClassName, array &$definitions) |
67
|
|
|
{ |
68
|
|
|
if (!isset($this->registered[$cubeClassName])) { |
69
|
|
|
$cube = new $cubeClassName; |
70
|
|
|
if (!$cube instanceof AbstractCube) { |
71
|
|
|
throw new CubeException($cubeClassName . ' is not an instance of ' . AbstractCube::class); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($cube->enabled) { |
75
|
|
|
$cube->registerDependencies($definitions); |
76
|
|
|
$this->registered[$cubeClassName] = $cube; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $directory |
83
|
|
|
* @param array $definitions |
84
|
|
|
* |
85
|
|
|
* @throws \Exception |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
88
|
|
|
* @throws \WebComplete\core\cube\CubeException |
89
|
|
|
*/ |
90
|
|
|
public function registerAll(string $directory, array &$definitions) |
91
|
|
|
{ |
92
|
|
|
$classMap = $this->findAll($directory); |
93
|
|
|
foreach ($classMap as $class) { |
94
|
|
|
$this->register($class, $definitions); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $directory |
100
|
|
|
* |
101
|
|
|
* @return array [file => class] |
102
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
103
|
|
|
* @throws \Exception |
104
|
|
|
* @throws \InvalidArgumentException |
105
|
|
|
*/ |
106
|
|
|
public function findAll($directory): array |
107
|
|
|
{ |
108
|
|
|
$cache = $this->cacheService->systemSimple(); |
109
|
|
|
$key = \str_replace(['::', '\\'], '_', __METHOD__) . '_' . \crc32($directory); |
110
|
|
|
$result = $cache->get($key); |
111
|
|
|
if (!$result) { |
112
|
|
|
$result = $this->classHelper->getClassMap($directory, self::FILENAME); |
113
|
|
|
$cache->set($key, $result); |
114
|
|
|
} |
115
|
|
|
return $result; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param ContainerInterface $container |
120
|
|
|
*/ |
121
|
|
|
public function bootstrap(ContainerInterface $container) |
122
|
|
|
{ |
123
|
|
|
foreach ($this->getCubes() as $cube) { |
124
|
|
|
$cube->bootstrap($container); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|