1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpAbModule\Service; |
4
|
|
|
|
5
|
|
|
use PhpAb\Engine\Engine; |
6
|
|
|
use PhpAb\Engine\EngineInterface; |
7
|
|
|
use PhpAb\Event\DispatcherInterface; |
8
|
|
|
use PhpAb\Participation\ParticipationManagerInterface; |
9
|
|
|
use PhpAb\Test\Test; |
10
|
|
|
use PhpAb\Variant\CallbackVariant; |
11
|
|
|
use PhpAb\Variant\SimpleVariant; |
12
|
|
|
use PhpAbModule\Variant\EventManagerVariant; |
13
|
|
|
use RuntimeException; |
14
|
|
|
use Zend\EventManager\EventManager; |
15
|
|
|
use Zend\ServiceManager\FactoryInterface; |
16
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
17
|
|
|
|
18
|
|
|
class EngineFactory implements FactoryInterface |
19
|
|
|
{ |
20
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
21
|
|
|
{ |
22
|
|
|
/** @var array $config */ |
23
|
|
|
$config = $serviceLocator->get('Config'); |
24
|
|
|
|
25
|
|
|
/** @var ParticipationManagerInterface $participationManager */ |
26
|
|
|
$participationManager = $serviceLocator->get('phpab.participation_manager'); |
27
|
|
|
|
28
|
|
|
/** @var DispatcherInterface $dispatcher */ |
29
|
|
|
$dispatcher = $serviceLocator->get('phpab.dispatcher'); |
30
|
|
|
|
31
|
|
|
$filter = $this->loadService($serviceLocator, $config['phpab']['default_filter']); |
32
|
|
|
$chooser = $this->loadService($serviceLocator, $config['phpab']['default_variant_chooser']); |
33
|
|
|
|
34
|
|
|
$engine = new Engine($participationManager, $dispatcher, $filter, $chooser); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$this->loadTests($engine, $serviceLocator, $config['phpab']); |
37
|
|
|
|
38
|
|
|
return $engine; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function loadService(ServiceLocatorInterface $serviceLocator, $serviceName) |
42
|
|
|
{ |
43
|
|
|
if (!$serviceLocator->has($serviceName)) { |
44
|
|
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $serviceLocator->get($serviceName); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function loadTests(EngineInterface $engine, ServiceLocatorInterface $serviceLocator, $config) |
51
|
|
|
{ |
52
|
|
|
foreach ($config['tests'] as $identifier => $testConfig) { |
53
|
|
|
$this->loadTest($serviceLocator, $engine, $identifier, $testConfig); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function loadTest(ServiceLocatorInterface $serviceLocator, EngineInterface $engine, $identifier, array $config) |
58
|
|
|
{ |
59
|
|
|
$filter = null; |
60
|
|
|
if (array_key_exists('filter', $config)) { |
61
|
|
|
$filter = $serviceLocator->get($config['filter']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$variantChooser = null; |
65
|
|
|
if (array_key_exists('variant_chooser', $config)) { |
66
|
|
|
$variantChooser = $serviceLocator->get($config['variant_chooser']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$variants = $this->loadTestVariants($serviceLocator, $config); |
70
|
|
|
$options = array_key_exists('options', $config) ? $config['options'] : []; |
71
|
|
|
|
72
|
|
|
$test = new Test($identifier, $variants); |
73
|
|
|
|
74
|
|
|
$engine->addTest($test, $options, $filter, $variantChooser); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function loadTestVariants(ServiceLocatorInterface $serviceLocator, $config) |
78
|
|
|
{ |
79
|
|
|
$variants = []; |
80
|
|
|
|
81
|
|
|
foreach ($config['variants'] as $identifier => $variant) { |
82
|
|
|
|
83
|
|
|
// We also support shortcuts so that the user can specify a service name straight away. |
84
|
|
|
if (is_string($variant)) { |
85
|
|
|
$variant = [ |
86
|
|
|
'type' => $variant, |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!array_key_exists('type', $variant)) { |
91
|
|
|
throw new RuntimeException('The type of the variant is missing.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$options = empty($variant['options']) ? [] : $variant['options']; |
95
|
|
|
|
96
|
|
|
$variants[] = $this->loadTestVariant($serviceLocator, $variant['type'], $identifier, $options); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $variants; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function loadTestVariant(ServiceLocatorInterface $serviceLocator, $type, $identifier, array $options) |
103
|
|
|
{ |
104
|
|
|
switch ($type) { |
105
|
|
|
case 'callback': |
106
|
|
|
$variant = $this->loadTestVariantCallback($serviceLocator, $identifier, $options); |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case 'event_manager': |
110
|
|
|
$variant = $this->loadTestVariantEventManager($serviceLocator, $identifier, $options); |
111
|
|
|
break; |
112
|
|
|
|
113
|
|
|
case 'simple': |
114
|
|
|
$variant = $this->loadTestVariantSimple($serviceLocator, $identifier, $options); |
115
|
|
|
break; |
116
|
|
|
|
117
|
|
|
case 'service_manager': |
118
|
|
|
default: |
119
|
|
|
$variant = $this->loadTestVariantFromServiceManager($serviceLocator, $type); |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $variant; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function loadTestVariantCallback(ServiceLocatorInterface $serviceLocator, $identifier, array $options) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
if (!array_key_exists('callback', $options)) { |
129
|
|
|
throw new RuntimeException('Missing "callback" for callback variant.'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (!is_callable($options['callback'])) { |
133
|
|
|
throw new RuntimeException('The "callback" for callback variant cannot be called.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return new CallbackVariant($identifier, $options['callback']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function loadTestVariantEventManager(ServiceLocatorInterface $serviceLocator, $identifier, array $options) |
140
|
|
|
{ |
141
|
|
|
if (empty($options['event_manager'])) { |
142
|
|
|
$eventManager = $serviceLocator->get('Application')->getEventManager(); |
143
|
|
|
} else { |
144
|
|
|
$eventManager = $serviceLocator->get($options['event_manager']); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$callback = $options['callback']; |
148
|
|
|
|
149
|
|
|
if (!is_callable($callback) && $serviceLocator->has($callback)) { |
150
|
|
|
$callback = $serviceLocator->get($callback); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return new EventManagerVariant( |
154
|
|
|
$eventManager, |
155
|
|
|
$identifier, |
156
|
|
|
$options['event'], |
157
|
|
|
$callback, |
158
|
|
|
$options['priority'] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function loadTestVariantSimple(ServiceLocatorInterface $serviceLocator, $identifier, array $options) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
return new SimpleVariant($identifier); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private function loadTestVariantFromServiceManager(ServiceLocatorInterface $serviceLocator, $type) |
168
|
|
|
{ |
169
|
|
|
if (!$serviceLocator->has($type)) { |
170
|
|
|
throw new RuntimeException(sprintf('The variant "%s" is not a valid service manager name.', $type)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $serviceLocator->get($type); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.