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