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