| Total Complexity | 52 |
| Total Lines | 360 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Container often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Container implements ContainerInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Config. |
||
| 25 | * |
||
| 26 | * @var Iterable |
||
| 27 | */ |
||
| 28 | protected $config = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Service registry. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $service = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Registered but not initialized service registry. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $registry = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Parent container |
||
| 46 | * |
||
| 47 | * @var ContainerInterface |
||
| 48 | */ |
||
| 49 | protected $parent; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Children container |
||
| 53 | * |
||
| 54 | * @var ContainerInterface[] |
||
| 55 | */ |
||
| 56 | protected $children = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Create container. |
||
| 60 | * |
||
| 61 | * @param Iterable $config |
||
| 62 | */ |
||
| 63 | public function __construct(Iterable $config = [], ?ContainerInterface $parent=null) |
||
| 64 | { |
||
| 65 | $this->config = $config; |
||
| 66 | $this->parent = $parent; |
||
| 67 | $this->add(ContainerInterface::class, $this); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get service. |
||
| 72 | * |
||
| 73 | * @param string $name |
||
| 74 | * |
||
| 75 | * @return mixed |
||
| 76 | */ |
||
| 77 | public function get($name) |
||
| 78 | { |
||
| 79 | if ($this->has($name)) { |
||
| 80 | return $this->service[$name]; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (isset($this->registry[$name])) { |
||
| 84 | return $this->addStaticService($name); |
||
| 85 | } |
||
| 86 | |||
| 87 | if(isset($this->config[$name])) { |
||
| 88 | return $this->autoWireClass($name); |
||
| 89 | } |
||
| 90 | |||
| 91 | try { |
||
| 92 | return $this->lookupService($name); |
||
| 93 | } catch(Exception\ServiceNotFound $e) { |
||
| 94 | return $this->autoWireClass($name); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Check for static injections |
||
| 100 | * |
||
| 101 | * @param string $name |
||
| 102 | * @return mixed |
||
| 103 | */ |
||
| 104 | protected function addStaticService(string $name) |
||
| 105 | { |
||
| 106 | if( $this->registry[$name] instanceof Closure) { |
||
| 107 | $this->service[$name] = $this->registry[$name]->call($this); |
||
| 108 | } else { |
||
| 109 | $this->service[$name] = $this->registry[$name]; |
||
| 110 | } |
||
| 111 | |||
| 112 | unset($this->registry[$name]); |
||
| 113 | return $this->service[$name]; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Traverse tree up and look for service |
||
| 118 | * |
||
| 119 | * @param string $name |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | public function lookupService(string $name) |
||
| 123 | { |
||
| 124 | if ($this->has($name)) { |
||
| 125 | return $this->service[$name]; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (isset($this->registry[$name])) { |
||
| 129 | return $this->addStaticService($name); |
||
| 130 | } |
||
| 131 | |||
| 132 | if(isset($this->config[$name])) { |
||
| 133 | return $this->autoWireClass($name); |
||
| 134 | } |
||
| 135 | |||
| 136 | if($this->parent !== null) { |
||
| 137 | return $this->parent->lookupService($name); |
||
| 138 | } |
||
| 139 | |||
| 140 | throw new Exception\ServiceNotFound("service $name was not found in service tree"); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get new instance (Do not store in container). |
||
| 145 | * |
||
| 146 | * @param string $name |
||
| 147 | * |
||
| 148 | * @return mixed |
||
| 149 | */ |
||
| 150 | public function getNew(string $name) |
||
| 151 | { |
||
| 152 | if (isset($this->registry[$name])) { |
||
| 153 | return $this->registry[$name]->call($this); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $this->autoWireClass($name); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Add service. |
||
| 161 | * |
||
| 162 | * @param string $name |
||
| 163 | * @param mixed $service |
||
| 164 | * |
||
| 165 | * @return Container |
||
| 166 | */ |
||
| 167 | public function add(string $name, $service): self |
||
| 168 | { |
||
| 169 | if ($this->has($name)) { |
||
| 170 | throw new Exception\ServiceAlreadyExists('service '.$name.' is already registered'); |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->registry[$name] = $service; |
||
| 174 | |||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Check if service is registered. |
||
| 180 | * |
||
| 181 | * @param mixed $name |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function has($name): bool |
||
| 186 | { |
||
| 187 | return isset($this->service[$name]); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Auto wire. |
||
| 192 | * |
||
| 193 | * @param string $name |
||
| 194 | * @param array $config |
||
| 195 | * @param array $parents |
||
| 196 | * |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | protected function autoWireClass(string $name) |
||
| 200 | { |
||
| 201 | $class = $name; |
||
| 202 | |||
| 203 | $config = $this->config; |
||
|
|
|||
| 204 | if (isset($this->config[$name])) { |
||
| 205 | $config = $this->config[$name]; |
||
| 206 | } else { |
||
| 207 | $config = []; |
||
| 208 | } |
||
| 209 | |||
| 210 | if (isset($config['use'])) { |
||
| 211 | $class = $config['use']; |
||
| 212 | } |
||
| 213 | |||
| 214 | if(preg_match('#^\{(.*)\}$#', $class, $match)) { |
||
| 215 | $service = $this->get($match[1]); |
||
| 216 | |||
| 217 | if(isset($this->config[$name]['selects'])) { |
||
| 218 | $reflection = new ReflectionClass(get_class($service)); |
||
| 219 | |||
| 220 | foreach($this->config[$name]['selects'] as $select) { |
||
| 221 | $args = $this->autoWireMethod($name, $reflection->getMethod($select['method']), $select); |
||
| 222 | $service = call_user_func_array([&$service, $select['method']], $args); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return $this->service[$name] = $service; |
||
| 227 | } |
||
| 228 | |||
| 229 | try { |
||
| 230 | $reflection = new ReflectionClass($class); |
||
| 231 | } catch (\Exception $e) { |
||
| 232 | throw new Exception\ServiceNotFound($class.' can not be resolved to an existing class for service '.$name); |
||
| 233 | } |
||
| 234 | |||
| 235 | $constructor = $reflection->getConstructor(); |
||
| 236 | |||
| 237 | if (null === $constructor) { |
||
| 238 | return new $class(); |
||
| 239 | } |
||
| 240 | |||
| 241 | $args = $this->autoWireMethod($name, $constructor, $config); |
||
| 242 | return $this->createInstance($name, $reflection, $args); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Create instance. |
||
| 247 | * |
||
| 248 | * @param string $name |
||
| 249 | * @param ReflectionClass $class |
||
| 250 | * @param array $arguments |
||
| 251 | * |
||
| 252 | * @return mixed |
||
| 253 | */ |
||
| 254 | protected function createInstance(string $name, ReflectionClass $class, array $arguments) |
||
| 255 | { |
||
| 256 | $instance = $class->newInstanceArgs($arguments); |
||
| 257 | $this->service[$name] = $instance; |
||
| 258 | |||
| 259 | if(isset($this->config[$name]['calls'])) { |
||
| 260 | foreach($this->config[$name]['calls'] as $call) { |
||
| 261 | if(!isset($call['method'])) { |
||
| 262 | throw new Exception\Configuration('method is required for setter injection in service '.$name); |
||
| 263 | } |
||
| 264 | |||
| 265 | $arguments = []; |
||
| 266 | try { |
||
| 267 | $method = $class->getMethod($call['method']); |
||
| 268 | } catch(\ReflectionException $e) { |
||
| 269 | throw new Exception\Configuration('method '.$call['method'].' is not callable in class '.$class->getName().' for service '.$name); |
||
| 270 | } |
||
| 271 | |||
| 272 | $arguments = $this->autoWireMethod($name, $method, $call); |
||
| 273 | call_user_func_array([&$instance, $call['method']], $arguments); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | return $instance; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Autowire method |
||
| 282 | * |
||
| 283 | * @param string $name |
||
| 284 | * @param ReflectionMethod $method |
||
| 285 | * @param array $config |
||
| 286 | * @return array |
||
| 287 | */ |
||
| 288 | protected function autoWireMethod(string $name, ReflectionMethod $method, array $config): array |
||
| 317 | } |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * Parse param value. |
||
| 322 | * |
||
| 323 | * @param mixed $param |
||
| 324 | * @param string $name |
||
| 325 | * |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | protected function parseParam($param, string $name) |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | /** |
||
| 364 | * Locate service |
||
| 365 | * |
||
| 366 | * @param string $current_service |
||
| 367 | * @param string $service |
||
| 368 | */ |
||
| 369 | protected function findService(string $current_service, string $service) |
||
| 381 | } |
||
| 382 | } |
||
| 383 |