|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Models\SmartProperties\Definitions; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Models\SmartProperties\Exceptions\InvalidArgumentException; |
|
6
|
|
|
use Nip\Utility\Traits\SingletonTrait; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class DefinitionRegistry |
|
10
|
|
|
* @package ByTIC\Models\SmartProperties\Definitions |
|
11
|
|
|
*/ |
|
12
|
|
|
class DefinitionRegistry |
|
13
|
|
|
{ |
|
14
|
|
|
use SingletonTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Definition[] |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $builders = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var [] |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
protected $definitionsBuilt = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Definition[] |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $definitions = []; |
|
30
|
|
|
|
|
31
|
|
|
public function has($manager, string $definitionName): bool |
|
32
|
|
|
{ |
|
33
|
|
|
$managerName = $this->managerName($manager); |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($this->definitions[$managerName][$definitionName])) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $manager |
|
44
|
|
|
* @param string $definitionName |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get($manager, string $definitionName) |
|
48
|
|
|
{ |
|
49
|
|
|
$managerName = $this->managerName($manager); |
|
50
|
|
|
|
|
51
|
|
|
if (!isset($this->definitions[$managerName][$definitionName])) { |
|
52
|
|
|
throw new InvalidArgumentException(sprintf( |
|
53
|
|
|
'Unable to find a definition "%s" for class "%s".', |
|
54
|
|
|
$definitionName, $managerName |
|
55
|
|
|
)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->definitions[$managerName][$definitionName]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $manager |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getAll($manager) |
|
66
|
|
|
{ |
|
67
|
|
|
$managerName = $this->managerName($manager); |
|
68
|
|
|
|
|
69
|
|
|
if (!isset($this->definitions[$managerName])) { |
|
70
|
|
|
return []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->definitions[$managerName]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param object $manager |
|
78
|
|
|
* @param $definition |
|
79
|
|
|
*/ |
|
80
|
|
|
public function set(object $manager, $definition) |
|
81
|
|
|
{ |
|
82
|
|
|
$managerName = $this->managerName($manager); |
|
83
|
|
|
$definitionName = $this->definitionName($definition); |
|
84
|
|
|
|
|
85
|
|
|
$this->definitions[$managerName][$definitionName] = $definition; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param object $manager |
|
90
|
|
|
* @param \Closure $callback |
|
91
|
|
|
*/ |
|
92
|
|
|
public function checkDefinitionToBuild(object $manager, \Closure $callback) |
|
93
|
|
|
{ |
|
94
|
|
|
$managerName = $this->managerName($manager); |
|
95
|
|
|
if (isset($this->definitionsBuilt[$managerName])) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
$callback(); |
|
99
|
|
|
$this->definitionsBuilt[$managerName] = true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param object|string $manager |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function managerName($manager): string |
|
107
|
|
|
{ |
|
108
|
|
|
if (is_object($manager)) { |
|
109
|
|
|
return get_class($manager); |
|
110
|
|
|
} |
|
111
|
|
|
return (string)$manager; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param object|string $definition |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function definitionName($definition): string |
|
119
|
|
|
{ |
|
120
|
|
|
if ($definition instanceof Definition) { |
|
121
|
|
|
return $definition->getName(); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
return (string)$definition; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|