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