| @@ -1,139 +1,143 @@ | ||
| 1 | -<?php | |
| 2 | -namespace Booktics\Container; | |
| 3 | - | |
| 4 | -use Booktics\Container\Exception\Dependency_Has_No_Default_Value_Exception; | |
| 5 | -use Booktics\Container\Exception\Dependency_Is_Not_Instantiable_Exception; | |
| 6 | -use Exception; | |
| 7 | -use ReflectionClass; | |
| 8 | -use ReflectionException; | |
| 9 | -use ReflectionParameter; | |
| 10 | - | |
| 11 | -class Container { | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * Store all services | |
| 15 | - * | |
| 16 | - * @var array | |
| 17 | - */ | |
| 18 | - protected $services = array(); | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Finds an entry of the container by its identifier and returns it. | |
| 22 | - * | |
| 23 | - * @param string $id Identifier of the entry to look for. | |
| 24 | - * | |
| 25 | - * @return mixed Entry. | |
| 26 | - * @throws Container_Exception_Interface|ReflectionException Error while retrieving the entry. | |
| 27 | - * | |
| 28 | - * @throws Not_Found_Exception_Interface No entry was found for **this** identifier. | |
| 29 | - */ | |
| 30 | - public function get( string $key ): object { | |
| 31 | - if ( ! $this->has( $key ) ) { | |
| 32 | - // translators: %s is the name of the class that is not instantiable. | |
| 33 | - throw new \Exception( sprintf( esc_html__( 'Service provider %s not found.', 'booktics' ), esc_html( $key ) ) ); | |
| 34 | - } | |
| 35 | - | |
| 36 | - $class_name = $this->services[ $key ]; | |
| 37 | - | |
| 38 | - return $this->resolve( $class_name ); | |
| 39 | - } | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Sets an entry of the container by its identifier | |
| 43 | - * | |
| 44 | - * @param string $id | |
| 45 | - * @param string $class_name | |
| 46 | - * | |
| 47 | - * @return void | |
| 48 | - */ | |
| 49 | - public function addServiceProvider( string $key, $class_name ): void { | |
| 50 | - $this->services[ $key ] = $class_name; | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * Returns true if the container can return an entry for the given identifier. | |
| 55 | - * Returns false otherwise. | |
| 56 | - * | |
| 57 | - * `has($key)` returning true does not mean that `get($id)` will not throw an exception. | |
| 58 | - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | |
| 59 | - * | |
| 60 | - * @param string $key Identifier of the entry to look for. | |
| 61 | - * | |
| 62 | - * @return bool | |
| 63 | - */ | |
| 64 | - public function has( string $key ): bool { | |
| 65 | - return array_key_exists( $key, $this->services ); | |
| 66 | - } | |
| 67 | - | |
| 68 | - | |
| 69 | - /** | |
| 70 | - * Resolves the service object by its name. | |
| 71 | - * | |
| 72 | - * @param string $class_name | |
| 73 | - * | |
| 74 | - * @return object | |
| 75 | - * @throws Dependency_Is_Not_Instantiable_Exception | |
| 76 | - * @throws ReflectionException|Exception | |
| 77 | - * | |
| 78 | - * @throws Dependency_Has_No_Default_Value_Exception | |
| 79 | - */ | |
| 80 | - public function resolve( string $class_name ): object { | |
| 81 | - if ( ! class_exists( $class_name ) ) { | |
| 82 | - // translators: %s is the name of the class that is not instantiable. | |
| 83 | - throw new Exception( sprintf( esc_html__( 'Class: %s does not exist.', 'booktics' ), esc_html( $class_name ) ) ); | |
| 84 | - } | |
| 85 | - | |
| 86 | - $reflection_class = new ReflectionClass( $class_name ); | |
| 87 | - | |
| 88 | - if ( ! $reflection_class->isInstantiable() ) { | |
| 89 | - throw new Dependency_Is_Not_Instantiable_Exception( | |
| 90 | - // translators: %s is the name of the class that is not instantiable. | |
| 91 | - sprintf( esc_html__( 'Class: %s is not instantiable.', 'booktics' ), esc_html( $class_name ) ) | |
| 92 | - ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - if ( null === $reflection_class->getConstructor() ) { | |
| 96 | - return $reflection_class->newInstance(); | |
| 97 | - } | |
| 98 | - | |
| 99 | - $parameters = $reflection_class->getConstructor()->getParameters(); | |
| 100 | - | |
| 101 | - $dependencies = $this->build_dependencies( $parameters ); | |
| 102 | - | |
| 103 | - return $reflection_class->newInstanceArgs( $dependencies ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * Builds the dependencies for the given parameters. | |
| 109 | - * | |
| 110 | - * @param ReflectionParameter[] $parameters | |
| 111 | - * | |
| 112 | - * @return array | |
| 113 | - * @throws Dependency_Has_No_Default_Value_Exception | |
| 114 | - * | |
| 115 | - * @throws ReflectionException | |
| 116 | - */ | |
| 117 | - public function build_dependencies( array $parameters ): array { | |
| 118 | - $dependencies = array(); | |
| 119 | - | |
| 120 | - foreach ( $parameters as $parameter ) { | |
| 121 | - $dependency = $parameter->getType() ? $parameter->getType()->getName() : null; | |
| 122 | - | |
| 123 | - if ( is_null( $dependency ) ) { | |
| 124 | - if ( $parameter->isDefaultValueAvailable() ) { | |
| 125 | - $dependencies[] = $parameter->getDefaultValue(); | |
| 126 | - } else { | |
| 127 | - throw new Dependency_Has_No_Default_Value_Exception( | |
| 128 | - // translators: %s is the name of the class that is not instantiable. | |
| 129 | - sprintf( esc_html__( 'Class: %s dependency cannot be resolved.', 'booktics' ), esc_html( $parameter->name ) ) | |
| 130 | - ); | |
| 131 | - } | |
| 132 | - } else { | |
| 133 | - $dependencies[] = $this->get( $dependency ); | |
| 134 | - } | |
| 135 | - } | |
| 136 | - | |
| 137 | - return $dependencies; | |
| 138 | - } | |
| 139 | -} | |
| 1 | +<?php | |
| 2 | +namespace Booktics\Container; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use Booktics\Container\Exception\Dependency_Has_No_Default_Value_Exception; | |
| 9 | +use Booktics\Container\Exception\Dependency_Is_Not_Instantiable_Exception; | |
| 10 | +use Exception; | |
| 11 | +use ReflectionClass; | |
| 12 | +use ReflectionException; | |
| 13 | +use ReflectionParameter; | |
| 14 | + | |
| 15 | +class Container { | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Store all services | |
| 19 | + * | |
| 20 | + * @var array | |
| 21 | + */ | |
| 22 | + protected $services = array(); | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Finds an entry of the container by its identifier and returns it. | |
| 26 | + * | |
| 27 | + * @param string $id Identifier of the entry to look for. | |
| 28 | + * | |
| 29 | + * @return mixed Entry. | |
| 30 | + * @throws Container_Exception_Interface|ReflectionException Error while retrieving the entry. | |
| 31 | + * | |
| 32 | + * @throws Not_Found_Exception_Interface No entry was found for **this** identifier. | |
| 33 | + */ | |
| 34 | + public function get( string $key ): object { | |
| 35 | + if ( ! $this->has( $key ) ) { | |
| 36 | + // translators: %s is the name of the class that is not instantiable. | |
| 37 | + throw new \Exception( sprintf( esc_html__( 'Service provider %s not found.', 'booktics' ), esc_html( $key ) ) ); | |
| 38 | + } | |
| 39 | + | |
| 40 | + $class_name = $this->services[ $key ]; | |
| 41 | + | |
| 42 | + return $this->resolve( $class_name ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Sets an entry of the container by its identifier | |
| 47 | + * | |
| 48 | + * @param string $id | |
| 49 | + * @param string $class_name | |
| 50 | + * | |
| 51 | + * @return void | |
| 52 | + */ | |
| 53 | + public function addServiceProvider( string $key, $class_name ): void { | |
| 54 | + $this->services[ $key ] = $class_name; | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Returns true if the container can return an entry for the given identifier. | |
| 59 | + * Returns false otherwise. | |
| 60 | + * | |
| 61 | + * `has($key)` returning true does not mean that `get($id)` will not throw an exception. | |
| 62 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. | |
| 63 | + * | |
| 64 | + * @param string $key Identifier of the entry to look for. | |
| 65 | + * | |
| 66 | + * @return bool | |
| 67 | + */ | |
| 68 | + public function has( string $key ): bool { | |
| 69 | + return array_key_exists( $key, $this->services ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Resolves the service object by its name. | |
| 75 | + * | |
| 76 | + * @param string $class_name | |
| 77 | + * | |
| 78 | + * @return object | |
| 79 | + * @throws Dependency_Is_Not_Instantiable_Exception | |
| 80 | + * @throws ReflectionException|Exception | |
| 81 | + * | |
| 82 | + * @throws Dependency_Has_No_Default_Value_Exception | |
| 83 | + */ | |
| 84 | + public function resolve( string $class_name ): object { | |
| 85 | + if ( ! class_exists( $class_name ) ) { | |
| 86 | + // translators: %s is the name of the class that is not instantiable. | |
| 87 | + throw new Exception( sprintf( esc_html__( 'Class: %s does not exist.', 'booktics' ), esc_html( $class_name ) ) ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + $reflection_class = new ReflectionClass( $class_name ); | |
| 91 | + | |
| 92 | + if ( ! $reflection_class->isInstantiable() ) { | |
| 93 | + throw new Dependency_Is_Not_Instantiable_Exception( | |
| 94 | + // translators: %s is the name of the class that is not instantiable. | |
| 95 | + sprintf( esc_html__( 'Class: %s is not instantiable.', 'booktics' ), esc_html( $class_name ) ) | |
| 96 | + ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + if ( null === $reflection_class->getConstructor() ) { | |
| 100 | + return $reflection_class->newInstance(); | |
| 101 | + } | |
| 102 | + | |
| 103 | + $parameters = $reflection_class->getConstructor()->getParameters(); | |
| 104 | + | |
| 105 | + $dependencies = $this->build_dependencies( $parameters ); | |
| 106 | + | |
| 107 | + return $reflection_class->newInstanceArgs( $dependencies ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Builds the dependencies for the given parameters. | |
| 113 | + * | |
| 114 | + * @param ReflectionParameter[] $parameters | |
| 115 | + * | |
| 116 | + * @return array | |
| 117 | + * @throws Dependency_Has_No_Default_Value_Exception | |
| 118 | + * | |
| 119 | + * @throws ReflectionException | |
| 120 | + */ | |
| 121 | + public function build_dependencies( array $parameters ): array { | |
| 122 | + $dependencies = array(); | |
| 123 | + | |
| 124 | + foreach ( $parameters as $parameter ) { | |
| 125 | + $dependency = $parameter->getType() ? $parameter->getType()->getName() : null; | |
| 126 | + | |
| 127 | + if ( is_null( $dependency ) ) { | |
| 128 | + if ( $parameter->isDefaultValueAvailable() ) { | |
| 129 | + $dependencies[] = $parameter->getDefaultValue(); | |
| 130 | + } else { | |
| 131 | + throw new Dependency_Has_No_Default_Value_Exception( | |
| 132 | + // translators: %s is the name of the class that is not instantiable. | |
| 133 | + sprintf( esc_html__( 'Class: %s dependency cannot be resolved.', 'booktics' ), esc_html( $parameter->name ) ) | |
| 134 | + ); | |
| 135 | + } | |
| 136 | + } else { | |
| 137 | + $dependencies[] = $this->get( $dependency ); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + return $dependencies; | |
| 142 | + } | |
| 143 | +} | |