@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | protected function classConfig(string $id) : ?array |
| 36 | 36 | { |
| 37 | - if(!isset($this->chachedConfig[$id])) { |
|
| 37 | + if (!isset($this->chachedConfig[$id])) { |
|
| 38 | 38 | $config = $this->internalConfig($id); |
| 39 | 39 | $this->initRequired($config['#required'] ?? []); |
| 40 | 40 | unset($config['#required']); |
@@ -45,26 +45,26 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | protected function internalConfig(string $id) : ?array |
| 47 | 47 | { |
| 48 | - if(!isset($this->config[$id]) and !class_exists($id)) { |
|
| 48 | + if (!isset($this->config[$id]) and !class_exists($id)) { |
|
| 49 | 49 | return null; |
| 50 | 50 | } |
| 51 | 51 | $config = []; |
| 52 | - if(!$this->isAlias($id)) { |
|
| 53 | - foreach(array_reverse(class_parents($id)) as $parent) { |
|
| 52 | + if (!$this->isAlias($id)) { |
|
| 53 | + foreach (array_reverse(class_parents($id)) as $parent) { |
|
| 54 | 54 | $config = array_merge($config, $this->config[$parent] ?? []); |
| 55 | 55 | } |
| 56 | - } elseif(isset($this->config[$id]['#class'])) { |
|
| 57 | - foreach(array_reverse(class_parents($this->config[$id]['#class'])) as $parent) { |
|
| 56 | + } elseif (isset($this->config[$id]['#class'])) { |
|
| 57 | + foreach (array_reverse(class_parents($this->config[$id]['#class'])) as $parent) { |
|
| 58 | 58 | $config = array_merge($config, $this->config[$parent] ?? []); |
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | 61 | $config = array_merge($config, $this->config[$id] ?? []); |
| 62 | 62 | // |
| 63 | - if(empty($config['#class'])) { |
|
| 63 | + if (empty($config['#class'])) { |
|
| 64 | 64 | $config['#class'] = $id; |
| 65 | 65 | } |
| 66 | 66 | // |
| 67 | - if(!isset($config['#singleton'])) { |
|
| 67 | + if (!isset($config['#singleton'])) { |
|
| 68 | 68 | $config['#singleton'] = false; |
| 69 | 69 | } |
| 70 | 70 | return $config; |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | protected function internalSingleton($id, array $config) |
| 80 | 80 | { |
| 81 | - if(!isset($this->storage[$id])) { |
|
| 81 | + if (!isset($this->storage[$id])) { |
|
| 82 | 82 | $this->storage[$id] = $this->internalMake($id, $config); |
| 83 | 83 | } |
| 84 | 84 | return $this->storage[$id]; |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | $fullParameters = array_merge($parameters, $config); |
| 98 | 98 | $obj = $this->getObject($fullParameters); |
| 99 | 99 | // |
| 100 | - if(!$this->isAlias($id) and !is_a($obj, $id)) { |
|
| 100 | + if (!$this->isAlias($id) and !is_a($obj, $id)) { |
|
| 101 | 101 | throw new ContainerException('Wrong class, will be '.$id); |
| 102 | 102 | } |
| 103 | 103 | return $obj; |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | protected function initRequired(array $requirement) : void |
| 112 | 112 | { |
| 113 | 113 | foreach ($requirement as $className) { |
| 114 | - if(!isset($this->storage[$className])) { |
|
| 114 | + if (!isset($this->storage[$className])) { |
|
| 115 | 115 | $this->singleton($className); |
| 116 | 116 | } |
| 117 | 117 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | protected function getCallableDependency(callable $callable) : array |
| 127 | 127 | { |
| 128 | - if(is_array($callable)) { |
|
| 128 | + if (is_array($callable)) { |
|
| 129 | 129 | $className = get_class($callable[0]); |
| 130 | 130 | $classReflector = new \ReflectionClass($className); |
| 131 | 131 | return $classReflector->getMethod($callable[1])->getParameters(); |
@@ -149,10 +149,10 @@ discard block |
||
| 149 | 149 | private $reflectionCache = []; |
| 150 | 150 | private function getClassDependency(string $className) : array |
| 151 | 151 | { |
| 152 | - if(!isset($this->reflectionCache[$className])) { |
|
| 152 | + if (!isset($this->reflectionCache[$className])) { |
|
| 153 | 153 | $reflector = new \ReflectionClass($className); |
| 154 | 154 | $constructor = $reflector->getConstructor(); |
| 155 | - if(!is_null($constructor)) { |
|
| 155 | + if (!is_null($constructor)) { |
|
| 156 | 156 | $this->reflectionCache[$className] = $constructor->getParameters(); |
| 157 | 157 | } else { |
| 158 | 158 | $this->reflectionCache[$className] = []; |
@@ -168,13 +168,13 @@ discard block |
||
| 168 | 168 | */ |
| 169 | 169 | private function getObject(array $parameters = []) |
| 170 | 170 | { |
| 171 | - if(isset($parameters['#factory'])) { |
|
| 171 | + if (isset($parameters['#factory'])) { |
|
| 172 | 172 | return call_user_func_array($parameters['#factory'], [$parameters, $this]); |
| 173 | 173 | } |
| 174 | 174 | $className = $parameters['#class']; |
| 175 | 175 | // |
| 176 | 176 | $dependency = $this->getClassDependency($className); |
| 177 | - if(!empty($dependency)) { |
|
| 177 | + if (!empty($dependency)) { |
|
| 178 | 178 | $preparedParameters = $this->prepareParameters($dependency, $parameters); |
| 179 | 179 | } else { |
| 180 | 180 | $preparedParameters = []; |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | protected function _store(array $data, ?array $keys = null) : void |
| 29 | 29 | { |
| 30 | - foreach($keys ?? array_keys($data) as $key) { |
|
| 30 | + foreach ($keys ?? array_keys($data) as $key) { |
|
| 31 | 31 | $this->$key = $data[$key] ?? null; |
| 32 | 32 | } |
| 33 | 33 | } |
@@ -23,7 +23,7 @@ |
||
| 23 | 23 | |
| 24 | 24 | public static function getInstance() : DiInterface |
| 25 | 25 | { |
| 26 | - if(is_null(self::$instance)) { |
|
| 26 | + if (is_null(self::$instance)) { |
|
| 27 | 27 | self::$instance = new Container(); |
| 28 | 28 | } |
| 29 | 29 | return self::$instance; |
@@ -67,11 +67,11 @@ discard block |
||
| 67 | 67 | public function get($id) |
| 68 | 68 | { |
| 69 | 69 | $config = $this->classConfig($id); |
| 70 | - if(is_null($config)) { |
|
| 70 | + if (is_null($config)) { |
|
| 71 | 71 | throw new NotFoundException(); |
| 72 | 72 | } |
| 73 | 73 | // |
| 74 | - if($config['#singleton']) { |
|
| 74 | + if ($config['#singleton']) { |
|
| 75 | 75 | return $this->internalSingleton($id, $config); |
| 76 | 76 | } else { |
| 77 | 77 | return $this->internalMake($id, $config); |
@@ -88,12 +88,12 @@ discard block |
||
| 88 | 88 | public function make(string $id, array $parameters = []) |
| 89 | 89 | { |
| 90 | 90 | $config = $this->classConfig($id); |
| 91 | - if(is_null($config)) { |
|
| 91 | + if (is_null($config)) { |
|
| 92 | 92 | throw new NotFoundException(); |
| 93 | 93 | } |
| 94 | 94 | // |
| 95 | - if($config['#singleton']) { |
|
| 96 | - throw new ContainerException($id. ' is singlton!'); |
|
| 95 | + if ($config['#singleton']) { |
|
| 96 | + throw new ContainerException($id.' is singlton!'); |
|
| 97 | 97 | } |
| 98 | 98 | // |
| 99 | 99 | return $this->internalMake($id, $config, $parameters); |
@@ -108,12 +108,12 @@ discard block |
||
| 108 | 108 | public function singleton(string $id) |
| 109 | 109 | { |
| 110 | 110 | $config = $this->classConfig($id); |
| 111 | - if(is_null($config)) { |
|
| 111 | + if (is_null($config)) { |
|
| 112 | 112 | throw new NotFoundException(); |
| 113 | 113 | } |
| 114 | 114 | // |
| 115 | - if(!$config['#singleton']) { |
|
| 116 | - throw new ContainerException($id. ' NOT singlton!'); |
|
| 115 | + if (!$config['#singleton']) { |
|
| 116 | + throw new ContainerException($id.' NOT singlton!'); |
|
| 117 | 117 | } |
| 118 | 118 | // |
| 119 | 119 | return $this->internalSingleton($id, $config); |
@@ -27,10 +27,10 @@ discard block |
||
| 27 | 27 | protected function prepareParameters(array $dependency, array $parameters) : array |
| 28 | 28 | {
|
| 29 | 29 | $preparedParameters = []; |
| 30 | - foreach($dependency as $paramReflector) {
|
|
| 30 | + foreach ($dependency as $paramReflector) {
|
|
| 31 | 31 | $name = $paramReflector->name; |
| 32 | 32 | $type = $paramReflector->getType(); |
| 33 | - if(isset($parameters[$name])) {
|
|
| 33 | + if (isset($parameters[$name])) {
|
|
| 34 | 34 | $value = $parameters[$name]; |
| 35 | 35 | $preparedParameters[] = $this->prepareValue($type, $value); |
| 36 | 36 | } else {
|
@@ -42,9 +42,9 @@ discard block |
||
| 42 | 42 | |
| 43 | 43 | private function prepareValue($type, $value) |
| 44 | 44 | {
|
| 45 | - if(empty($type) or $type->isBuiltIn() or is_a($value, $type->getName())) {
|
|
| 45 | + if (empty($type) or $type->isBuiltIn() or is_a($value, $type->getName())) {
|
|
| 46 | 46 | return $value; |
| 47 | - } elseif(is_array($value)) {
|
|
| 47 | + } elseif (is_array($value)) {
|
|
| 48 | 48 | $className = $value['#class'] ?? $type->getName(); |
| 49 | 49 | return $this->make($className, $value); |
| 50 | 50 | } else {
|
@@ -60,22 +60,22 @@ discard block |
||
| 60 | 60 | private function getParameter(\ReflectionParameter $param) |
| 61 | 61 | {
|
| 62 | 62 | $type = $param->getType(); |
| 63 | - if(!empty($type) and !$type->isBuiltIn()) {
|
|
| 63 | + if (!empty($type) and !$type->isBuiltIn()) {
|
|
| 64 | 64 | return $this->get($type->getName()); |
| 65 | - } elseif($param->isDefaultValueAvailable()) {
|
|
| 65 | + } elseif ($param->isDefaultValueAvailable()) {
|
|
| 66 | 66 | return $param->getDefaultValue(); |
| 67 | - }elseif($type->allowsNull()) {
|
|
| 67 | + }elseif ($type->allowsNull()) {
|
|
| 68 | 68 | return null; |
| 69 | 69 | } else {
|
| 70 | - throw new ContainerException('Unknown parameter '. $param->name);
|
|
| 70 | + throw new ContainerException('Unknown parameter '.$param->name);
|
|
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | private function tryTransformValue(string $className, $value) |
| 75 | 75 | {
|
| 76 | - foreach($this->transformers as $transformer) {
|
|
| 76 | + foreach ($this->transformers as $transformer) {
|
|
| 77 | 77 | $value = $transformer($value, $className, $this); |
| 78 | - if(is_a($value, $className)) {
|
|
| 78 | + if (is_a($value, $className)) {
|
|
| 79 | 79 | return $value; |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | return $this->get($type->getName()); |
| 65 | 65 | } elseif($param->isDefaultValueAvailable()) {
|
| 66 | 66 | return $param->getDefaultValue(); |
| 67 | - }elseif($type->allowsNull()) {
|
|
| 67 | + } elseif($type->allowsNull()) {
|
|
| 68 | 68 | return null; |
| 69 | 69 | } else {
|
| 70 | 70 | throw new ContainerException('Unknown parameter '. $param->name);
|
@@ -37,7 +37,7 @@ |
||
| 37 | 37 | protected function serviceLocator(string $id, ?array $parameters = null) |
| 38 | 38 | { |
| 39 | 39 | $di = Di::getInstance(); |
| 40 | - if(is_null($parameters)) { |
|
| 40 | + if (is_null($parameters)) { |
|
| 41 | 41 | return $di->get($id); |
| 42 | 42 | } else { |
| 43 | 43 | return $di->make($id, $parameters); |