Container.php
1 year ago
FeatureProviderInterface.php
4 years ago
FeatureServiceProvider.php
4 years ago
Resolver.php
1 year ago
ServiceProvider.php
3 years ago
Container.php
184 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\DI; |
| 4 | |
| 5 | use WPStaging\Framework\Interfaces\ShutdownableInterface; |
| 6 | use WPStaging\Framework\DI\Resolver; |
| 7 | use WPStaging\Vendor\lucatume\DI52\Builders\Factory; |
| 8 | use WPStaging\Vendor\Psr\Container\ContainerInterface; |
| 9 | use WPStaging\Vendor\lucatume\DI52\Container as BaseContainer; |
| 10 | |
| 11 | class Container extends BaseContainer |
| 12 | { |
| 13 | /** |
| 14 | * @var string The PSR-4 namespace prefix we use to isolate third-party dependencies. |
| 15 | */ |
| 16 | protected $prefix = 'WPStaging\\Vendor\\'; |
| 17 | |
| 18 | /** |
| 19 | * Somehow the singleton version of this child container is not working on unit tests with 3.3.5 version of DI52 |
| 20 | * so we have to use the parent container to make it work for unit tests. |
| 21 | * @param bool $resolveUnboundAsSingletons |
| 22 | * @param bool $useBaseContainer |
| 23 | */ |
| 24 | public function __construct($resolveUnboundAsSingletons = false, $useBaseContainer = false) |
| 25 | { |
| 26 | if ($useBaseContainer) { |
| 27 | parent::__construct($resolveUnboundAsSingletons); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $this->resolver = new Resolver($resolveUnboundAsSingletons); |
| 32 | $this->builders = new Factory($this, $this->resolver); |
| 33 | $this->bindThis(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @deprecated Currently, all usages of _get in the codebase |
| 38 | * are Service Locators, not Dependency Injection. |
| 39 | * They need to be refactored in the future. |
| 40 | * |
| 41 | * @param $offset |
| 42 | * |
| 43 | * @return mixed|null |
| 44 | */ |
| 45 | public function _get($offset) |
| 46 | { |
| 47 | try { |
| 48 | return $this->offsetGet($offset); |
| 49 | } catch (\Exception $e) { |
| 50 | \WPStaging\functions\debug_log($e->getMessage()); |
| 51 | |
| 52 | return null; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Allows to enqueue the ShutdownableInterface hook |
| 58 | * on classes requested directly by the application |
| 59 | * to the container. |
| 60 | */ |
| 61 | public function get($classOrInterface) |
| 62 | { |
| 63 | $instance = parent::get($classOrInterface); |
| 64 | if (is_object($instance) && $instance instanceof ShutdownableInterface) { |
| 65 | if (!has_action('shutdown', [$instance, 'onWpShutdown'])) { |
| 66 | add_action('shutdown', [$instance, 'onWpShutdown']); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return $instance; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @deprecated 4.1.15 |
| 75 | * Use get instead |
| 76 | */ |
| 77 | public function make($classOrInterface) |
| 78 | { |
| 79 | return $this->get($classOrInterface); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * You can use this to store an array of data in the container, without having to worry |
| 84 | * if the array was already initialized or not. |
| 85 | * |
| 86 | * @param $arrayName string The name of the array. If it doesn't exist yet, it will be created. |
| 87 | * @param $value mixed The value to add to the array. |
| 88 | * |
| 89 | * @return bool True if the value was added to the array. False if value already existed in the array. |
| 90 | */ |
| 91 | public function pushToArray($arrayName, $value) |
| 92 | { |
| 93 | try { |
| 94 | $arrayValues = (array)$this->offsetGet($arrayName); |
| 95 | |
| 96 | if (in_array($value, $arrayValues)) { |
| 97 | // Do nothing, as the item already exists in this array. |
| 98 | return false; |
| 99 | } |
| 100 | } catch (\Exception $e) { |
| 101 | // If nothing is set in the container yet, create an empty one. |
| 102 | $this->setVar($arrayName, []); |
| 103 | $arrayValues = []; |
| 104 | } |
| 105 | |
| 106 | // Add this value to the array. |
| 107 | $arrayValues[] = $value; |
| 108 | |
| 109 | $this->setVar($arrayName, $arrayValues); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * You can use this to get an array of data in the container, without having to worry |
| 116 | * if the array was already initialized or not. |
| 117 | * |
| 118 | * @param $arrayName string The name of the array. If it doesn't exist yet, an empty array will be returned. |
| 119 | * |
| 120 | * @return array The array of data requested, or an empty array if it's not set. |
| 121 | */ |
| 122 | public function getFromArray($arrayName) |
| 123 | { |
| 124 | try { |
| 125 | return (array)$this->offsetGet($arrayName); |
| 126 | } catch (\Exception $e) { |
| 127 | return []; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Overloads bind definition binding the prefix as well so that the DI container works locally. |
| 133 | * |
| 134 | * @param string $classOrInterface |
| 135 | * @param mixed $implementation |
| 136 | * @param array|null $afterBuildMethods |
| 137 | */ |
| 138 | public function bind($classOrInterface, $implementation = null, $afterBuildMethods = null) |
| 139 | { |
| 140 | if ($this->isDevAutoloader()) { |
| 141 | parent::bind(str_replace($this->prefix, '', $classOrInterface), $implementation, $afterBuildMethods); |
| 142 | } |
| 143 | |
| 144 | parent::bind($classOrInterface, $implementation, $afterBuildMethods); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Overloads singleton definition binding the prefix as well so that the DI container works locally. |
| 149 | * |
| 150 | * @param string $classOrInterface |
| 151 | * @param mixed $implementation |
| 152 | * @param array|null $afterBuildMethods |
| 153 | */ |
| 154 | public function singleton($classOrInterface, $implementation = null, $afterBuildMethods = null) |
| 155 | { |
| 156 | if ($this->isDevAutoloader()) { |
| 157 | parent::singleton(str_replace($this->prefix, '', $classOrInterface), $implementation, $afterBuildMethods); |
| 158 | } |
| 159 | |
| 160 | parent::singleton($classOrInterface, $implementation, $afterBuildMethods); |
| 161 | } |
| 162 | |
| 163 | private function isDevAutoloader() |
| 164 | { |
| 165 | if (defined('WPSTG_IS_DEV') && constant('WPSTG_IS_DEV')) { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | return defined('WPSTG_IS_DEV_AUTOLOADER') && constant('WPSTG_IS_DEV_AUTOLOADER'); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Binds the container to the base class name, the current class name and the container interface. |
| 174 | * Re-adding again from the Parent Container class with some adjustments so it can be called in constructor to keep the container instance singleton |
| 175 | * @return void |
| 176 | */ |
| 177 | private function bindThis() |
| 178 | { |
| 179 | $this->singleton(ContainerInterface::class, $this); |
| 180 | $this->singleton(BaseContainer::class, $this); |
| 181 | $this->singleton(Container::class, $this); |
| 182 | } |
| 183 | } |
| 184 |