1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\ExtensionDriverLocator; |
4
|
|
|
|
5
|
|
|
use Bex\Behat\ExtensionDriverLocator\DriverClassNameResolver; |
6
|
|
|
use Bex\Behat\ExtensionDriverLocator\DriverClassValidator; |
7
|
|
|
use Bex\Behat\ExtensionDriverLocator\DriverInterface; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
9
|
|
|
use Symfony\Component\Config\Definition\NodeInterface; |
10
|
|
|
use Symfony\Component\Config\Definition\Processor; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
|
13
|
|
|
class DriverLocator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var DriverClassNameResolver |
17
|
|
|
*/ |
18
|
|
|
private $classNameResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var DriverInterface[] |
22
|
|
|
*/ |
23
|
|
|
private $drivers = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param DriverClassNameResolver $classNameResolver |
27
|
|
|
*/ |
28
|
|
|
public function __construct(DriverClassNameResolver $classNameResolver) |
29
|
|
|
{ |
30
|
|
|
$this->classNameResolver = $classNameResolver; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $namespace |
35
|
|
|
* @param string $parent |
36
|
|
|
* |
37
|
|
|
* @return Locator |
38
|
|
|
*/ |
39
|
|
|
public static function getInstance($namespace, $parent = '') |
40
|
|
|
{ |
41
|
|
|
return new self(new DriverClassNameResolver($namespace, new DriverClassValidator($parent))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ContainerBuilder $container |
46
|
|
|
* @param array $activeDrivers |
47
|
|
|
* @param array $driverConfigs |
48
|
|
|
* |
49
|
|
|
* @return DriverInterface[] |
50
|
|
|
*/ |
51
|
|
|
public function findDrivers(ContainerBuilder $container, array $activeDrivers, array $driverConfigs) |
52
|
|
|
{ |
53
|
|
|
$driverConfigs = $this->removeUnusedDrivers($activeDrivers, $driverConfigs); |
54
|
|
|
$this->createDrivers($activeDrivers); |
55
|
|
|
$configTree = $this->configureDrivers(); |
56
|
|
|
$driverConfigs = $this->processDriverConfiguration($configTree, $driverConfigs); |
57
|
|
|
$this->loadDrivers($container, $driverConfigs); |
58
|
|
|
|
59
|
|
|
return $this->drivers; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return DriverInterface[] |
64
|
|
|
*/ |
65
|
|
|
public function getDrivers() |
66
|
|
|
{ |
67
|
|
|
return $this->drivers; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $activeDrivers |
72
|
|
|
* @param array $driverConfigs |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
private function removeUnusedDrivers($activeDrivers, $driverConfigs) |
77
|
|
|
{ |
78
|
|
|
foreach ($driverConfigs as $driverKey => $driverConfig) { |
79
|
|
|
if (!in_array($driverKey, $activeDrivers)) { |
80
|
|
|
unset($driverConfigs[$driverKey]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $driverConfigs; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $driverKeys |
89
|
|
|
* |
90
|
|
|
* @return DriverInterface[] |
91
|
|
|
*/ |
92
|
|
|
private function createDrivers($driverKeys) |
93
|
|
|
{ |
94
|
|
|
$this->drivers = []; |
95
|
|
|
|
96
|
|
|
foreach ($driverKeys as $driverKey) { |
97
|
|
|
$driverClass = $this->classNameResolver->getClassNameByDriverKey($driverKey); |
98
|
|
|
$this->drivers[$driverKey] = new $driverClass(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->drivers; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return NodeInterface |
106
|
|
|
*/ |
107
|
|
|
private function configureDrivers() |
108
|
|
|
{ |
109
|
|
|
$tree = new TreeBuilder('drivers'); |
110
|
|
|
|
111
|
|
|
foreach ($this->drivers as $driverKey => $driver) { |
112
|
|
|
$driver->configure($tree->getRootNode()->children()->arrayNode($driverKey)); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $tree->buildTree(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param NodeInterface $configTree |
120
|
|
|
* @param array $configs |
121
|
|
|
* |
122
|
|
|
* @return array The processed configuration |
123
|
|
|
*/ |
124
|
|
|
private function processDriverConfiguration(NodeInterface $configTree, array $configs) |
125
|
|
|
{ |
126
|
|
|
$configProcessor = new Processor(); |
127
|
|
|
|
128
|
|
|
foreach ($this->drivers as $driverKey => $driver) { |
129
|
|
|
$configs[$driverKey] = isset($configs[$driverKey]) ? $configs[$driverKey] : []; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $configProcessor->process($configTree, ['drivers' => $configs]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param ContainerBuilder $container |
137
|
|
|
* @param array $driverConfigs |
138
|
|
|
* |
139
|
|
|
* @return DriverInterface[] |
140
|
|
|
*/ |
141
|
|
|
private function loadDrivers(ContainerBuilder $container, array $driverConfigs) |
142
|
|
|
{ |
143
|
|
|
foreach ($this->drivers as $driverKey => $driver) { |
144
|
|
|
$driver->load($container, $driverConfigs[$driverKey]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->drivers; |
148
|
|
|
} |
149
|
|
|
} |
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 sub-classes 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 parent class: