Container_DI52.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\Container; |
| 4 | |
| 5 | // Don't load directly. |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | die( '-1' ); |
| 8 | } |
| 9 | |
| 10 | use Pods\Prefixed\lucatume\DI52\Container as DI52Container; |
| 11 | |
| 12 | /** |
| 13 | * Describes the interface of a container that exposes methods to read its entries. |
| 14 | * |
| 15 | * @credit The StellarWP team - https://github.com/stellarwp/container-contract |
| 16 | * |
| 17 | * @since 3.0 |
| 18 | * |
| 19 | * @method mixed getVar( string $key, mixed|null $default = null ) |
| 20 | * @method void register( string $serviceProviderClass, string ...$alias ) |
| 21 | * @method self when( string $class ) |
| 22 | * @method self needs( string $id ) |
| 23 | * @method void give( mixed $implementation ) |
| 24 | */ |
| 25 | class Container_DI52 implements Container_Interface { |
| 26 | |
| 27 | /** |
| 28 | * @var self |
| 29 | */ |
| 30 | protected static $instance; |
| 31 | |
| 32 | /** |
| 33 | * @var DI52Container |
| 34 | */ |
| 35 | protected $container; |
| 36 | |
| 37 | /** |
| 38 | * Container constructor. |
| 39 | * |
| 40 | * @param object $container The container to use. |
| 41 | */ |
| 42 | public function __construct( $container = null ) { |
| 43 | $this->container = $container ?: new DI52Container(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return self |
| 48 | */ |
| 49 | public static function init() { |
| 50 | if ( empty( self::$instance ) ) { |
| 51 | self::$instance = new self(); |
| 52 | } |
| 53 | |
| 54 | return self::$instance; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @inheritDoc |
| 59 | */ |
| 60 | public function bind( string $id, $implementation = null, ?array $afterBuildMethods = null ) { |
| 61 | $this->container->bind( $id, $implementation, $afterBuildMethods ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritDoc |
| 66 | */ |
| 67 | public function get( string $id ) { |
| 68 | return $this->container->get( $id ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @return DI52Container |
| 73 | */ |
| 74 | public function get_container() { |
| 75 | return $this->container; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @inheritDoc |
| 80 | */ |
| 81 | public function has( string $id ) { |
| 82 | return $this->container->has( $id ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @inheritDoc |
| 87 | */ |
| 88 | public function singleton( string $id, $implementation = null, ?array $afterBuildMethods = null ) { |
| 89 | $this->container->singleton( $id, $implementation, $afterBuildMethods ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Defer all other calls to the container object. |
| 94 | */ |
| 95 | #[\ReturnTypeWillChange] |
| 96 | public function __call( $name, $arguments ) { |
| 97 | return $this->container->{$name}( ...$arguments ); |
| 98 | } |
| 99 | } |
| 100 |