| 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 |
} |
| 140 |
|