| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Contracts; |
| 4 |
|
| 5 |
use Bookit\Vendor\StellarWP\ContainerContract\ContainerInterface; |
| 6 |
use Bookit\Exceptions\Not_Bound_Exception; |
| 7 |
use Bookit\Vendor\lucatume\DI52\Container as DI52_Container; |
| 8 |
|
| 9 |
/** |
| 10 |
* Container |
| 11 |
* |
| 12 |
* @since 2.5.0 |
| 13 |
* |
| 14 |
* @package Bookit\Contracts |
| 15 |
*/ |
| 16 |
class Container extends DI52_Container implements ContainerInterface { |
| 17 |
/** |
| 18 |
* Finds an entry of the container by its identifier and returns it. |
| 19 |
* |
| 20 |
* @since 2.5.0 |
| 21 |
* |
| 22 |
* @param string $id A fully qualified class or interface name or an already built object. |
| 23 |
* |
| 24 |
* @return mixed The entry for an id. |
| 25 |
* @throws Not_Bound_Exception Error while retrieving the entry. |
| 26 |
* |
| 27 |
*/ |
| 28 |
public function get( $id ) { |
| 29 |
try { |
| 30 |
return parent::get( $id ); |
| 31 |
} catch ( \Exception $e ) { |
| 32 |
// Do not chain the previous exception into ours, as it makes the error log confusing. |
| 33 |
throw new Not_Bound_Exception( esc_html( $e->getMessage() ), esc_html( $e->getCode() ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Overrides the parent method to fire an action when a service provider is registered. |
| 39 |
* |
| 40 |
* @since 2.5.0 |
| 41 |
* |
| 42 |
* @param string $serviceProviderClass The service provider class name. |
| 43 |
* @param string ...$alias Optional. The alias(es) to register the service provider with. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
* |
| 47 |
* @throws \Bookit\Vendor\lucatume\DI52\ContainerException If the provider class is marked as deferred but |
| 48 |
* does not provide a set of deferred registrations. |
| 49 |
*/ |
| 50 |
public function register( $serviceProviderClass, ...$alias ) { |
| 51 |
// Register the provider with the parent container. |
| 52 |
parent::register( $serviceProviderClass, ...$alias ); |
| 53 |
|
| 54 |
/** |
| 55 |
* Fires when a service provider is registered by the container. |
| 56 |
* |
| 57 |
* @since 2.5.0 |
| 58 |
* |
| 59 |
* @param string $serviceProviderClass The service provider class name. |
| 60 |
* @param array<string> $alias The alias(es) the service provider was registered with. |
| 61 |
*/ |
| 62 |
do_action( 'bookit_container_registered_provider', $serviceProviderClass, $alias ); |
| 63 |
|
| 64 |
/** |
| 65 |
* Fires a class-specific action when a service provider is registered by the container. |
| 66 |
* |
| 67 |
* @since 2.5.0 |
| 68 |
* |
| 69 |
* @param array<string> $alias The alias(es) the service provider was registered with. |
| 70 |
*/ |
| 71 |
do_action( 'bookit_container_registered_provider_' . $serviceProviderClass, $alias ); |
| 72 |
|
| 73 |
if ( |
| 74 |
// Back compat with older definition of Service Provider. |
| 75 |
! property_exists( $serviceProviderClass, 'registration_action' ) |
| 76 |
// New definition of Service Provider: default action is empty. |
| 77 |
|| empty( $serviceProviderClass::$registration_action ) |
| 78 |
) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Fires a custom action defined by the Service Provider when it's registered. |
| 84 |
* |
| 85 |
* @since 2.5.0 |
| 86 |
*/ |
| 87 |
do_action( $serviceProviderClass::$registration_action, $serviceProviderClass, $alias ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Registers a service provider on a given action is dispatched. |
| 92 |
* |
| 93 |
* @since 2.5.0 |
| 94 |
* |
| 95 |
* @param string $action The action to register the provider on. |
| 96 |
* @param string $class The service provider class name. |
| 97 |
* @param string ...$alias Optional. The alias(es) to register the service provider with. |
| 98 |
* |
| 99 |
* @return void The Service Provider is registered when the action fires, |
| 100 |
* or immediately if the action has already fired. |
| 101 |
* |
| 102 |
* @throws \Bookit\Vendor\lucatume\DI52\ContainerException If the provider class is marked as deferred but |
| 103 |
* does not provide a set of deferred registrations. |
| 104 |
*/ |
| 105 |
public function register_on_action( string $action, string $class, string ...$alias ): void { |
| 106 |
if ( did_action( $action ) ) { |
| 107 |
// If the action has already fired, register the provider immediately. |
| 108 |
$this->register( $class, ...$alias ); |
| 109 |
|
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// If the action has not fired yet, register the provider when it does. |
| 114 |
$registration_closure = function () use ( $action, $class, $alias, &$registration_closure ) { |
| 115 |
// Remove the closure from the action to avoid calling it again. |
| 116 |
remove_action( $action, $registration_closure ); |
| 117 |
$this->register( $class, ...$alias ); |
| 118 |
}; |
| 119 |
add_action( $action, $registration_closure ); |
| 120 |
} |
| 121 |
} |
| 122 |
|