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