Completed
Push — master ( 98d04a...5d9a92 )
by Raffael
03:41
created
src/Container.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
      *
49 49
      * @param array $config
50 50
      */
51
-    public function __construct(Iterable $config=[])
51
+    public function __construct(Iterable $config = [])
52 52
     {
53 53
         $this->flattenConfig($config);
54 54
         $container = $this;
@@ -65,14 +65,14 @@  discard block
 block discarded – undo
65 65
      * @param  string $parent
66 66
      * @return array
67 67
      */
68
-    protected function flattenConfig(Iterable $config, ?string $parent=null): array
68
+    protected function flattenConfig(Iterable $config, ?string $parent = null) : array
69 69
     {
70 70
         $flat = [];
71
-        foreach($config as $name => $service) {
72
-            if(isset($service['name']) && $parent === null) {
71
+        foreach ($config as $name => $service) {
72
+            if (isset($service['name']) && $parent === null) {
73 73
                 $id = $service['name'];
74 74
            } else {
75
-                if($parent === null) {
75
+                if ($parent === null) {
76 76
                     $id = $name;
77 77
                 } else {
78 78
                     $id = $parent.'.'.$name;
@@ -85,8 +85,8 @@  discard block
 block discarded – undo
85 85
 
86 86
             $new_parent = $parent.'.'.$name;
87 87
 
88
-            foreach($service as $option => $value) {
89
-                switch($option) {
88
+            foreach ($service as $option => $value) {
89
+                switch ($option) {
90 90
 
91 91
                     case 'name' :
92 92
                     case 'use' :
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
                         //$parent = $parent.'.'.$name;
103 103
                         $services = $this->flattenConfig($service['service'], $new_parent);
104 104
                         $flat[$id]['service'] = [];
105
-                        foreach($services as $key => $sub) {
105
+                        foreach ($services as $key => $sub) {
106 106
                             $flat[$id]['service'][$sub['name']] = $key;
107 107
                         }
108 108
                     break;
@@ -141,10 +141,10 @@  discard block
 block discarded – undo
141 141
      */
142 142
     public function get($name)
143 143
     {
144
-        if($this->has($name)) {
144
+        if ($this->has($name)) {
145 145
             return $this->service[$name];
146 146
         } else {
147
-            if(isset($this->registry[$name])) {
147
+            if (isset($this->registry[$name])) {
148 148
                 $this->service[$name] = $this->registry[$name]->call($this);
149 149
                 unset($this->registry[$name]);
150 150
                 return $this->service[$name];
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
      */
164 164
     public function getNew(string $name)
165 165
     {
166
-        if(isset($this->registry[$name])) {
166
+        if (isset($this->registry[$name])) {
167 167
             return $this->registry[$name]->call($this);
168 168
         } else {
169 169
             return $this->autoWire($name);
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
      */
181 181
     public function add(string $name, Closure $service): Container
182 182
     {
183
-        if($this->has($name)) {
183
+        if ($this->has($name)) {
184 184
             throw new Exception('service '.$name.' is already registered');
185 185
         }
186 186
 
@@ -208,9 +208,9 @@  discard block
 block discarded – undo
208 208
      */
209 209
     protected function autoWire(string $name)
210 210
     {
211
-        if(isset($this->config[$name]['use'])) {
211
+        if (isset($this->config[$name]['use'])) {
212 212
             $class = $this->config[$name]['use'];
213
-        } elseif(isset($this->config[$name]['name'])) {
213
+        } elseif (isset($this->config[$name]['name'])) {
214 214
             $class = $this->config[$name]['name'];
215 215
         } else {
216 216
             $class = $name;
@@ -218,27 +218,27 @@  discard block
 block discarded – undo
218 218
 
219 219
         try {
220 220
             $reflection = new ReflectionClass($class);
221
-        } catch(\Exception $e) {
221
+        } catch (\Exception $e) {
222 222
             throw new Exception($class.' can not be resolved to an existing class');
223 223
         }
224 224
 
225 225
         $constructor = $reflection->getConstructor();
226 226
 
227
-        if($constructor === null) {
227
+        if ($constructor === null) {
228 228
             return new $class();
229 229
         } else {
230 230
             $params = $constructor->getParameters();
231 231
             $args = [];
232 232
 
233
-            foreach($params as $param) {
233
+            foreach ($params as $param) {
234 234
                 $type = $param->getClass();
235 235
                 $param_name = $param->getName();
236 236
 
237
-                if($type === null) {
237
+                if ($type === null) {
238 238
                     try {
239 239
                         $args[$param_name] = $this->getParam($name, $param_name);
240
-                    } catch(Exception $e) {
241
-                        if($param->isDefaultValueAvailable()) {
240
+                    } catch (Exception $e) {
241
+                        if ($param->isDefaultValueAvailable()) {
242 242
                             $args[$param_name] = $param->getDefaultValue();
243 243
                         } else {
244 244
                             throw $e;
@@ -264,9 +264,9 @@  discard block
 block discarded – undo
264 264
      */
265 265
     protected function findParentService(string $name, string $class)
266 266
     {
267
-        if(isset($this->config[$name]['service'][$class])) {
267
+        if (isset($this->config[$name]['service'][$class])) {
268 268
             return $this->get($this->config[$name]['service'][$class]);
269
-        } elseif(isset($this->config[$name]['parent'])) {
269
+        } elseif (isset($this->config[$name]['parent'])) {
270 270
             return $this->findParentService($this->config[$name]['parent'], $class);
271 271
         } else {
272 272
             return $this->get($class);
@@ -285,14 +285,14 @@  discard block
 block discarded – undo
285 285
     protected function createInstance(string $name, ReflectionClass $class, array $args)
286 286
     {
287 287
         $instance = $class->newInstanceArgs($args);
288
-        if($instance instanceof AdapterAwareInterface) {
289
-            if(isset($this->config[$name]['adapter'])) {
290
-                foreach($this->config[$name]['adapter'] as $adapter => $config) {
291
-                    if(!isset($config['name'])) {
288
+        if ($instance instanceof AdapterAwareInterface) {
289
+            if (isset($this->config[$name]['adapter'])) {
290
+                foreach ($this->config[$name]['adapter'] as $adapter => $config) {
291
+                    if (!isset($config['name'])) {
292 292
                         throw new Exception('adapter requires name configuration');
293 293
                     }
294 294
 
295
-                    if(isset($config['enabled']) && $config['enabled'] === '0') {
295
+                    if (isset($config['enabled']) && $config['enabled'] === '0') {
296 296
                         continue;
297 297
                     }
298 298
 
@@ -315,11 +315,11 @@  discard block
 block discarded – undo
315 315
      */
316 316
     public function getParam(string $name, string $param)
317 317
     {
318
-        if(!isset($this->config[$name]) && !isset($this->config[$name]['options'])) {
318
+        if (!isset($this->config[$name]) && !isset($this->config[$name]['options'])) {
319 319
             throw new Exception('no configuration available for service '.$name);
320 320
         }
321 321
 
322
-        if(!isset($this->config[$name]['options'][$param])) {
322
+        if (!isset($this->config[$name]['options'][$param])) {
323 323
             throw new Exception('no configuration available for service parameter '.$param);
324 324
         }
325 325
 
Please login to merge, or discard this patch.