1 | <?php |
||
22 | final class EnvironmentLoader |
||
23 | { |
||
24 | /** |
||
25 | * DI container of the extension. |
||
26 | * |
||
27 | * @var ContainerBuilder |
||
28 | */ |
||
29 | private $container; |
||
30 | /** |
||
31 | * Path of the extension. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $path = ''; |
||
36 | /** |
||
37 | * Namespace of the extension. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $namespace = ''; |
||
42 | /** |
||
43 | * Configuration key of the extension in a lowercase. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $configKey = ''; |
||
48 | /** |
||
49 | * Formula: <EXTENSION_NAMESPACE>\<EXTENSION_SUB_NAMESPACE>\<EXTENSION_CONFIG_KEY>. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $classPath = ''; |
||
54 | /** |
||
55 | * Definitions for extending container. |
||
56 | * |
||
57 | * @var Definition[] |
||
58 | */ |
||
59 | private $definitions = []; |
||
60 | |||
61 | /** |
||
62 | * EnvironmentLoader constructor. |
||
63 | * |
||
64 | * @param Extension $extension |
||
65 | * @param ContainerBuilder $container |
||
66 | * @param array $config |
||
67 | */ |
||
68 | 16 | public function __construct(Extension $extension, ContainerBuilder $container, array $config = []) |
|
104 | |||
105 | /** |
||
106 | * Implement extension's own environment reader. |
||
107 | * |
||
108 | * @param array $arguments |
||
109 | */ |
||
110 | 4 | public function addEnvironmentReader(array $arguments = []) |
|
111 | { |
||
112 | // Full namespace: <EXTENSION_NAMESPACE>\Environment\<EXTENSION_CONFIG_KEY>EnvironmentReader. |
||
113 | 4 | $this->addDefinition( |
|
114 | 4 | 'Environment', |
|
115 | 4 | 'Reader', |
|
116 | 4 | 'Behat\Testwork\Environment\Reader\EnvironmentReader', |
|
117 | 4 | EnvironmentExtension::READER_TAG, |
|
118 | 4 | array_merge($arguments, [$this->namespace, $this->path]) |
|
119 | 4 | ); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Extend DI container by dependencies. |
||
124 | */ |
||
125 | 4 | public function load() |
|
126 | { |
||
127 | 4 | foreach ($this->definitions as $tag => $definition) { |
|
128 | 4 | $this->extendContainer($tag, $definition); |
|
129 | 4 | } |
|
130 | 4 | } |
|
131 | |||
132 | /** |
||
133 | * Add dependency definition for DI container. |
||
134 | * |
||
135 | * @param string $subNamespace |
||
136 | * @param string $objectType |
||
137 | * @param string $interface |
||
138 | * @param string$tag |
||
139 | * @param array $arguments |
||
140 | */ |
||
141 | 16 | private function addDefinition($subNamespace, $objectType, $interface, $tag, array $arguments = []) |
|
142 | { |
||
143 | // For example we have registered extension at namespace: "Behat\TqExtension". Class, which |
||
144 | // implements extension interface, located at "Behat\TqExtension\ServiceContainer\TqExtension" |
||
145 | // and its method, "getConfigKey()", returns the "tq" string. In this case the full namespace |
||
146 | // of the reader object will be: "Behat\TqExtension\Environment\TqEnvironmentReader" and its |
||
147 | // constructor will have a set of arguments that were passed to this method. |
||
148 | 16 | $class = sprintf($this->classPath, $subNamespace) . $subNamespace . $objectType; |
|
149 | |||
150 | 16 | if (!class_exists($class)) { |
|
151 | 4 | throw new \RuntimeException(sprintf('Class "%s" does not exists!', $class)); |
|
152 | } |
||
153 | |||
154 | 16 | if (!in_array($interface, class_implements($class))) { |
|
155 | throw new \RuntimeException(sprintf('Class "%s" must implement the "%s" interface!', $class, $interface)); |
||
156 | } |
||
157 | |||
158 | 16 | $this->definitions[$tag] = new Definition($class, $arguments); |
|
159 | 16 | } |
|
160 | |||
161 | /** |
||
162 | * Add dependency to DI container. |
||
163 | * |
||
164 | * @param string $tag |
||
165 | * @param Definition $definition |
||
166 | */ |
||
167 | 16 | private function extendContainer($tag, Definition $definition) |
|
173 | } |
||
174 |