Nav apraksta

Container.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php declare(strict_types=1);
  2. namespace Automattic\WooCommerce\Vendor\League\Container;
  3. use Automattic\WooCommerce\Vendor\League\Container\Definition\{DefinitionAggregate, DefinitionInterface, DefinitionAggregateInterface};
  4. use Automattic\WooCommerce\Vendor\League\Container\Exception\{NotFoundException, ContainerException};
  5. use Automattic\WooCommerce\Vendor\League\Container\Inflector\{InflectorAggregate, InflectorInterface, InflectorAggregateInterface};
  6. use Automattic\WooCommerce\Vendor\League\Container\ServiceProvider\{
  7. ServiceProviderAggregate,
  8. ServiceProviderAggregateInterface,
  9. ServiceProviderInterface
  10. };
  11. use Psr\Container\ContainerInterface;
  12. class Container implements ContainerInterface
  13. {
  14. /**
  15. * @var boolean
  16. */
  17. protected $defaultToShared = false;
  18. /**
  19. * @var DefinitionAggregateInterface
  20. */
  21. protected $definitions;
  22. /**
  23. * @var ServiceProviderAggregateInterface
  24. */
  25. protected $providers;
  26. /**
  27. * @var InflectorAggregateInterface
  28. */
  29. protected $inflectors;
  30. /**
  31. * @var ContainerInterface[]
  32. */
  33. protected $delegates = [];
  34. /**
  35. * Construct.
  36. *
  37. * @param DefinitionAggregateInterface|null $definitions
  38. * @param ServiceProviderAggregateInterface|null $providers
  39. * @param InflectorAggregateInterface|null $inflectors
  40. */
  41. public function __construct(
  42. DefinitionAggregateInterface $definitions = null,
  43. ServiceProviderAggregateInterface $providers = null,
  44. InflectorAggregateInterface $inflectors = null
  45. ) {
  46. $this->definitions = $definitions ?? new DefinitionAggregate;
  47. $this->providers = $providers ?? new ServiceProviderAggregate;
  48. $this->inflectors = $inflectors ?? new InflectorAggregate;
  49. if ($this->definitions instanceof ContainerAwareInterface) {
  50. $this->definitions->setLeagueContainer($this);
  51. }
  52. if ($this->providers instanceof ContainerAwareInterface) {
  53. $this->providers->setLeagueContainer($this);
  54. }
  55. if ($this->inflectors instanceof ContainerAwareInterface) {
  56. $this->inflectors->setLeagueContainer($this);
  57. }
  58. }
  59. /**
  60. * Add an item to the container.
  61. *
  62. * @param string $id
  63. * @param mixed $concrete
  64. * @param boolean $shared
  65. *
  66. * @return DefinitionInterface
  67. */
  68. public function add(string $id, $concrete = null, bool $shared = null) : DefinitionInterface
  69. {
  70. $concrete = $concrete ?? $id;
  71. $shared = $shared ?? $this->defaultToShared;
  72. return $this->definitions->add($id, $concrete, $shared);
  73. }
  74. /**
  75. * Proxy to add with shared as true.
  76. *
  77. * @param string $id
  78. * @param mixed $concrete
  79. *
  80. * @return DefinitionInterface
  81. */
  82. public function share(string $id, $concrete = null) : DefinitionInterface
  83. {
  84. return $this->add($id, $concrete, true);
  85. }
  86. /**
  87. * Whether the container should default to defining shared definitions.
  88. *
  89. * @param boolean $shared
  90. *
  91. * @return self
  92. */
  93. public function defaultToShared(bool $shared = true) : ContainerInterface
  94. {
  95. $this->defaultToShared = $shared;
  96. return $this;
  97. }
  98. /**
  99. * Get a definition to extend.
  100. *
  101. * @param string $id [description]
  102. *
  103. * @return DefinitionInterface
  104. */
  105. public function extend(string $id) : DefinitionInterface
  106. {
  107. if ($this->providers->provides($id)) {
  108. $this->providers->register($id);
  109. }
  110. if ($this->definitions->has($id)) {
  111. return $this->definitions->getDefinition($id);
  112. }
  113. throw new NotFoundException(
  114. sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $id)
  115. );
  116. }
  117. /**
  118. * Add a service provider.
  119. *
  120. * @param ServiceProviderInterface|string $provider
  121. *
  122. * @return self
  123. */
  124. public function addServiceProvider($provider) : self
  125. {
  126. $this->providers->add($provider);
  127. return $this;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function get($id, bool $new = false)
  133. {
  134. if ($this->definitions->has($id)) {
  135. $resolved = $this->definitions->resolve($id, $new);
  136. return $this->inflectors->inflect($resolved);
  137. }
  138. if ($this->definitions->hasTag($id)) {
  139. $arrayOf = $this->definitions->resolveTagged($id, $new);
  140. array_walk($arrayOf, function (&$resolved) {
  141. $resolved = $this->inflectors->inflect($resolved);
  142. });
  143. return $arrayOf;
  144. }
  145. if ($this->providers->provides($id)) {
  146. $this->providers->register($id);
  147. if (!$this->definitions->has($id) && !$this->definitions->hasTag($id)) {
  148. throw new ContainerException(sprintf('Service provider lied about providing (%s) service', $id));
  149. }
  150. return $this->get($id, $new);
  151. }
  152. foreach ($this->delegates as $delegate) {
  153. if ($delegate->has($id)) {
  154. $resolved = $delegate->get($id);
  155. return $this->inflectors->inflect($resolved);
  156. }
  157. }
  158. throw new NotFoundException(sprintf('Alias (%s) is not being managed by the container or delegates', $id));
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function has($id) : bool
  164. {
  165. if ($this->definitions->has($id)) {
  166. return true;
  167. }
  168. if ($this->definitions->hasTag($id)) {
  169. return true;
  170. }
  171. if ($this->providers->provides($id)) {
  172. return true;
  173. }
  174. foreach ($this->delegates as $delegate) {
  175. if ($delegate->has($id)) {
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. /**
  182. * Allows for manipulation of specific types on resolution.
  183. *
  184. * @param string $type
  185. * @param callable|null $callback
  186. *
  187. * @return InflectorInterface
  188. */
  189. public function inflector(string $type, callable $callback = null) : InflectorInterface
  190. {
  191. return $this->inflectors->add($type, $callback);
  192. }
  193. /**
  194. * Delegate a backup container to be checked for services if it
  195. * cannot be resolved via this container.
  196. *
  197. * @param ContainerInterface $container
  198. *
  199. * @return self
  200. */
  201. public function delegate(ContainerInterface $container) : self
  202. {
  203. $this->delegates[] = $container;
  204. if ($container instanceof ContainerAwareInterface) {
  205. $container->setLeagueContainer($this);
  206. }
  207. return $this;
  208. }
  209. }