@@ -14,152 +14,152 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | 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 | } |
@@ -9,62 +9,62 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class ServiceDefinition 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 | } |
@@ -15,41 +15,41 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class ServiceContainerTest extends TestCase |
| 17 | 17 | { |
| 18 | - public function test_addService() |
|
| 19 | - { |
|
| 20 | - $definition = $this->getMockBuilder(ServiceDefinitionInterface::class)->getMock(); |
|
| 18 | + public function test_addService() |
|
| 19 | + { |
|
| 20 | + $definition = $this->getMockBuilder(ServiceDefinitionInterface::class)->getMock(); |
|
| 21 | 21 | |
| 22 | - $definition->expects($this->once()) |
|
| 23 | - ->method('getServiceName') |
|
| 24 | - ->willReturn('service.name'); |
|
| 22 | + $definition->expects($this->once()) |
|
| 23 | + ->method('getServiceName') |
|
| 24 | + ->willReturn('service.name'); |
|
| 25 | 25 | |
| 26 | - $this->assertSame('service.name', $definition->getServiceName()); |
|
| 27 | - } |
|
| 26 | + $this->assertSame('service.name', $definition->getServiceName()); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | // * @expectedException DuplicatedServiceException |
| 30 | - /** |
|
| 31 | - */ |
|
| 32 | - public function test_addServiceInstance() |
|
| 33 | - { |
|
| 34 | - $instance = $this->getMockBuilder(ServiceInstanceInterface::class)->getMock(); |
|
| 30 | + /** |
|
| 31 | + */ |
|
| 32 | + public function test_addServiceInstance() |
|
| 33 | + { |
|
| 34 | + $instance = $this->getMockBuilder(ServiceInstanceInterface::class)->getMock(); |
|
| 35 | 35 | |
| 36 | - $instance->expects($this->once()) |
|
| 37 | - ->method('getServiceName') |
|
| 38 | - ->willReturn('service.name'); |
|
| 36 | + $instance->expects($this->once()) |
|
| 37 | + ->method('getServiceName') |
|
| 38 | + ->willReturn('service.name'); |
|
| 39 | 39 | |
| 40 | 40 | |
| 41 | - $this->assertSame('service.name', $instance->getServiceName()); |
|
| 42 | - } |
|
| 41 | + $this->assertSame('service.name', $instance->getServiceName()); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - public function test_addServiceInstance2() |
|
| 45 | - { |
|
| 46 | - $instance = $this->getMockBuilder(ServiceInstanceInterface::class)->getMock(); |
|
| 44 | + public function test_addServiceInstance2() |
|
| 45 | + { |
|
| 46 | + $instance = $this->getMockBuilder(ServiceInstanceInterface::class)->getMock(); |
|
| 47 | 47 | |
| 48 | - $object = new \DateTime(); |
|
| 49 | - $instance->expects($this->once()) |
|
| 50 | - ->method('getInstance') |
|
| 51 | - ->willReturn($object); |
|
| 48 | + $object = new \DateTime(); |
|
| 49 | + $instance->expects($this->once()) |
|
| 50 | + ->method('getInstance') |
|
| 51 | + ->willReturn($object); |
|
| 52 | 52 | |
| 53 | - $this->assertSame($object, $instance->getInstance()); |
|
| 54 | - } |
|
| 53 | + $this->assertSame($object, $instance->getInstance()); |
|
| 54 | + } |
|
| 55 | 55 | } |