@@ -10,10 +10,10 @@ |
||
| 10 | 10 | interface ServiceInstanceInterface |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** @return string */ |
|
| 14 | - public function getServiceName(); |
|
| 13 | + /** @return string */ |
|
| 14 | + public function getServiceName(); |
|
| 15 | 15 | |
| 16 | - /** @return mixed */ |
|
| 17 | - public function getInstance(); |
|
| 16 | + /** @return mixed */ |
|
| 17 | + public function getInstance(); |
|
| 18 | 18 | |
| 19 | 19 | } |
@@ -9,62 +9,62 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Definition implements ServiceDefinitionInterface |
| 11 | 11 | { |
| 12 | - /** @var string */ |
|
| 13 | - private $serviceName; |
|
| 12 | + /** @var string */ |
|
| 13 | + private $serviceName; |
|
| 14 | 14 | |
| 15 | - /** @var string */ |
|
| 16 | - private $class; |
|
| 15 | + /** @var string */ |
|
| 16 | + private $class; |
|
| 17 | 17 | |
| 18 | - /** @var array */ |
|
| 19 | - private $constructArgs; |
|
| 18 | + /** @var array */ |
|
| 19 | + private $constructArgs; |
|
| 20 | 20 | |
| 21 | - /** @var array */ |
|
| 22 | - private $calls; |
|
| 21 | + /** @var array */ |
|
| 22 | + private $calls; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * RepositoryFactory constructor. |
|
| 26 | - * |
|
| 27 | - * @param string $serviceName |
|
| 28 | - * @param string $class |
|
| 29 | - * @param array $args |
|
| 30 | - * @param array $calls |
|
| 31 | - */ |
|
| 32 | - public function __construct(string $serviceName, string $class, array $args = [], array $calls = []) |
|
| 33 | - { |
|
| 34 | - $this->serviceName = $serviceName; |
|
| 35 | - $this->class = $class; |
|
| 36 | - $this->constructArgs = $args; |
|
| 37 | - $this->calls = $calls; |
|
| 38 | - } |
|
| 24 | + /** |
|
| 25 | + * RepositoryFactory constructor. |
|
| 26 | + * |
|
| 27 | + * @param string $serviceName |
|
| 28 | + * @param string $class |
|
| 29 | + * @param array $args |
|
| 30 | + * @param array $calls |
|
| 31 | + */ |
|
| 32 | + public function __construct(string $serviceName, string $class, array $args = [], array $calls = []) |
|
| 33 | + { |
|
| 34 | + $this->serviceName = $serviceName; |
|
| 35 | + $this->class = $class; |
|
| 36 | + $this->constructArgs = $args; |
|
| 37 | + $this->calls = $calls; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function getClass() |
|
| 44 | - { |
|
| 45 | - return $this->class; |
|
| 46 | - } |
|
| 40 | + /** |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function getClass() |
|
| 44 | + { |
|
| 45 | + return $this->class; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** @return string */ |
|
| 49 | - public function getServiceName() |
|
| 50 | - { |
|
| 51 | - return $this->serviceName; |
|
| 52 | - } |
|
| 48 | + /** @return string */ |
|
| 49 | + public function getServiceName() |
|
| 50 | + { |
|
| 51 | + return $this->serviceName; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return array |
|
| 56 | - */ |
|
| 57 | - public function getConstructArgs() |
|
| 58 | - { |
|
| 59 | - return $this->constructArgs; |
|
| 60 | - } |
|
| 54 | + /** |
|
| 55 | + * @return array |
|
| 56 | + */ |
|
| 57 | + public function getConstructArgs() |
|
| 58 | + { |
|
| 59 | + return $this->constructArgs; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return array |
|
| 64 | - */ |
|
| 65 | - public function getCalls() |
|
| 66 | - { |
|
| 67 | - return $this->calls; |
|
| 68 | - } |
|
| 62 | + /** |
|
| 63 | + * @return array |
|
| 64 | + */ |
|
| 65 | + public function getCalls() |
|
| 66 | + { |
|
| 67 | + return $this->calls; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | 70 | } |
@@ -9,28 +9,28 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class InstanceDefinition implements ServiceInstanceInterface |
| 11 | 11 | { |
| 12 | - /** @var string */ |
|
| 13 | - private $serviceName; |
|
| 12 | + /** @var string */ |
|
| 13 | + private $serviceName; |
|
| 14 | 14 | |
| 15 | - /** @var mixed */ |
|
| 16 | - private $instance; |
|
| 15 | + /** @var mixed */ |
|
| 16 | + private $instance; |
|
| 17 | 17 | |
| 18 | - public function __construct(string $serviceName, $instance) |
|
| 19 | - { |
|
| 20 | - $this->serviceName = $serviceName; |
|
| 21 | - $this->instance = $instance; |
|
| 22 | - } |
|
| 18 | + public function __construct(string $serviceName, $instance) |
|
| 19 | + { |
|
| 20 | + $this->serviceName = $serviceName; |
|
| 21 | + $this->instance = $instance; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** @return string */ |
|
| 25 | - public function getServiceName() |
|
| 26 | - { |
|
| 27 | - return $this->serviceName; |
|
| 28 | - } |
|
| 24 | + /** @return string */ |
|
| 25 | + public function getServiceName() |
|
| 26 | + { |
|
| 27 | + return $this->serviceName; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** @return mixed */ |
|
| 31 | - public function getInstance() |
|
| 32 | - { |
|
| 33 | - return $this->instance; |
|
| 34 | - } |
|
| 30 | + /** @return mixed */ |
|
| 31 | + public function getInstance() |
|
| 32 | + { |
|
| 33 | + return $this->instance; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -10,16 +10,16 @@ |
||
| 10 | 10 | interface ServiceDefinitionInterface |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** @return string */ |
|
| 14 | - public function getServiceName(); |
|
| 13 | + /** @return string */ |
|
| 14 | + public function getServiceName(); |
|
| 15 | 15 | |
| 16 | - /** @return string */ |
|
| 17 | - public function getClass(); |
|
| 16 | + /** @return string */ |
|
| 17 | + public function getClass(); |
|
| 18 | 18 | |
| 19 | - /** @return array */ |
|
| 20 | - public function getConstructArgs(); |
|
| 19 | + /** @return array */ |
|
| 20 | + public function getConstructArgs(); |
|
| 21 | 21 | |
| 22 | - /** @return array */ |
|
| 23 | - public function getCalls(); |
|
| 22 | + /** @return array */ |
|
| 23 | + public function getCalls(); |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -14,152 +14,152 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | final class ServiceContainer implements ContainerInterface |
| 16 | 16 | { |
| 17 | - /** @var ServiceDefinitionInterface[] */ |
|
| 18 | - private $services; |
|
| 19 | - |
|
| 20 | - /** @var array */ |
|
| 21 | - private $serviceInstances; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * ServiceContainer constructor. |
|
| 25 | - */ |
|
| 26 | - public function __construct() |
|
| 27 | - { |
|
| 28 | - $this->serviceInstances = []; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
| 33 | - */ |
|
| 34 | - public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
| 35 | - { |
|
| 36 | - $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @param ServiceInstanceInterface $definition |
|
| 41 | - * @throws DuplicatedServiceException |
|
| 42 | - */ |
|
| 43 | - public function addServiceInstance(ServiceInstanceInterface $definition) |
|
| 44 | - { |
|
| 45 | - if (true === array_key_exists($definition->getServiceName(), $this->serviceInstances)) { |
|
| 46 | - throw new DuplicatedServiceException( |
|
| 47 | - 'The service' . $definition->getServiceName() . ' is already registered in the container' |
|
| 48 | - ); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param string $index |
|
| 56 | - * @param bool $new |
|
| 57 | - * @return mixed|null |
|
| 58 | - */ |
|
| 59 | - public function get($index, $new = false) |
|
| 60 | - { |
|
| 61 | - // if service is shared true and already exists we return it |
|
| 62 | - if (isset($this->serviceInstances[$index]) && false === $new) { |
|
| 63 | - return $this->serviceInstances[$index]; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // is service not defined return null |
|
| 67 | - if (!isset($this->services[$index])) { |
|
| 68 | - return null; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // we create a new instance |
|
| 72 | - $instance = $this->create($this->services[$index]); |
|
| 73 | - |
|
| 74 | - // if shared true we set it in the current instances |
|
| 75 | - if (false === $new) { |
|
| 76 | - $this->serviceInstances[$index] = $instance; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $instance; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Returns true if the container can return an entry for the given identifier. |
|
| 84 | - * Returns false otherwise. |
|
| 85 | - * |
|
| 86 | - * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
| 87 | - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
| 88 | - * |
|
| 89 | - * @param string $index Identifier of the entry to look for. |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function has($index) |
|
| 94 | - { |
|
| 95 | - // if service is shared true and already exists we return it |
|
| 96 | - if (isset($this->serviceInstances[$index])) { |
|
| 97 | - return true; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // is service not defined return null |
|
| 101 | - if (isset($this->services[$index])) { |
|
| 102 | - return true; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
| 110 | - * @return mixed |
|
| 111 | - */ |
|
| 112 | - private function create(ServiceDefinitionInterface $serviceDefinition) |
|
| 113 | - { |
|
| 114 | - // we get the class name in a var for the new later |
|
| 115 | - $newClass = $serviceDefinition->getClass(); |
|
| 116 | - |
|
| 117 | - // we get the construct args |
|
| 118 | - $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
| 119 | - $instance = new $newClass(...$args); |
|
| 120 | - |
|
| 121 | - // we call the injection methods after we instance the object |
|
| 122 | - $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
| 123 | - foreach ($calls as $call => $params) { |
|
| 124 | - call_user_func_array([$instance, $call], $params); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - // we create the new instance |
|
| 128 | - return $instance; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param array $args |
|
| 133 | - * @return array |
|
| 134 | - */ |
|
| 135 | - private function checkArgsServices(array $args) |
|
| 136 | - { |
|
| 137 | - // we check if any of the construct args are services themself and we create them |
|
| 138 | - foreach ($args as $k => $arg) { |
|
| 139 | - if (true === $this->has($arg)) { |
|
| 140 | - $args[$k] = $this->get($arg); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - return $args; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param array $calls |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 151 | - private function checkCallsServices(array $calls) |
|
| 152 | - { |
|
| 153 | - // we check if any of the calls args are services themself and we create them |
|
| 154 | - foreach ($calls as $callArgs) { |
|
| 155 | - foreach ($callArgs as $k => $arg) { |
|
| 156 | - if (true === $this->has($arg)) { |
|
| 157 | - $args[$k] = $this->get($arg); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return $calls; |
|
| 163 | - } |
|
| 17 | + /** @var ServiceDefinitionInterface[] */ |
|
| 18 | + private $services; |
|
| 19 | + |
|
| 20 | + /** @var array */ |
|
| 21 | + private $serviceInstances; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * ServiceContainer constructor. |
|
| 25 | + */ |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | + $this->serviceInstances = []; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
| 33 | + */ |
|
| 34 | + public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
| 35 | + { |
|
| 36 | + $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @param ServiceInstanceInterface $definition |
|
| 41 | + * @throws DuplicatedServiceException |
|
| 42 | + */ |
|
| 43 | + public function addServiceInstance(ServiceInstanceInterface $definition) |
|
| 44 | + { |
|
| 45 | + if (true === array_key_exists($definition->getServiceName(), $this->serviceInstances)) { |
|
| 46 | + throw new DuplicatedServiceException( |
|
| 47 | + 'The service' . $definition->getServiceName() . ' is already registered in the container' |
|
| 48 | + ); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param string $index |
|
| 56 | + * @param bool $new |
|
| 57 | + * @return mixed|null |
|
| 58 | + */ |
|
| 59 | + public function get($index, $new = false) |
|
| 60 | + { |
|
| 61 | + // if service is shared true and already exists we return it |
|
| 62 | + if (isset($this->serviceInstances[$index]) && false === $new) { |
|
| 63 | + return $this->serviceInstances[$index]; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // is service not defined return null |
|
| 67 | + if (!isset($this->services[$index])) { |
|
| 68 | + return null; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // we create a new instance |
|
| 72 | + $instance = $this->create($this->services[$index]); |
|
| 73 | + |
|
| 74 | + // if shared true we set it in the current instances |
|
| 75 | + if (false === $new) { |
|
| 76 | + $this->serviceInstances[$index] = $instance; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $instance; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Returns true if the container can return an entry for the given identifier. |
|
| 84 | + * Returns false otherwise. |
|
| 85 | + * |
|
| 86 | + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
| 87 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
| 88 | + * |
|
| 89 | + * @param string $index Identifier of the entry to look for. |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function has($index) |
|
| 94 | + { |
|
| 95 | + // if service is shared true and already exists we return it |
|
| 96 | + if (isset($this->serviceInstances[$index])) { |
|
| 97 | + return true; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // is service not defined return null |
|
| 101 | + if (isset($this->services[$index])) { |
|
| 102 | + return true; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
| 110 | + * @return mixed |
|
| 111 | + */ |
|
| 112 | + private function create(ServiceDefinitionInterface $serviceDefinition) |
|
| 113 | + { |
|
| 114 | + // we get the class name in a var for the new later |
|
| 115 | + $newClass = $serviceDefinition->getClass(); |
|
| 116 | + |
|
| 117 | + // we get the construct args |
|
| 118 | + $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
| 119 | + $instance = new $newClass(...$args); |
|
| 120 | + |
|
| 121 | + // we call the injection methods after we instance the object |
|
| 122 | + $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
| 123 | + foreach ($calls as $call => $params) { |
|
| 124 | + call_user_func_array([$instance, $call], $params); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + // we create the new instance |
|
| 128 | + return $instance; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param array $args |
|
| 133 | + * @return array |
|
| 134 | + */ |
|
| 135 | + private function checkArgsServices(array $args) |
|
| 136 | + { |
|
| 137 | + // we check if any of the construct args are services themself and we create them |
|
| 138 | + foreach ($args as $k => $arg) { |
|
| 139 | + if (true === $this->has($arg)) { |
|
| 140 | + $args[$k] = $this->get($arg); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + return $args; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param array $calls |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | + private function checkCallsServices(array $calls) |
|
| 152 | + { |
|
| 153 | + // we check if any of the calls args are services themself and we create them |
|
| 154 | + foreach ($calls as $callArgs) { |
|
| 155 | + foreach ($callArgs as $k => $arg) { |
|
| 156 | + if (true === $this->has($arg)) { |
|
| 157 | + $args[$k] = $this->get($arg); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return $calls; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | 165 | } |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | declare( strict_types = 1 ); |
| 3 | 3 | |
| 4 | 4 | if ( PHP_SAPI !== 'cli' ) { |
| 5 | - die( 'Not an entry point' ); |
|
| 5 | + die( 'Not an entry point' ); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | error_reporting( -1 ); |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | ini_set( 'display_errors', '1' ); |
| 11 | 11 | |
| 12 | 12 | if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
| 13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 13 | + die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -1,16 +1,16 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare( strict_types = 1 ); |
|
| 2 | +declare(strict_types=1); |
|
| 3 | 3 | |
| 4 | -if ( PHP_SAPI !== 'cli' ) { |
|
| 5 | - die( 'Not an entry point' ); |
|
| 4 | +if (PHP_SAPI !== 'cli') { |
|
| 5 | + die('Not an entry point'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | error_reporting( -1 ); |
| 9 | 9 | |
| 10 | -ini_set( 'display_errors', '1' ); |
|
| 10 | +ini_set('display_errors', '1'); |
|
| 11 | 11 | |
| 12 | -if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
|
| 13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 12 | +if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { |
|
| 13 | + die('You need to install this package with Composer before you can run the tests'); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |