PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.21
Booktics – Appointment Booking Calendar for Service Businesses v1.0.21
1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 All 27 releases
booktics / base / container / container.php

container.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.21, at base/container/container.php

144 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
144