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