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