1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Discovery\Strategy; |
4
|
|
|
|
5
|
|
|
use Http\Discovery\Exception\PuliUnavailableException; |
6
|
|
|
use Puli\Discovery\Api\Discovery; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author David de Boer <[email protected]> |
10
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Puli implements DiscoveryStrategy |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var GeneratedPuliFactory |
16
|
|
|
*/ |
17
|
|
|
private static $puliFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Discovery |
21
|
|
|
*/ |
22
|
|
|
private static $puliDiscovery; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return GeneratedPuliFactory |
26
|
|
|
* |
27
|
|
|
* @throws PuliUnavailableException |
28
|
|
|
*/ |
29
|
|
|
public static function getPuliFactory() |
30
|
|
|
{ |
31
|
|
|
if (null === self::$puliFactory) { |
32
|
|
|
if (!defined('PULI_FACTORY_CLASS')) { |
33
|
|
|
throw new PuliUnavailableException('Puli Factory is not available'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$puliFactoryClass = PULI_FACTORY_CLASS; |
37
|
|
|
|
38
|
|
|
if (!class_exists($puliFactoryClass)) { |
39
|
|
|
throw new PuliUnavailableException('Puli Factory class does not exist'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
self::$puliFactory = new $puliFactoryClass(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return self::$puliFactory; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets the Puli factory. |
50
|
|
|
* |
51
|
|
|
* @param object $puliFactory |
52
|
|
|
*/ |
53
|
|
|
public static function setPuliFactory($puliFactory) |
54
|
|
|
{ |
55
|
|
|
if (!is_callable([$puliFactory, 'createRepository']) || !is_callable([$puliFactory, 'createDiscovery'])) { |
56
|
|
|
throw new \InvalidArgumentException('The Puli Factory must expose a repository and a discovery'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
self::$puliFactory = $puliFactory; |
60
|
|
|
self::$puliDiscovery = null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Resets the factory. |
65
|
|
|
*/ |
66
|
|
|
public static function resetPuliFactory() |
67
|
|
|
{ |
68
|
|
|
self::$puliFactory = null; |
69
|
|
|
self::$puliDiscovery = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the Puli discovery layer. |
74
|
|
|
* |
75
|
|
|
* @return Discovery |
76
|
|
|
* |
77
|
|
|
* @throws PuliUnavailableException |
78
|
|
|
*/ |
79
|
|
|
public static function getPuliDiscovery() |
80
|
|
|
{ |
81
|
|
|
if (!isset(self::$puliDiscovery)) { |
82
|
|
|
$factory = self::getPuliFactory(); |
83
|
|
|
$repository = $factory->createRepository(); |
84
|
|
|
|
85
|
|
|
self::$puliDiscovery = $factory->createDiscovery($repository); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return self::$puliDiscovery; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public static function find($type) |
95
|
|
|
{ |
96
|
|
|
$returnData = []; |
97
|
|
|
$bindings = self::getPuliDiscovery()->findBindings($type); |
98
|
|
|
|
99
|
|
|
foreach ($bindings as $binding) { |
100
|
|
|
$condition = true; |
101
|
|
|
if ($binding->hasParameterValue('depends')) { |
102
|
|
|
$condition = $binding->getParameterValue('depends'); |
103
|
|
|
} |
104
|
|
|
$returnData[] = ['class' => $binding->getClassName(), 'condition' => $condition]; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $returnData; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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: