@@ -9,8 +9,8 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | final class Fields |
| 11 | 11 | { |
| 12 | - const ROOT_CONFIG_NAME = 'services'; |
|
| 13 | - const CLASS_CONFIG_NAME = 'class'; |
|
| 14 | - const CONSTRUCTOR_PARAMS_CONFIG_NAME = 'arguments'; |
|
| 15 | - const METHOD_CALLS_CONFIG_NAME = 'calls'; |
|
| 12 | + const ROOT_CONFIG_NAME = 'services'; |
|
| 13 | + const CLASS_CONFIG_NAME = 'class'; |
|
| 14 | + const CONSTRUCTOR_PARAMS_CONFIG_NAME = 'arguments'; |
|
| 15 | + const METHOD_CALLS_CONFIG_NAME = 'calls'; |
|
| 16 | 16 | } |
@@ -10,16 +10,16 @@ |
||
| 10 | 10 | interface ParserInterface |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @param string $filePath |
|
| 15 | - * @return array |
|
| 16 | - */ |
|
| 17 | - public function loadFile(string $filePath): array; |
|
| 13 | + /** |
|
| 14 | + * @param string $filePath |
|
| 15 | + * @return array |
|
| 16 | + */ |
|
| 17 | + public function loadFile(string $filePath): array; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param string $directorPath |
|
| 21 | - * @return array |
|
| 22 | - */ |
|
| 23 | - public function loadDirectory(string $directorPath): array; |
|
| 19 | + /** |
|
| 20 | + * @param string $directorPath |
|
| 21 | + * @return array |
|
| 22 | + */ |
|
| 23 | + public function loadDirectory(string $directorPath): array; |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -15,122 +15,122 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class ServiceLoader |
| 17 | 17 | { |
| 18 | - /** @var ParserInterface */ |
|
| 19 | - private $parser; |
|
| 20 | - |
|
| 21 | - /** @var ConfigurationValidator */ |
|
| 22 | - private $validator; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * ServiceLoader constructor. |
|
| 26 | - * @param ParserInterface $parser |
|
| 27 | - */ |
|
| 28 | - public function __construct(ParserInterface $parser) |
|
| 29 | - { |
|
| 30 | - $this->parser = $parser; |
|
| 31 | - $this->validator = new ConfigurationValidator(); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param string $filePath |
|
| 36 | - * @return ServiceDefinition[] |
|
| 37 | - * @throws InvalidConfigFormatException |
|
| 38 | - */ |
|
| 39 | - public function loadFile(string $filePath) |
|
| 40 | - { |
|
| 41 | - $definitionList = $this->parser->loadFile($filePath); |
|
| 42 | - |
|
| 43 | - return $this->loadDefinitions($definitionList); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param string $directoryPath |
|
| 48 | - * @return ServiceDefinition[] |
|
| 49 | - * @throws InvalidConfigFormatException |
|
| 50 | - */ |
|
| 51 | - public function loadDirectory(string $directoryPath) |
|
| 52 | - { |
|
| 53 | - $definitionList = $this->parser->loadDirectory($directoryPath); |
|
| 54 | - |
|
| 55 | - $list = []; |
|
| 56 | - foreach ($definitionList as $item) { |
|
| 57 | - $list = array_merge($list, $this->loadDefinitions($item)); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - return $list; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param array $definitionList |
|
| 65 | - * @return ServiceDefinition[] |
|
| 66 | - * @throws InvalidConfigFormatException |
|
| 67 | - */ |
|
| 68 | - private function loadDefinitions(array $definitionList) |
|
| 69 | - { |
|
| 70 | - $this->validator->validateDefinitions($definitionList); |
|
| 71 | - |
|
| 72 | - if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
| 73 | - return []; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $list = []; |
|
| 77 | - foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $serviceConfig) { |
|
| 78 | - $list[] = $this->initServiceDefinition($serviceName, $serviceConfig); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return $list; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $serviceName |
|
| 86 | - * @param array $serviceConfig |
|
| 87 | - * @return ServiceDefinition |
|
| 88 | - */ |
|
| 89 | - private function initServiceDefinition(string $serviceName, array $serviceConfig) |
|
| 90 | - { |
|
| 91 | - $definition = new ServiceDefinition( |
|
| 92 | - $serviceName, |
|
| 93 | - $this->getClass($serviceConfig), |
|
| 94 | - $this->getConstructorParams($serviceConfig), |
|
| 95 | - $this->getMethodCalls($serviceConfig) |
|
| 96 | - ); |
|
| 97 | - |
|
| 98 | - return $definition; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param $serviceConfig |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - private function getClass($serviceConfig) |
|
| 106 | - { |
|
| 107 | - return $serviceConfig[Fields::CLASS_CONFIG_NAME]; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param $serviceConfig |
|
| 112 | - * @return array |
|
| 113 | - */ |
|
| 114 | - private function getConstructorParams($serviceConfig) |
|
| 115 | - { |
|
| 116 | - if (false === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $serviceConfig)) { |
|
| 117 | - return []; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $serviceConfig[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param $serviceConfig |
|
| 125 | - * @return array |
|
| 126 | - */ |
|
| 127 | - private function getMethodCalls($serviceConfig) |
|
| 128 | - { |
|
| 129 | - if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $serviceConfig)) { |
|
| 130 | - return []; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $serviceConfig[Fields::METHOD_CALLS_CONFIG_NAME]; |
|
| 134 | - } |
|
| 18 | + /** @var ParserInterface */ |
|
| 19 | + private $parser; |
|
| 20 | + |
|
| 21 | + /** @var ConfigurationValidator */ |
|
| 22 | + private $validator; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * ServiceLoader constructor. |
|
| 26 | + * @param ParserInterface $parser |
|
| 27 | + */ |
|
| 28 | + public function __construct(ParserInterface $parser) |
|
| 29 | + { |
|
| 30 | + $this->parser = $parser; |
|
| 31 | + $this->validator = new ConfigurationValidator(); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param string $filePath |
|
| 36 | + * @return ServiceDefinition[] |
|
| 37 | + * @throws InvalidConfigFormatException |
|
| 38 | + */ |
|
| 39 | + public function loadFile(string $filePath) |
|
| 40 | + { |
|
| 41 | + $definitionList = $this->parser->loadFile($filePath); |
|
| 42 | + |
|
| 43 | + return $this->loadDefinitions($definitionList); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param string $directoryPath |
|
| 48 | + * @return ServiceDefinition[] |
|
| 49 | + * @throws InvalidConfigFormatException |
|
| 50 | + */ |
|
| 51 | + public function loadDirectory(string $directoryPath) |
|
| 52 | + { |
|
| 53 | + $definitionList = $this->parser->loadDirectory($directoryPath); |
|
| 54 | + |
|
| 55 | + $list = []; |
|
| 56 | + foreach ($definitionList as $item) { |
|
| 57 | + $list = array_merge($list, $this->loadDefinitions($item)); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + return $list; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param array $definitionList |
|
| 65 | + * @return ServiceDefinition[] |
|
| 66 | + * @throws InvalidConfigFormatException |
|
| 67 | + */ |
|
| 68 | + private function loadDefinitions(array $definitionList) |
|
| 69 | + { |
|
| 70 | + $this->validator->validateDefinitions($definitionList); |
|
| 71 | + |
|
| 72 | + if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
| 73 | + return []; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $list = []; |
|
| 77 | + foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $serviceConfig) { |
|
| 78 | + $list[] = $this->initServiceDefinition($serviceName, $serviceConfig); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return $list; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $serviceName |
|
| 86 | + * @param array $serviceConfig |
|
| 87 | + * @return ServiceDefinition |
|
| 88 | + */ |
|
| 89 | + private function initServiceDefinition(string $serviceName, array $serviceConfig) |
|
| 90 | + { |
|
| 91 | + $definition = new ServiceDefinition( |
|
| 92 | + $serviceName, |
|
| 93 | + $this->getClass($serviceConfig), |
|
| 94 | + $this->getConstructorParams($serviceConfig), |
|
| 95 | + $this->getMethodCalls($serviceConfig) |
|
| 96 | + ); |
|
| 97 | + |
|
| 98 | + return $definition; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param $serviceConfig |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + private function getClass($serviceConfig) |
|
| 106 | + { |
|
| 107 | + return $serviceConfig[Fields::CLASS_CONFIG_NAME]; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param $serviceConfig |
|
| 112 | + * @return array |
|
| 113 | + */ |
|
| 114 | + private function getConstructorParams($serviceConfig) |
|
| 115 | + { |
|
| 116 | + if (false === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $serviceConfig)) { |
|
| 117 | + return []; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $serviceConfig[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param $serviceConfig |
|
| 125 | + * @return array |
|
| 126 | + */ |
|
| 127 | + private function getMethodCalls($serviceConfig) |
|
| 128 | + { |
|
| 129 | + if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $serviceConfig)) { |
|
| 130 | + return []; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $serviceConfig[Fields::METHOD_CALLS_CONFIG_NAME]; |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | 136 | } |
@@ -13,109 +13,109 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ConfigurationValidator |
| 15 | 15 | { |
| 16 | - private const ERROR_PREFIX = 'The service configuration'; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param array $definitionList |
|
| 20 | - * @throws InvalidConfigFormatException |
|
| 21 | - */ |
|
| 22 | - public function validateDefinitions(array $definitionList) |
|
| 23 | - { |
|
| 24 | - $this->validateRoot($definitionList); |
|
| 25 | - |
|
| 26 | - if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
| 27 | - return; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $service) { |
|
| 31 | - $this->validateService($serviceName, $service); |
|
| 32 | - } |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @param array $definitionList |
|
| 37 | - * @throws InvalidConfigFormatException |
|
| 38 | - */ |
|
| 39 | - private function validateRoot(array $definitionList) |
|
| 40 | - { |
|
| 41 | - if (false === array_key_exists(Fields::ROOT_CONFIG_NAME, $definitionList)) { |
|
| 42 | - throw new InvalidConfigFormatException( |
|
| 43 | - self::ERROR_PREFIX . ' need the root config key "' . Fields::ROOT_CONFIG_NAME . '"' |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $field = $definitionList[Fields::ROOT_CONFIG_NAME]; |
|
| 48 | - if (false === is_array($field) && false === is_null($field)) { |
|
| 49 | - throw new InvalidConfigFormatException( |
|
| 50 | - self::ERROR_PREFIX . ' key "' . Fields::ROOT_CONFIG_NAME . '" must be an associative array' |
|
| 51 | - ); |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param $serviceName |
|
| 57 | - * @param $service |
|
| 58 | - * @throws InvalidConfigFormatException |
|
| 59 | - */ |
|
| 60 | - private function validateService($serviceName, $service) |
|
| 61 | - { |
|
| 62 | - if (false === is_array($service)) { |
|
| 63 | - throw new InvalidConfigFormatException( |
|
| 64 | - self::ERROR_PREFIX . ' key "' . $serviceName . '" must be an associative array' |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if (false === array_key_exists(Fields::CLASS_CONFIG_NAME, $service)) { |
|
| 69 | - throw new InvalidConfigFormatException( |
|
| 70 | - self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" is required' |
|
| 71 | - ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if (false === is_string($service[Fields::CLASS_CONFIG_NAME])) { |
|
| 75 | - throw new InvalidConfigFormatException( |
|
| 76 | - self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" must be a string' |
|
| 77 | - ); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if (true === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $service) && |
|
| 81 | - false === is_array($service[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]) |
|
| 82 | - ) { |
|
| 83 | - throw new InvalidConfigFormatException( |
|
| 84 | - self::ERROR_PREFIX . ' key "' . Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME . |
|
| 85 | - '" must be an array or null' |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - if (true === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service) && |
|
| 90 | - false === is_array($service[Fields::METHOD_CALLS_CONFIG_NAME]) |
|
| 91 | - ) { |
|
| 92 | - throw new InvalidConfigFormatException( |
|
| 93 | - self::ERROR_PREFIX . ' key "' . Fields::METHOD_CALLS_CONFIG_NAME . |
|
| 94 | - '" must be an array or null' |
|
| 95 | - ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service)) { |
|
| 99 | - return; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - foreach ($service[Fields::METHOD_CALLS_CONFIG_NAME] as $methodName => $call) { |
|
| 103 | - $this->validateCalls($methodName, $call); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param $methodName |
|
| 109 | - * @param $call |
|
| 110 | - * @throws InvalidConfigFormatException |
|
| 111 | - */ |
|
| 112 | - private function validateCalls($methodName, $call) |
|
| 113 | - { |
|
| 114 | - if (false === is_array($call)) { |
|
| 115 | - throw new InvalidConfigFormatException( |
|
| 116 | - self::ERROR_PREFIX . ' key "' . $methodName . '" must be be an array' |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - } |
|
| 16 | + private const ERROR_PREFIX = 'The service configuration'; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param array $definitionList |
|
| 20 | + * @throws InvalidConfigFormatException |
|
| 21 | + */ |
|
| 22 | + public function validateDefinitions(array $definitionList) |
|
| 23 | + { |
|
| 24 | + $this->validateRoot($definitionList); |
|
| 25 | + |
|
| 26 | + if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $service) { |
|
| 31 | + $this->validateService($serviceName, $service); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @param array $definitionList |
|
| 37 | + * @throws InvalidConfigFormatException |
|
| 38 | + */ |
|
| 39 | + private function validateRoot(array $definitionList) |
|
| 40 | + { |
|
| 41 | + if (false === array_key_exists(Fields::ROOT_CONFIG_NAME, $definitionList)) { |
|
| 42 | + throw new InvalidConfigFormatException( |
|
| 43 | + self::ERROR_PREFIX . ' need the root config key "' . Fields::ROOT_CONFIG_NAME . '"' |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $field = $definitionList[Fields::ROOT_CONFIG_NAME]; |
|
| 48 | + if (false === is_array($field) && false === is_null($field)) { |
|
| 49 | + throw new InvalidConfigFormatException( |
|
| 50 | + self::ERROR_PREFIX . ' key "' . Fields::ROOT_CONFIG_NAME . '" must be an associative array' |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param $serviceName |
|
| 57 | + * @param $service |
|
| 58 | + * @throws InvalidConfigFormatException |
|
| 59 | + */ |
|
| 60 | + private function validateService($serviceName, $service) |
|
| 61 | + { |
|
| 62 | + if (false === is_array($service)) { |
|
| 63 | + throw new InvalidConfigFormatException( |
|
| 64 | + self::ERROR_PREFIX . ' key "' . $serviceName . '" must be an associative array' |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if (false === array_key_exists(Fields::CLASS_CONFIG_NAME, $service)) { |
|
| 69 | + throw new InvalidConfigFormatException( |
|
| 70 | + self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" is required' |
|
| 71 | + ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if (false === is_string($service[Fields::CLASS_CONFIG_NAME])) { |
|
| 75 | + throw new InvalidConfigFormatException( |
|
| 76 | + self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" must be a string' |
|
| 77 | + ); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if (true === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $service) && |
|
| 81 | + false === is_array($service[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]) |
|
| 82 | + ) { |
|
| 83 | + throw new InvalidConfigFormatException( |
|
| 84 | + self::ERROR_PREFIX . ' key "' . Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME . |
|
| 85 | + '" must be an array or null' |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + if (true === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service) && |
|
| 90 | + false === is_array($service[Fields::METHOD_CALLS_CONFIG_NAME]) |
|
| 91 | + ) { |
|
| 92 | + throw new InvalidConfigFormatException( |
|
| 93 | + self::ERROR_PREFIX . ' key "' . Fields::METHOD_CALLS_CONFIG_NAME . |
|
| 94 | + '" must be an array or null' |
|
| 95 | + ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service)) { |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + foreach ($service[Fields::METHOD_CALLS_CONFIG_NAME] as $methodName => $call) { |
|
| 103 | + $this->validateCalls($methodName, $call); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param $methodName |
|
| 109 | + * @param $call |
|
| 110 | + * @throws InvalidConfigFormatException |
|
| 111 | + */ |
|
| 112 | + private function validateCalls($methodName, $call) |
|
| 113 | + { |
|
| 114 | + if (false === is_array($call)) { |
|
| 115 | + throw new InvalidConfigFormatException( |
|
| 116 | + self::ERROR_PREFIX . ' key "' . $methodName . '" must be be an array' |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | 121 | } |
@@ -14,212 +14,212 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ServiceContainer implements ContainerInterface |
| 16 | 16 | { |
| 17 | - private const SERVICE_PREFIX = '@'; |
|
| 18 | - |
|
| 19 | - /** @var ServiceDefinitionInterface[] */ |
|
| 20 | - private $services; |
|
| 21 | - |
|
| 22 | - /** @var array */ |
|
| 23 | - private $serviceInstances; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * ServiceContainer constructor. |
|
| 27 | - */ |
|
| 28 | - public function __construct() |
|
| 29 | - { |
|
| 30 | - $this->services = []; |
|
| 31 | - $this->serviceInstances = []; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
| 36 | - * @return ServiceContainer |
|
| 37 | - * @throws DuplicatedServiceException |
|
| 38 | - */ |
|
| 39 | - public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
| 40 | - { |
|
| 41 | - if (true === $this->has($serviceDefinition->getServiceName())) { |
|
| 42 | - throw new DuplicatedServiceException( |
|
| 43 | - 'The service "' . $serviceDefinition->getServiceName() . '" is already registered in the container' |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
| 48 | - |
|
| 49 | - return $this; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param ServiceDefinitionInterface[] $definitionList |
|
| 54 | - * @return ServiceContainer |
|
| 55 | - * @throws DuplicatedServiceException |
|
| 56 | - */ |
|
| 57 | - public function loadServices(array $definitionList) |
|
| 58 | - { |
|
| 59 | - foreach ($definitionList as $definition) { |
|
| 60 | - $this->addService($definition); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param InstanceDefinitionInterface $definition |
|
| 68 | - * @return ServiceContainer |
|
| 69 | - * @throws DuplicatedServiceException |
|
| 70 | - */ |
|
| 71 | - public function addServiceInstance(InstanceDefinitionInterface $definition) |
|
| 72 | - { |
|
| 73 | - if (true === $this->has($definition->getServiceName())) { |
|
| 74 | - throw new DuplicatedServiceException( |
|
| 75 | - 'The service "' . $definition->getServiceName() . '" is already registered in the container' |
|
| 76 | - ); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
| 80 | - |
|
| 81 | - return $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $index |
|
| 86 | - * @param bool $new |
|
| 87 | - * @return mixed|null |
|
| 88 | - */ |
|
| 89 | - public function get($index, $new = false) |
|
| 90 | - { |
|
| 91 | - $index = $this->cleanServiceName($index); |
|
| 92 | - |
|
| 93 | - // if service is shared true and already exists we return it |
|
| 94 | - if (isset($this->serviceInstances[$index]) && false === $new) { |
|
| 95 | - return $this->serviceInstances[$index]; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // is service not defined return null |
|
| 99 | - if (!isset($this->services[$index])) { |
|
| 100 | - return null; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - // we create a new instance |
|
| 104 | - $instance = $this->create($this->services[$index]); |
|
| 105 | - |
|
| 106 | - // if shared true we set it in the current instances |
|
| 107 | - if (false === $new) { |
|
| 108 | - $this->serviceInstances[$index] = $instance; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return $instance; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Returns true if the container can return an entry for the given identifier. |
|
| 116 | - * Returns false otherwise. |
|
| 117 | - * |
|
| 118 | - * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
| 119 | - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
| 120 | - * |
|
| 121 | - * @param string $index Identifier of the entry to look for. |
|
| 122 | - * |
|
| 123 | - * @return bool |
|
| 124 | - */ |
|
| 125 | - public function has($index) |
|
| 126 | - { |
|
| 127 | - $index = $this->cleanServiceName($index); |
|
| 128 | - |
|
| 129 | - // if service is shared true and already exists we return it |
|
| 130 | - if (isset($this->serviceInstances[$index])) { |
|
| 131 | - return true; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // is service not defined return null |
|
| 135 | - if (isset($this->services[$index])) { |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
| 144 | - * @return mixed |
|
| 145 | - */ |
|
| 146 | - private function create(ServiceDefinitionInterface $serviceDefinition) |
|
| 147 | - { |
|
| 148 | - // we get the class name in a var for the new later |
|
| 149 | - $newClass = $serviceDefinition->getClass(); |
|
| 150 | - |
|
| 151 | - // we get the construct args |
|
| 152 | - $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
| 153 | - $instance = new $newClass(...$args); |
|
| 154 | - |
|
| 155 | - // we call the injection methods after we instance the object |
|
| 156 | - $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
| 157 | - foreach ($calls as $call => $params) { |
|
| 158 | - call_user_func_array([$instance, $call], $params); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // we create the new instance |
|
| 162 | - return $instance; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * @param array $args |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - private function checkArgsServices(array $args) |
|
| 170 | - { |
|
| 171 | - // we check if any of the construct args are services themself and we create them |
|
| 172 | - foreach ($args as $k => $arg) { |
|
| 173 | - if (false === $this->isServiceName($arg)) { |
|
| 174 | - continue; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (true === $this->has($arg)) { |
|
| 178 | - $args[$k] = $this->get($arg); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $args; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @param array $calls |
|
| 187 | - * @return array |
|
| 188 | - */ |
|
| 189 | - private function checkCallsServices(array $calls) |
|
| 190 | - { |
|
| 191 | - // we check if any of the calls args are services themself and we create them |
|
| 192 | - foreach ($calls as $i => $callArgs) { |
|
| 193 | - foreach ($callArgs as $k => $arg) { |
|
| 194 | - if (false === $this->isServiceName($arg)) { |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - if (true === $this->has($arg)) { |
|
| 199 | - $calls[$i][$k] = $this->get($arg); |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return $calls; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @param string $str |
|
| 209 | - * @return bool |
|
| 210 | - */ |
|
| 211 | - private function isServiceName(string $str) |
|
| 212 | - { |
|
| 213 | - return self::SERVICE_PREFIX === substr($str, 0, 1); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param string $str |
|
| 218 | - * @return string |
|
| 219 | - */ |
|
| 220 | - private function cleanServiceName(string $str) |
|
| 221 | - { |
|
| 222 | - return trim($str, self::SERVICE_PREFIX); |
|
| 223 | - } |
|
| 17 | + private const SERVICE_PREFIX = '@'; |
|
| 18 | + |
|
| 19 | + /** @var ServiceDefinitionInterface[] */ |
|
| 20 | + private $services; |
|
| 21 | + |
|
| 22 | + /** @var array */ |
|
| 23 | + private $serviceInstances; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * ServiceContainer constructor. |
|
| 27 | + */ |
|
| 28 | + public function __construct() |
|
| 29 | + { |
|
| 30 | + $this->services = []; |
|
| 31 | + $this->serviceInstances = []; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
| 36 | + * @return ServiceContainer |
|
| 37 | + * @throws DuplicatedServiceException |
|
| 38 | + */ |
|
| 39 | + public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
| 40 | + { |
|
| 41 | + if (true === $this->has($serviceDefinition->getServiceName())) { |
|
| 42 | + throw new DuplicatedServiceException( |
|
| 43 | + 'The service "' . $serviceDefinition->getServiceName() . '" is already registered in the container' |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
| 48 | + |
|
| 49 | + return $this; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param ServiceDefinitionInterface[] $definitionList |
|
| 54 | + * @return ServiceContainer |
|
| 55 | + * @throws DuplicatedServiceException |
|
| 56 | + */ |
|
| 57 | + public function loadServices(array $definitionList) |
|
| 58 | + { |
|
| 59 | + foreach ($definitionList as $definition) { |
|
| 60 | + $this->addService($definition); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param InstanceDefinitionInterface $definition |
|
| 68 | + * @return ServiceContainer |
|
| 69 | + * @throws DuplicatedServiceException |
|
| 70 | + */ |
|
| 71 | + public function addServiceInstance(InstanceDefinitionInterface $definition) |
|
| 72 | + { |
|
| 73 | + if (true === $this->has($definition->getServiceName())) { |
|
| 74 | + throw new DuplicatedServiceException( |
|
| 75 | + 'The service "' . $definition->getServiceName() . '" is already registered in the container' |
|
| 76 | + ); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
| 80 | + |
|
| 81 | + return $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $index |
|
| 86 | + * @param bool $new |
|
| 87 | + * @return mixed|null |
|
| 88 | + */ |
|
| 89 | + public function get($index, $new = false) |
|
| 90 | + { |
|
| 91 | + $index = $this->cleanServiceName($index); |
|
| 92 | + |
|
| 93 | + // if service is shared true and already exists we return it |
|
| 94 | + if (isset($this->serviceInstances[$index]) && false === $new) { |
|
| 95 | + return $this->serviceInstances[$index]; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // is service not defined return null |
|
| 99 | + if (!isset($this->services[$index])) { |
|
| 100 | + return null; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + // we create a new instance |
|
| 104 | + $instance = $this->create($this->services[$index]); |
|
| 105 | + |
|
| 106 | + // if shared true we set it in the current instances |
|
| 107 | + if (false === $new) { |
|
| 108 | + $this->serviceInstances[$index] = $instance; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return $instance; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Returns true if the container can return an entry for the given identifier. |
|
| 116 | + * Returns false otherwise. |
|
| 117 | + * |
|
| 118 | + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
| 119 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
| 120 | + * |
|
| 121 | + * @param string $index Identifier of the entry to look for. |
|
| 122 | + * |
|
| 123 | + * @return bool |
|
| 124 | + */ |
|
| 125 | + public function has($index) |
|
| 126 | + { |
|
| 127 | + $index = $this->cleanServiceName($index); |
|
| 128 | + |
|
| 129 | + // if service is shared true and already exists we return it |
|
| 130 | + if (isset($this->serviceInstances[$index])) { |
|
| 131 | + return true; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // is service not defined return null |
|
| 135 | + if (isset($this->services[$index])) { |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
| 144 | + * @return mixed |
|
| 145 | + */ |
|
| 146 | + private function create(ServiceDefinitionInterface $serviceDefinition) |
|
| 147 | + { |
|
| 148 | + // we get the class name in a var for the new later |
|
| 149 | + $newClass = $serviceDefinition->getClass(); |
|
| 150 | + |
|
| 151 | + // we get the construct args |
|
| 152 | + $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
| 153 | + $instance = new $newClass(...$args); |
|
| 154 | + |
|
| 155 | + // we call the injection methods after we instance the object |
|
| 156 | + $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
| 157 | + foreach ($calls as $call => $params) { |
|
| 158 | + call_user_func_array([$instance, $call], $params); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // we create the new instance |
|
| 162 | + return $instance; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * @param array $args |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + private function checkArgsServices(array $args) |
|
| 170 | + { |
|
| 171 | + // we check if any of the construct args are services themself and we create them |
|
| 172 | + foreach ($args as $k => $arg) { |
|
| 173 | + if (false === $this->isServiceName($arg)) { |
|
| 174 | + continue; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (true === $this->has($arg)) { |
|
| 178 | + $args[$k] = $this->get($arg); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $args; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @param array $calls |
|
| 187 | + * @return array |
|
| 188 | + */ |
|
| 189 | + private function checkCallsServices(array $calls) |
|
| 190 | + { |
|
| 191 | + // we check if any of the calls args are services themself and we create them |
|
| 192 | + foreach ($calls as $i => $callArgs) { |
|
| 193 | + foreach ($callArgs as $k => $arg) { |
|
| 194 | + if (false === $this->isServiceName($arg)) { |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + if (true === $this->has($arg)) { |
|
| 199 | + $calls[$i][$k] = $this->get($arg); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return $calls; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @param string $str |
|
| 209 | + * @return bool |
|
| 210 | + */ |
|
| 211 | + private function isServiceName(string $str) |
|
| 212 | + { |
|
| 213 | + return self::SERVICE_PREFIX === substr($str, 0, 1); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param string $str |
|
| 218 | + * @return string |
|
| 219 | + */ |
|
| 220 | + private function cleanServiceName(string $str) |
|
| 221 | + { |
|
| 222 | + return trim($str, self::SERVICE_PREFIX); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | 225 | } |
@@ -9,28 +9,28 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class InstanceDefinition implements InstanceDefinitionInterface |
| 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,10 +10,10 @@ |
||
| 10 | 10 | interface InstanceDefinitionInterface |
| 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 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if ( !is_readable( __DIR__ . '/../../vendor/autoload.php' ) ) { |
| 4 | - die( 'You need to install this package with Composer before you can run the examples' ); |
|
| 4 | + die( 'You need to install this package with Composer before you can run the examples' ); |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | require_once __DIR__ . '/../../vendor/autoload.php'; |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if ( !is_readable( __DIR__ . '/../../vendor/autoload.php' ) ) { |
|
| 4 | - die( 'You need to install this package with Composer before you can run the examples' ); |
|
| 3 | +if (!is_readable(__DIR__ . '/../../vendor/autoload.php')) { |
|
| 4 | + die('You need to install this package with Composer before you can run the examples'); |
|
| 5 | 5 | } |
| 6 | 6 | |
| 7 | 7 | require_once __DIR__ . '/../../vendor/autoload.php'; |
@@ -9,18 +9,18 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class SampleClass |
| 11 | 11 | { |
| 12 | - private $first; |
|
| 13 | - private $second; |
|
| 12 | + private $first; |
|
| 13 | + private $second; |
|
| 14 | 14 | |
| 15 | - public function __construct($param1, $param2) |
|
| 16 | - { |
|
| 17 | - $this->first = $param1; |
|
| 18 | - $this->second = $param2; |
|
| 19 | - } |
|
| 15 | + public function __construct($param1, $param2) |
|
| 16 | + { |
|
| 17 | + $this->first = $param1; |
|
| 18 | + $this->second = $param2; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function callMethod($param1, $param2) |
|
| 22 | - { |
|
| 23 | - $this->first = $param1; |
|
| 24 | - $this->second = $param2; |
|
| 25 | - } |
|
| 21 | + public function callMethod($param1, $param2) |
|
| 22 | + { |
|
| 23 | + $this->first = $param1; |
|
| 24 | + $this->second = $param2; |
|
| 25 | + } |
|
| 26 | 26 | } |