1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Discovery; |
4
|
|
|
|
5
|
|
|
use Puli\Discovery\Api\Discovery; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Registry that based find results on class existence. |
9
|
|
|
* |
10
|
|
|
* @author David de Boer <[email protected]> |
11
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
abstract class ClassDiscovery |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var GeneratedPuliFactory |
17
|
|
|
*/ |
18
|
7 |
|
private static $puliFactory; |
19
|
|
|
|
20
|
7 |
|
/** |
21
|
|
|
* @var Discovery |
22
|
|
|
*/ |
23
|
7 |
|
private static $puliDiscovery; |
24
|
7 |
|
|
25
|
7 |
|
/** |
26
|
|
|
* @return GeneratedPuliFactory |
27
|
7 |
|
*/ |
28
|
7 |
|
public static function getPuliFactory() |
29
|
|
|
{ |
30
|
|
|
if (null === self::$puliFactory) { |
31
|
|
|
if (!defined('PULI_FACTORY_CLASS')) { |
32
|
|
|
throw new \RuntimeException('Puli Factory is not available'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$puliFactoryClass = PULI_FACTORY_CLASS; |
36
|
|
|
|
37
|
14 |
|
if (!class_exists($puliFactoryClass)) { |
38
|
|
|
throw new \RuntimeException('Puli Factory class does not exist'); |
39
|
|
|
} |
40
|
14 |
|
|
41
|
1 |
|
self::$puliFactory = new $puliFactoryClass(); |
42
|
|
|
} |
43
|
|
|
|
44
|
14 |
|
return self::$puliFactory; |
45
|
13 |
|
} |
46
|
13 |
|
|
47
|
|
|
/** |
48
|
13 |
|
* Sets the Puli factory. |
49
|
|
|
* |
50
|
6 |
|
* @param object $puliFactory |
51
|
|
|
*/ |
52
|
1 |
|
public static function setPuliFactory($puliFactory) |
53
|
|
|
{ |
54
|
|
|
if (!is_callable([$puliFactory, 'createRepository']) || !is_callable([$puliFactory, 'createDiscovery'])) { |
55
|
|
|
throw new \InvalidArgumentException('The Puli Factory must expose a repository and a discovery'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
self::$puliFactory = $puliFactory; |
59
|
|
|
self::$puliDiscovery = null; |
60
|
|
|
} |
61
|
|
|
|
62
|
13 |
|
/** |
63
|
|
|
* Resets the factory. |
64
|
13 |
|
*/ |
65
|
|
|
public static function resetPuliFactory() |
66
|
10 |
|
{ |
67
|
8 |
|
self::$puliFactory = null; |
68
|
1 |
|
self::$puliDiscovery = null; |
69
|
8 |
|
} |
70
|
5 |
|
|
71
|
5 |
|
/** |
72
|
4 |
|
* Returns the Puli discovery layer. |
73
|
|
|
* |
74
|
|
|
* @return Discovery |
75
|
4 |
|
*/ |
76
|
4 |
|
public static function getPuliDiscovery() |
77
|
4 |
|
{ |
78
|
|
|
if (!isset(self::$puliDiscovery)) { |
79
|
4 |
|
$factory = self::getPuliFactory(); |
80
|
|
|
$repository = $factory->createRepository(); |
81
|
|
|
|
82
|
1 |
|
self::$puliDiscovery = $factory->createDiscovery($repository); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return self::$puliDiscovery; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Finds a class. |
90
|
|
|
* |
91
|
|
|
* @param $type |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
* |
95
|
|
|
* @throws NotFoundException |
96
|
|
|
*/ |
97
|
|
|
public static function findOneByType($type) |
98
|
|
|
{ |
99
|
|
|
$bindings = self::getPuliDiscovery()->findBindings($type); |
100
|
|
|
|
101
|
|
|
foreach ($bindings as $binding) { |
102
|
|
|
if ($binding->hasParameterValue('depends')) { |
103
|
|
|
$dependency = $binding->getParameterValue('depends'); |
104
|
|
|
|
105
|
|
|
if (!self::evaluateCondition($dependency)) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// TODO: check class binding |
111
|
|
|
return $binding->getClassName(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new NotFoundException(sprintf('Binding of type "%s" not found', $type)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Finds a resource. |
119
|
|
|
* |
120
|
|
|
* @return object |
121
|
|
|
*/ |
122
|
|
|
public static function find() |
123
|
|
|
{ |
124
|
|
|
throw new \LogicException('Not implemented'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Evaulates conditions to boolean. |
129
|
|
|
* |
130
|
|
|
* @param mixed $condition |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
protected static function evaluateCondition($condition) |
135
|
|
|
{ |
136
|
|
|
if (is_string($condition)) { |
137
|
|
|
// Should be extended for functions, extensions??? |
138
|
|
|
return class_exists($condition); |
139
|
|
|
} elseif (is_callable($condition)) { |
140
|
|
|
return $condition(); |
141
|
|
|
} elseif (is_bool($condition)) { |
142
|
|
|
return $condition; |
143
|
|
|
} elseif (is_array($condition)) { |
144
|
|
|
$evaluatedCondition = true; |
145
|
|
|
|
146
|
|
|
// Immediately stop execution if the condition is false |
147
|
|
|
for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; ++$i) { |
148
|
|
|
$evaluatedCondition &= static::evaluateCondition($condition[$i]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $evaluatedCondition; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: