initial commit
This commit is contained in:
274
lib/packages/League/Container/Definition/Definition.php
Normal file
274
lib/packages/League/Container/Definition/Definition.php
Normal file
@ -0,0 +1,274 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\Definition;
|
||||
|
||||
use Automattic\WooCommerce\Vendor\League\Container\Argument\{
|
||||
ArgumentResolverInterface, ArgumentResolverTrait, ClassNameInterface, RawArgumentInterface
|
||||
};
|
||||
use Automattic\WooCommerce\Vendor\League\Container\ContainerAwareTrait;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
|
||||
class Definition implements ArgumentResolverInterface, DefinitionInterface
|
||||
{
|
||||
use ArgumentResolverTrait;
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $alias;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $concrete;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $shared = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $tags = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $methods = [];
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $resolved;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $id
|
||||
* @param mixed $concrete
|
||||
*/
|
||||
public function __construct(string $id, $concrete = null)
|
||||
{
|
||||
$concrete = $concrete ?? $id;
|
||||
|
||||
$this->alias = $id;
|
||||
$this->concrete = $concrete;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addTag(string $tag) : DefinitionInterface
|
||||
{
|
||||
$this->tags[$tag] = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasTag(string $tag) : bool
|
||||
{
|
||||
return isset($this->tags[$tag]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setAlias(string $id) : DefinitionInterface
|
||||
{
|
||||
$this->alias = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAlias() : string
|
||||
{
|
||||
return $this->alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setShared(bool $shared = true) : DefinitionInterface
|
||||
{
|
||||
$this->shared = $shared;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isShared() : bool
|
||||
{
|
||||
return $this->shared;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConcrete()
|
||||
{
|
||||
return $this->concrete;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setConcrete($concrete) : DefinitionInterface
|
||||
{
|
||||
$this->concrete = $concrete;
|
||||
$this->resolved = null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addArgument($arg) : DefinitionInterface
|
||||
{
|
||||
$this->arguments[] = $arg;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addArguments(array $args) : DefinitionInterface
|
||||
{
|
||||
foreach ($args as $arg) {
|
||||
$this->addArgument($arg);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMethodCall(string $method, array $args = []) : DefinitionInterface
|
||||
{
|
||||
$this->methods[] = [
|
||||
'method' => $method,
|
||||
'arguments' => $args
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addMethodCalls(array $methods = []) : DefinitionInterface
|
||||
{
|
||||
foreach ($methods as $method => $args) {
|
||||
$this->addMethodCall($method, $args);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function resolve(bool $new = false)
|
||||
{
|
||||
$concrete = $this->concrete;
|
||||
|
||||
if ($this->isShared() && $this->resolved !== null && $new === false) {
|
||||
return $this->resolved;
|
||||
}
|
||||
|
||||
if (is_callable($concrete)) {
|
||||
$concrete = $this->resolveCallable($concrete);
|
||||
}
|
||||
|
||||
if ($concrete instanceof RawArgumentInterface) {
|
||||
$this->resolved = $concrete->getValue();
|
||||
|
||||
return $concrete->getValue();
|
||||
}
|
||||
|
||||
if ($concrete instanceof ClassNameInterface) {
|
||||
$concrete = $concrete->getClassName();
|
||||
}
|
||||
|
||||
if (is_string($concrete) && class_exists($concrete)) {
|
||||
$concrete = $this->resolveClass($concrete);
|
||||
}
|
||||
|
||||
if (is_object($concrete)) {
|
||||
$concrete = $this->invokeMethods($concrete);
|
||||
}
|
||||
|
||||
$this->resolved = $concrete;
|
||||
|
||||
return $concrete;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a callable.
|
||||
*
|
||||
* @param callable $concrete
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function resolveCallable(callable $concrete)
|
||||
{
|
||||
$resolved = $this->resolveArguments($this->arguments);
|
||||
|
||||
return call_user_func_array($concrete, $resolved);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a class.
|
||||
*
|
||||
* @param string $concrete
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
protected function resolveClass(string $concrete)
|
||||
{
|
||||
$resolved = $this->resolveArguments($this->arguments);
|
||||
$reflection = new ReflectionClass($concrete);
|
||||
|
||||
return $reflection->newInstanceArgs($resolved);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke methods on resolved instance.
|
||||
*
|
||||
* @param object $instance
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function invokeMethods($instance)
|
||||
{
|
||||
foreach ($this->methods as $method) {
|
||||
$args = $this->resolveArguments($method['arguments']);
|
||||
|
||||
/** @var callable $callable */
|
||||
$callable = [$instance, $method['method']];
|
||||
call_user_func_array($callable, $args);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
124
lib/packages/League/Container/Definition/DefinitionAggregate.php
Normal file
124
lib/packages/League/Container/Definition/DefinitionAggregate.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\Definition;
|
||||
|
||||
use Generator;
|
||||
use Automattic\WooCommerce\Vendor\League\Container\ContainerAwareTrait;
|
||||
use Automattic\WooCommerce\Vendor\League\Container\Exception\NotFoundException;
|
||||
|
||||
class DefinitionAggregate implements DefinitionAggregateInterface
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var DefinitionInterface[]
|
||||
*/
|
||||
protected $definitions = [];
|
||||
|
||||
/**
|
||||
* Construct.
|
||||
*
|
||||
* @param DefinitionInterface[] $definitions
|
||||
*/
|
||||
public function __construct(array $definitions = [])
|
||||
{
|
||||
$this->definitions = array_filter($definitions, function ($definition) {
|
||||
return ($definition instanceof DefinitionInterface);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add(string $id, $definition, bool $shared = false) : DefinitionInterface
|
||||
{
|
||||
if (!$definition instanceof DefinitionInterface) {
|
||||
$definition = new Definition($id, $definition);
|
||||
}
|
||||
|
||||
$this->definitions[] = $definition
|
||||
->setAlias($id)
|
||||
->setShared($shared)
|
||||
;
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function has(string $id) : bool
|
||||
{
|
||||
foreach ($this->getIterator() as $definition) {
|
||||
if ($id === $definition->getAlias()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasTag(string $tag) : bool
|
||||
{
|
||||
foreach ($this->getIterator() as $definition) {
|
||||
if ($definition->hasTag($tag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(string $id) : DefinitionInterface
|
||||
{
|
||||
foreach ($this->getIterator() as $definition) {
|
||||
if ($id === $definition->getAlias()) {
|
||||
return $definition->setLeagueContainer($this->getLeagueContainer());
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function resolve(string $id, bool $new = false)
|
||||
{
|
||||
return $this->getDefinition($id)->resolve($new);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function resolveTagged(string $tag, bool $new = false) : array
|
||||
{
|
||||
$arrayOf = [];
|
||||
|
||||
foreach ($this->getIterator() as $definition) {
|
||||
if ($definition->hasTag($tag)) {
|
||||
$arrayOf[] = $definition->setLeagueContainer($this->getLeagueContainer())->resolve($new);
|
||||
}
|
||||
}
|
||||
|
||||
return $arrayOf;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getIterator() : Generator
|
||||
{
|
||||
$count = count($this->definitions);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
yield $this->definitions[$i];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\Definition;
|
||||
|
||||
use IteratorAggregate;
|
||||
use Automattic\WooCommerce\Vendor\League\Container\ContainerAwareInterface;
|
||||
|
||||
interface DefinitionAggregateInterface extends ContainerAwareInterface, IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* Add a definition to the aggregate.
|
||||
*
|
||||
* @param string $id
|
||||
* @param mixed $definition
|
||||
* @param boolean $shared
|
||||
*
|
||||
* @return DefinitionInterface
|
||||
*/
|
||||
public function add(string $id, $definition, bool $shared = false) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Checks whether alias exists as definition.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function has(string $id) : bool;
|
||||
|
||||
/**
|
||||
* Checks whether tag exists as definition.
|
||||
*
|
||||
* @param string $tag
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasTag(string $tag) : bool;
|
||||
|
||||
/**
|
||||
* Get the definition to be extended.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return DefinitionInterface
|
||||
*/
|
||||
public function getDefinition(string $id) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Resolve and build a concrete value from an id/alias.
|
||||
*
|
||||
* @param string $id
|
||||
* @param boolean $new
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function resolve(string $id, bool $new = false);
|
||||
|
||||
/**
|
||||
* Resolve and build an array of concrete values from a tag.
|
||||
*
|
||||
* @param string $tag
|
||||
* @param boolean $new
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function resolveTagged(string $tag, bool $new = false);
|
||||
}
|
120
lib/packages/League/Container/Definition/DefinitionInterface.php
Normal file
120
lib/packages/League/Container/Definition/DefinitionInterface.php
Normal file
@ -0,0 +1,120 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\Definition;
|
||||
|
||||
use Automattic\WooCommerce\Vendor\League\Container\ContainerAwareInterface;
|
||||
|
||||
interface DefinitionInterface extends ContainerAwareInterface
|
||||
{
|
||||
/**
|
||||
* Add a tag to the definition.
|
||||
*
|
||||
* @param string $tag
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function addTag(string $tag) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Does the definition have a tag?
|
||||
*
|
||||
* @param string $tag
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasTag(string $tag) : bool;
|
||||
|
||||
/**
|
||||
* Set the alias of the definition.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return DefinitionInterface
|
||||
*/
|
||||
public function setAlias(string $id) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Get the alias of the definition.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAlias() : string;
|
||||
|
||||
/**
|
||||
* Set whether this is a shared definition.
|
||||
*
|
||||
* @param boolean $shared
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setShared(bool $shared) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Is this a shared definition?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isShared() : bool;
|
||||
|
||||
/**
|
||||
* Get the concrete of the definition.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConcrete();
|
||||
|
||||
/**
|
||||
* Set the concrete of the definition.
|
||||
*
|
||||
* @param mixed $concrete
|
||||
*
|
||||
* @return DefinitionInterface
|
||||
*/
|
||||
public function setConcrete($concrete) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Add an argument to be injected.
|
||||
*
|
||||
* @param mixed $arg
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function addArgument($arg) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Add multiple arguments to be injected.
|
||||
*
|
||||
* @param array $args
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function addArguments(array $args) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Add a method to be invoked
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function addMethodCall(string $method, array $args = []) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Add multiple methods to be invoked
|
||||
*
|
||||
* @param array $methods
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function addMethodCalls(array $methods = []) : DefinitionInterface;
|
||||
|
||||
/**
|
||||
* Handle instantiation and manipulation of value and return.
|
||||
*
|
||||
* @param boolean $new
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function resolve(bool $new = false);
|
||||
}
|
Reference in New Issue
Block a user