PluginProbe
Bookit — Booking & Appointment Calendar / trunk
Bookit — Booking & Appointment Calendar vtrunk
2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 All 61 releases
bookit / src / Bookit / Container.php

Container.php in Bookit — Booking & Appointment Calendar trunk, at src/Bookit/Container.php

344 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Bookit\Contracts\Container;
4
5 if ( ! class_exists( 'Bookit__Container' ) ) {
6 /**
7 * Class Bookit__Container
8 *
9 * Bookit Dependency Injection Container.
10 */
11 class Bookit__Container extends Container {
12
13 /**
14 * @var Bookit__Container
15 */
16 protected static $instance;
17
18 /**
19 * @return Bookit__Container
20 */
21 public static function init() {
22 if ( empty( self::$instance ) ) {
23 self::$instance = new self();
24 }
25
26 return self::$instance;
27 }
28 }
29 }
30
31 if ( ! function_exists( 'bookit_singleton' ) ) {
32 /**
33 * Registers a class as a singleton.
34 *
35 * Each call to obtain an instance of this class made using the `bookit( $slug )` function
36 * will return the same instance; the instances are built just in time (if not passing an
37 * object instance or callback function) and on the first request.
38 * The container will call the class `__construct` method on the class (if not passing an object
39 * or a callback function) and will try to automagically resolve dependencies.
40 *
41 * Example use:
42 *
43 * bookit_singleton( 'tec.admin.class', 'Bookit__Admin__Class' );
44 *
45 * // some code later...
46 *
47 * // class is built here
48 * bookit( 'tec.admin.class' )->doSomething();
49 *
50 * Need the class built immediately? Build it and register it:
51 *
52 * bookit_singleton( 'tec.admin.class', new Bookit__Admin__Class() );
53 *
54 * // some code later...
55 *
56 * bookit( 'tec.admin.class' )->doSomething();
57 *
58 * Need a very custom way to build the class? Register a callback:
59 *
60 * bookit_singleton( 'tec.admin.class', array( Bookit__Admin__Class__Factory, 'make' ) );
61 *
62 * // some code later...
63 *
64 * bookit( 'tec.admin.class' )->doSomething();
65 *
66 * Or register the methods that should be called on the object after its construction:
67 *
68 * bookit_singleton( 'tec.admin.class', 'Bookit__Admin__Class', array( 'hook', 'register' ) );
69 *
70 * // some code later...
71 *
72 * // the `hook` and `register` methods will be called on the built instance.
73 * bookit( 'tec.admin.class' )->doSomething();
74 *
75 * The class will be built only once (if passing the class name or a callback function), stored
76 * and the same instance will be returned from that moment on.
77 *
78 * @param string $slug The human-readable and catchy name of the class.
79 * @param string|object|callable $class The full class name or an instance of the class
80 * or a callback that will return the instance of the class.
81 * @param array $after_build_methods An array of methods that should be called on
82 * the built object after the `__construct` method; the methods
83 * will be called only once after the singleton instance
84 * construction.
85 */
86 function bookit_singleton( $slug, $class, array $after_build_methods = null ) {
87 Bookit__Container::init()->singleton( $slug, $class, $after_build_methods );
88 }
89 }
90
91 if ( ! function_exists( 'bookit_register' ) ) {
92 /**
93 * Registers a class.
94 *
95 * Each call to obtain an instance of this class made using the `bookit( $slug )` function
96 * will return a new instance; the instances are built just in time (if not passing an
97 * object instance, in that case it will work as a singleton) and on the first request.
98 * The container will call the class `__construct` method on the class (if not passing an object
99 * or a callback function) and will try to automagically resolve dependencies.
100 *
101 * Example use:
102 *
103 * bookit_register( 'tec.some', 'Bookit__Some' );
104 *
105 * // some code later...
106 *
107 * // class is built here
108 * $some_one = bookit( 'tec.some' )->doSomething();
109 *
110 * // $some_two !== $some_one
111 * $some_two = bookit( 'tec.some' )->doSomething();
112 *
113 * Need the class built immediately? Build it and register it:
114 *
115 * bookit_register( 'tec.admin.class', new Bookit__Admin__Class() );
116 *
117 * // some code later...
118 *
119 * // $some_two === $some_one
120 * // acts like a singleton
121 * $some_one = bookit( 'tec.some' )->doSomething();
122 * $some_two = bookit( 'tec.some' )->doSomething();
123 *
124 * Need a very custom way to build the class? Register a callback:
125 *
126 * bookit_register( 'tec.some', array( Bookit__Some__Factory, 'make' ) );
127 *
128 * // some code later...
129 *
130 * // $some_two !== $some_one
131 * $some_one = bookit( 'tec.some' )->doSomething();
132 * $some_two = bookit( 'tec.some' )->doSomething();
133 *
134 * Or register the methods that should be called on the object after its construction:
135 *
136 * bookit_singleton( 'tec.admin.class', 'Bookit__Admin__Class', array( 'hook', 'register' ) );
137 *
138 * // some code later...
139 *
140 * // the `hook` and `register` methods will be called on the built instance.
141 * bookit( 'tec.admin.class' )->doSomething();
142 *
143 * @param string $slug The human-readable and catchy name of the class.
144 * @param string|object|callable $class The full class name or an instance of the class
145 * or a callback that will return the instance of the class.
146 * @param array $after_build_methods An array of methods that should be called on
147 * the built object after the `__construct` method; the methods
148 * will be called each time after the instance construction.
149 */
150 function bookit_register( $slug, $class, array $after_build_methods = null ) {
151 Bookit__Container::init()->bind( $slug, $class, $after_build_methods );
152 }
153 }
154
155 if ( ! function_exists( 'bookit' ) ) {
156 /**
157 * Returns a ready to use instance of the requested class.
158 *
159 * Example use:
160 *
161 * bookit_singleton( 'common.main', 'Bookit__Main');
162 *
163 * // some code later...
164 *
165 * bookit( 'common.main' )->do_something();
166 *
167 * @param string|null $slug_or_class Either the slug of a binding previously registered using `bookit_singleton` or
168 * `bookit_register` or the full class name that should be automagically created or
169 * `null` to get the container instance itself.
170 *
171 * @return mixed|object|Bookit__Container The instance of the requested class. Please note that the cardinality of
172 * the class is controlled registering it as a singleton using `bookit_singleton`
173 * or `bookit_register`; if the `$slug_or_class` parameter is null then the
174 * container itself will be returned.
175 */
176 function bookit( $slug_or_class = null ) {
177 $container = Bookit__Container::init();
178
179 return null === $slug_or_class ? $container : $container->make( $slug_or_class );
180 }
181 }
182
183 if ( ! function_exists( 'bookit_set_var' ) ) {
184 /**
185 * Registers a value under a slug in the container.
186 *
187 * Example use:
188 *
189 * bookit_set_var( 'tec.url', 'http://example.com' );
190 *
191 * @param string $slug The human-readable and catchy name of the var.
192 * @param mixed $value The variable value.
193 */
194 function bookit_set_var( $slug, $value ) {
195 $container = Bookit__Container::init();
196 $container->setVar( $slug, $value );
197 }
198 }
199
200 if ( ! function_exists( 'bookit_get_var' ) ) {
201 /**
202 * Returns the value of a registered variable.
203 *
204 * Example use:
205 *
206 * bookit_set_var( 'tec.url', 'http://example.com' );
207 *
208 * $url = bookit_get_var( 'tec.url' );
209 *
210 * @param string $slug The slug of the variable registered using `bookit_set_var`.
211 * @param null $default The value that should be returned if the variable slug
212 * is not a registered one.
213 *
214 * @return mixed Either the registered value or the default value if the variable
215 * is not registered.
216 */
217 function bookit_get_var( $slug, $default = null ) {
218 $container = Bookit__Container::init();
219
220 try {
221 $var = $container->getVar( $slug );
222 } catch ( InvalidArgumentException $e ) {
223 return $default;
224 }
225
226 return $var;
227 }
228 }
229
230 if ( ! function_exists( 'bookit_unset_var' ) ) {
231 /**
232 * Returns the value of a registered variable.
233 *
234 * Example use:
235 *
236 * bookit_set_var( 'tec.url', 'http://example.com' );
237 *
238 * bookit_unset_var( 'tec.url' );
239 *
240 * @since 4.11.0
241 *
242 * @param string $slug The slug of the variable registered using `bookit_unset_var`.
243 *
244 * @return void
245 */
246 function bookit_unset_var( $slug ) {
247 $container = Bookit__Container::init();
248 try {
249 $container->offsetUnset( $slug );
250 } catch ( Exception $e ) {}
251 }
252 }
253
254 if ( ! function_exists( 'bookit_isset_var' ) ) {
255 /**
256 * Returns the value of a registered variable.
257 *
258 * Example use:
259 *
260 * bookit_set_var( 'tec.url', 'http://example.com' );
261 *
262 * bookit_isset_var( 'tec.url' );
263 *
264 * @since 4.11.0
265 *
266 * @param string $slug The slug of the variable checked using `bookit_isset_var`.
267 *
268 * @return boolean Either a the given slug exists.
269 */
270 function bookit_isset_var( $slug ) {
271 $container = Bookit__Container::init();
272 return $container->offsetExists( $slug );
273 }
274 }
275
276 if ( ! function_exists( 'bookit_register_provider' ) ) {
277 /**
278 * Registers a service provider in the container.
279 *
280 * Service providers must implement the `use ServiceProviderInterface` interface or extend
281 * the `ServiceProvider` class.
282 *
283 * @see ServiceProvider
284 * @see ServiceProviderInterface
285 *
286 * @param string $provider_class
287 */
288 function bookit_register_provider( $provider_class ) {
289 $container = Bookit__Container::init();
290
291 $container->register( $provider_class );
292 }
293
294 if ( ! function_exists( 'bookit_callback' ) ) {
295 /**
296 * Returns a lambda function suitable to use as a callback; when called the function will build the implementation
297 * bound to `$classOrInterface` and return the value of a call to `$method` method with the call arguments.
298 *
299 * @since 4.7
300 * @since 4.6.2 Included the $argsN params
301 *
302 * @param string $slug A class or interface fully qualified name or a string slug.
303 * @param string $method The method that should be called on the resolved implementation with the
304 * specified array arguments.
305 * @param mixed [$argsN] (optional) Any number of arguments that will be passed down to the Callback
306 *
307 * @return callable A PHP Callable based on the Slug and Methods passed
308 */
309 function bookit_callback( $slug, $method ) {
310 $container = Bookit__Container::init();
311 $arguments = func_get_args();
312 $is_empty = 2 === count( $arguments );
313
314 if ( $is_empty ) {
315 $callable = $container->callback( $slug, $method );
316 } else {
317 $callback = $container->callback( 'callback', 'get' );
318 $callable = call_user_func_array( $callback, $arguments );
319 }
320
321 return $callable;
322 }
323 }
324
325 if ( ! function_exists( 'bookit_callback_return' ) ) {
326 /**
327 * Returns a bookit_callback for a very simple Return value method
328 *
329 * Example of Usage:
330 *
331 * add_filter( 'admin_title', bookit_callback_return( __( 'Ready to work.' ) ) );
332 *
333 * @since 4.6.2
334 *
335 * @param mixed $value The value to be returned
336 *
337 * @return callable A PHP Callable based on the Slug and Methods passed
338 */
339 function bookit_callback_return( $value ) {
340 return bookit_callback( 'callback', 'return_value', $value );
341 }
342 }
343 }
344