@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @param array $config |
| 50 | 50 | */ |
| 51 | - public function __construct(array $config=[]) |
|
| 51 | + public function __construct(array $config = []) |
|
| 52 | 52 | { |
| 53 | 53 | $this->flattenConfig($config); |
| 54 | 54 | $container = $this; |
@@ -65,14 +65,14 @@ discard block |
||
| 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; |
@@ -83,8 +83,8 @@ discard block |
||
| 83 | 83 | 'name' => $name |
| 84 | 84 | ]; |
| 85 | 85 | |
| 86 | - foreach($service as $option => $value) { |
|
| 87 | - switch($option) { |
|
| 86 | + foreach ($service as $option => $value) { |
|
| 87 | + switch ($option) { |
|
| 88 | 88 | |
| 89 | 89 | case 'name' : |
| 90 | 90 | case 'use' : |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | $parent = $parent.'.'.$name; |
| 101 | 101 | $services = $this->flattenConfig($service['service'], $parent); |
| 102 | 102 | $flat[$id]['service'] = []; |
| 103 | - foreach($services as $key => $sub) { |
|
| 103 | + foreach ($services as $key => $sub) { |
|
| 104 | 104 | $flat[$id]['service'][$sub['name']] = $key; |
| 105 | 105 | } |
| 106 | 106 | break; |
@@ -139,10 +139,10 @@ discard block |
||
| 139 | 139 | */ |
| 140 | 140 | public function get($name) |
| 141 | 141 | { |
| 142 | - if($this->has($name)) { |
|
| 142 | + if ($this->has($name)) { |
|
| 143 | 143 | return $this->service[$name]; |
| 144 | 144 | } else { |
| 145 | - if(isset($this->registry[$name])) { |
|
| 145 | + if (isset($this->registry[$name])) { |
|
| 146 | 146 | $this->service[$name] = $this->registry[$name]->call($this); |
| 147 | 147 | unset($this->registry[$name]); |
| 148 | 148 | return $this->service[$name]; |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | public function getNew(string $name) |
| 163 | 163 | { |
| 164 | - if(isset($this->registry[$name])) { |
|
| 164 | + if (isset($this->registry[$name])) { |
|
| 165 | 165 | return $this->registry[$name]->call($this); |
| 166 | 166 | } else { |
| 167 | 167 | return $this->autoWire($name); |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | public function add(string $name, Closure $service): Container |
| 180 | 180 | { |
| 181 | - if($this->has($name)) { |
|
| 181 | + if ($this->has($name)) { |
|
| 182 | 182 | throw new Exception('service '.$name.' is already registered'); |
| 183 | 183 | } |
| 184 | 184 | |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | */ |
| 207 | 207 | protected function autoWire(string $name) |
| 208 | 208 | { |
| 209 | - if(isset($this->config[$name]['use'])) { |
|
| 209 | + if (isset($this->config[$name]['use'])) { |
|
| 210 | 210 | $class = $this->config[$name]['use']; |
| 211 | - } elseif(isset($this->config[$name]['name'])) { |
|
| 211 | + } elseif (isset($this->config[$name]['name'])) { |
|
| 212 | 212 | $class = $this->config[$name]['name']; |
| 213 | 213 | } else { |
| 214 | 214 | $class = $name; |
@@ -216,27 +216,27 @@ discard block |
||
| 216 | 216 | |
| 217 | 217 | try { |
| 218 | 218 | $reflection = new ReflectionClass($class); |
| 219 | - } catch(\Exception $e) { |
|
| 219 | + } catch (\Exception $e) { |
|
| 220 | 220 | throw new Exception($class.' can not be resolved to an existing class'); |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | $constructor = $reflection->getConstructor(); |
| 224 | 224 | |
| 225 | - if($constructor === null) { |
|
| 225 | + if ($constructor === null) { |
|
| 226 | 226 | return new $class(); |
| 227 | 227 | } else { |
| 228 | 228 | $params = $constructor->getParameters(); |
| 229 | 229 | $args = []; |
| 230 | 230 | |
| 231 | - foreach($params as $param) { |
|
| 231 | + foreach ($params as $param) { |
|
| 232 | 232 | $type = $param->getClass(); |
| 233 | 233 | $param_name = $param->getName(); |
| 234 | 234 | |
| 235 | - if($type === null) { |
|
| 235 | + if ($type === null) { |
|
| 236 | 236 | try { |
| 237 | 237 | $args[$param_name] = $this->getParam($name, $param_name); |
| 238 | - } catch(Exception $e) { |
|
| 239 | - if($param->isDefaultValueAvailable()) { |
|
| 238 | + } catch (Exception $e) { |
|
| 239 | + if ($param->isDefaultValueAvailable()) { |
|
| 240 | 240 | $args[$param_name] = $param->getDefaultValue(); |
| 241 | 241 | } else { |
| 242 | 242 | throw $e; |
@@ -262,9 +262,9 @@ discard block |
||
| 262 | 262 | */ |
| 263 | 263 | protected function findParentService(string $name, string $class) |
| 264 | 264 | { |
| 265 | - if(isset($this->config[$name]['service'][$class])) { |
|
| 265 | + if (isset($this->config[$name]['service'][$class])) { |
|
| 266 | 266 | return $this->get($this->config[$name]['service'][$class]); |
| 267 | - } elseif(isset($this->config[$name]['parent'])) { |
|
| 267 | + } elseif (isset($this->config[$name]['parent'])) { |
|
| 268 | 268 | return $this->findParentService($this->config[$name]['parent'], $class); |
| 269 | 269 | } else { |
| 270 | 270 | return $this->get($class); |
@@ -283,14 +283,14 @@ discard block |
||
| 283 | 283 | protected function createInstance(string $name, ReflectionClass $class, array $args) |
| 284 | 284 | { |
| 285 | 285 | $instance = $class->newInstanceArgs($args); |
| 286 | - if($instance instanceof AdapterAwareInterface) { |
|
| 287 | - if(isset($this->config[$name]['adapter'])) { |
|
| 288 | - foreach($this->config[$name]['adapter'] as $adapter => $config) { |
|
| 289 | - if(!isset($config['name'])) { |
|
| 286 | + if ($instance instanceof AdapterAwareInterface) { |
|
| 287 | + if (isset($this->config[$name]['adapter'])) { |
|
| 288 | + foreach ($this->config[$name]['adapter'] as $adapter => $config) { |
|
| 289 | + if (!isset($config['name'])) { |
|
| 290 | 290 | throw new Exception('adapter requires name configuration'); |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | - if(isset($config['enabled']) && $config['enabled'] === '0') { |
|
| 293 | + if (isset($config['enabled']) && $config['enabled'] === '0') { |
|
| 294 | 294 | continue; |
| 295 | 295 | } |
| 296 | 296 | |
@@ -313,11 +313,11 @@ discard block |
||
| 313 | 313 | */ |
| 314 | 314 | public function getParam(string $name, string $param) |
| 315 | 315 | { |
| 316 | - if(!isset($this->config[$name]) && !isset($this->config[$name]['options'])) { |
|
| 316 | + if (!isset($this->config[$name]) && !isset($this->config[$name]['options'])) { |
|
| 317 | 317 | throw new Exception('no configuration available for service '.$name); |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - if(!isset($this->config[$name]['options'][$param])) { |
|
| 320 | + if (!isset($this->config[$name]['options'][$param])) { |
|
| 321 | 321 | throw new Exception('no configuration available for service parameter '.$param); |
| 322 | 322 | } |
| 323 | 323 | |