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