add wp-rocket

This commit is contained in:
nguyen dung
2022-02-18 19:09:35 +07:00
parent 39b8cb3612
commit 3110d00ee7
927 changed files with 271703 additions and 2 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace WP_Rocket\Engine\Container\Argument;
use WP_Rocket\Engine\Container\ImmutableContainerAwareInterface;
use ReflectionFunctionAbstract;
interface ArgumentResolverInterface extends ImmutableContainerAwareInterface
{
/**
* Resolve an array of arguments to their concrete implementations.
*
* @param array $arguments
* @return array
*/
public function resolveArguments(array $arguments);
/**
* Resolves the correct arguments to be passed to a method.
*
* @param \ReflectionFunctionAbstract $method
* @param array $args
* @return array
*/
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = []);
}

View File

@@ -0,0 +1,82 @@
<?php
namespace WP_Rocket\Engine\Container\Argument;
use WP_Rocket\Engine\Container\Exception\NotFoundException;
use WP_Rocket\Engine\Container\ReflectionContainer;
use ReflectionFunctionAbstract;
use ReflectionParameter;
trait ArgumentResolverTrait
{
/**
* {@inheritdoc}
*/
public function resolveArguments(array $arguments)
{
foreach ($arguments as &$arg) {
if ($arg instanceof RawArgumentInterface) {
$arg = $arg->getValue();
continue;
}
if (! is_string($arg)) {
continue;
}
$container = $this->getContainer();
if (is_null($container) && $this instanceof ReflectionContainer) {
$container = $this;
}
if (! is_null($container) && $container->has($arg)) {
$arg = $container->get($arg);
if ($arg instanceof RawArgumentInterface) {
$arg = $arg->getValue();
}
continue;
}
}
return $arguments;
}
/**
* {@inheritdoc}
*/
public function reflectArguments(ReflectionFunctionAbstract $method, array $args = [])
{
$arguments = array_map(function (ReflectionParameter $param) use ($method, $args) {
$name = $param->getName();
$class = $param->getClass();
if (array_key_exists($name, $args)) {
return $args[$name];
}
if (! is_null($class)) {
return $class->getName();
}
if ($param->isDefaultValueAvailable()) {
return $param->getDefaultValue();
}
throw new NotFoundException(sprintf(
'Unable to resolve a value for parameter (%s) in the function/method (%s)',
$name,
$method->getName()
));
}, $method->getParameters());
return $this->resolveArguments($arguments);
}
/**
* @return \WP_Rocket\Engine\Container\ContainerInterface
*/
abstract public function getContainer();
}

View File

@@ -0,0 +1,27 @@
<?php
namespace WP_Rocket\Engine\Container\Argument;
class RawArgument implements RawArgumentInterface
{
/**
* @var mixed
*/
protected $value;
/**
* {@inheritdoc}
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function getValue()
{
return $this->value;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace WP_Rocket\Engine\Container\Argument;
interface RawArgumentInterface
{
/**
* Return the value of the raw argument.
*
* @return mixed
*/
public function getValue();
}

View File

@@ -0,0 +1,305 @@
<?php
namespace WP_Rocket\Engine\Container;
use Psr\Container\ContainerInterface as InteropContainerInterface;
use WP_Rocket\Engine\Container\Argument\RawArgumentInterface;
use WP_Rocket\Engine\Container\Definition\DefinitionFactory;
use WP_Rocket\Engine\Container\Definition\DefinitionFactoryInterface;
use WP_Rocket\Engine\Container\Definition\DefinitionInterface;
use WP_Rocket\Engine\Container\Exception\NotFoundException;
use WP_Rocket\Engine\Container\Inflector\InflectorAggregate;
use WP_Rocket\Engine\Container\Inflector\InflectorAggregateInterface;
use WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderAggregate;
use WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderAggregateInterface;
class Container implements ContainerInterface
{
/**
* @var \WP_Rocket\Engine\Container\Definition\DefinitionFactoryInterface
*/
protected $definitionFactory;
/**
* @var \WP_Rocket\Engine\Container\Definition\DefinitionInterface[]
*/
protected $definitions = [];
/**
* @var \WP_Rocket\Engine\Container\Definition\DefinitionInterface[]
*/
protected $sharedDefinitions = [];
/**
* @var \WP_Rocket\Engine\Container\Inflector\InflectorAggregateInterface
*/
protected $inflectors;
/**
* @var \WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderAggregateInterface
*/
protected $providers;
/**
* @var array
*/
protected $shared = [];
/**
* @var \Psr\Container\ContainerInterface[]
*/
protected $delegates = [];
/**
* Constructor.
*
* @param \WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers
* @param \WP_Rocket\Engine\Container\Inflector\InflectorAggregateInterface|null $inflectors
* @param \WP_Rocket\Engine\Container\Definition\DefinitionFactoryInterface|null $definitionFactory
*/
public function __construct(
ServiceProviderAggregateInterface $providers = null,
InflectorAggregateInterface $inflectors = null,
DefinitionFactoryInterface $definitionFactory = null
) {
// set required dependencies
$this->providers = (is_null($providers))
? (new ServiceProviderAggregate)->setContainer($this)
: $providers->setContainer($this);
$this->inflectors = (is_null($inflectors))
? (new InflectorAggregate)->setContainer($this)
: $inflectors->setContainer($this);
$this->definitionFactory = (is_null($definitionFactory))
? (new DefinitionFactory)->setContainer($this)
: $definitionFactory->setContainer($this);
}
/**
* {@inheritdoc}
*/
public function get($alias, array $args = [])
{
try {
return $this->getFromThisContainer($alias, $args);
} catch (NotFoundException $exception) {
if ($this->providers->provides($alias)) {
$this->providers->register($alias);
return $this->getFromThisContainer($alias, $args);
}
$resolved = $this->getFromDelegate($alias, $args);
return $this->inflectors->inflect($resolved);
}
}
/**
* {@inheritdoc}
*/
public function has($alias)
{
if (array_key_exists($alias, $this->definitions) || $this->hasShared($alias)) {
return true;
}
if ($this->providers->provides($alias)) {
return true;
}
return $this->hasInDelegate($alias);
}
/**
* Returns a boolean to determine if the container has a shared instance of an alias.
*
* @param string $alias
* @param boolean $resolved
* @return boolean
*/
public function hasShared($alias, $resolved = false)
{
$shared = ($resolved === false) ? array_merge($this->shared, $this->sharedDefinitions) : $this->shared;
return (array_key_exists($alias, $shared));
}
/**
* {@inheritdoc}
*/
public function add($alias, $concrete = null, $share = false)
{
unset($this->shared[$alias]);
unset($this->definitions[$alias]);
unset($this->sharedDefinitions[$alias]);
if (is_null($concrete)) {
$concrete = $alias;
}
$definition = $this->definitionFactory->getDefinition($alias, $concrete);
if ($definition instanceof DefinitionInterface) {
if ($share === false) {
$this->definitions[$alias] = $definition;
} else {
$this->sharedDefinitions[$alias] = $definition;
}
return $definition;
}
// dealing with a value that cannot build a definition
$this->shared[$alias] = $concrete;
}
/**
* {@inheritdoc}
*/
public function share($alias, $concrete = null)
{
return $this->add($alias, $concrete, true);
}
/**
* {@inheritdoc}
*/
public function addServiceProvider($provider)
{
$this->providers->add($provider);
return $this;
}
/**
* {@inheritdoc}
*/
public function extend($alias)
{
if ($this->providers->provides($alias)) {
$this->providers->register($alias);
}
if (array_key_exists($alias, $this->definitions)) {
return $this->definitions[$alias];
}
if (array_key_exists($alias, $this->sharedDefinitions)) {
return $this->sharedDefinitions[$alias];
}
throw new NotFoundException(
sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $alias)
);
}
/**
* {@inheritdoc}
*/
public function inflector($type, callable $callback = null)
{
return $this->inflectors->add($type, $callback);
}
/**
* {@inheritdoc}
*/
public function call(callable $callable, array $args = [])
{
return (new ReflectionContainer)->setContainer($this)->call($callable, $args);
}
/**
* Delegate a backup container to be checked for services if it
* cannot be resolved via this container.
*
* @param \Psr\Container\ContainerInterface $container
* @return $this
*/
public function delegate(InteropContainerInterface $container)
{
$this->delegates[] = $container;
if ($container instanceof ImmutableContainerAwareInterface) {
$container->setContainer($this);
}
return $this;
}
/**
* Returns true if service is registered in one of the delegated backup containers.
*
* @param string $alias
* @return boolean
*/
public function hasInDelegate($alias)
{
foreach ($this->delegates as $container) {
if ($container->has($alias)) {
return true;
}
}
return false;
}
/**
* Attempt to get a service from the stack of delegated backup containers.
*
* @param string $alias
* @param array $args
* @return mixed
*/
protected function getFromDelegate($alias, array $args = [])
{
foreach ($this->delegates as $container) {
if ($container->has($alias)) {
return $container->get($alias, $args);
}
continue;
}
throw new NotFoundException(
sprintf('Alias (%s) is not being managed by the container', $alias)
);
}
/**
* Get a service that has been registered in this container.
*
* @param string $alias
* @param array $args
* @return mixed
*/
protected function getFromThisContainer($alias, array $args = [])
{
if ($this->hasShared($alias, true)) {
$shared = $this->inflectors->inflect($this->shared[$alias]);
if ($shared instanceof RawArgumentInterface) {
return $shared->getValue();
}
return $shared;
}
if (array_key_exists($alias, $this->sharedDefinitions)) {
$shared = $this->inflectors->inflect($this->sharedDefinitions[$alias]->build());
$this->shared[$alias] = $shared;
return $shared;
}
if (array_key_exists($alias, $this->definitions)) {
return $this->inflectors->inflect(
$this->definitions[$alias]->build($args)
);
}
throw new NotFoundException(
sprintf('Alias (%s) is not being managed by the container', $alias)
);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace WP_Rocket\Engine\Container;
interface ContainerAwareInterface
{
/**
* Set a container
*
* @param \WP_Rocket\Engine\Container\ContainerInterface $container
*/
public function setContainer(ContainerInterface $container);
/**
* Get the container
*
* @return \WP_Rocket\Engine\Container\ContainerInterface
*/
public function getContainer();
}

View File

@@ -0,0 +1,34 @@
<?php
namespace WP_Rocket\Engine\Container;
trait ContainerAwareTrait
{
/**
* @var \WP_Rocket\Engine\Container\ContainerInterface
*/
protected $container;
/**
* Set a container.
*
* @param \WP_Rocket\Engine\Container\ContainerInterface $container
* @return $this
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
return $this;
}
/**
* Get the container.
*
* @return \WP_Rocket\Engine\Container\ContainerInterface
*/
public function getContainer()
{
return $this->container;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace WP_Rocket\Engine\Container;
interface ContainerInterface extends ImmutableContainerInterface
{
/**
* Add an item to the container.
*
* @param string $alias
* @param mixed|null $concrete
* @param boolean $share
* @return \WP_Rocket\Engine\Container\Definition\DefinitionInterface
*/
public function add($alias, $concrete = null, $share = false);
/**
* Convenience method to add an item to the container as a shared item.
*
* @param string $alias
* @param mixed|null $concrete
* @return \WP_Rocket\Engine\Container\Definition\DefinitionInterface
*/
public function share($alias, $concrete = null);
/**
* Add a service provider to the container.
*
* @param string|\WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderInterface $provider
* @return void
*/
public function addServiceProvider($provider);
/**
* Returns a definition of an item to be extended.
*
* @param string $alias
* @return \WP_Rocket\Engine\Container\Definition\DefinitionInterface
*/
public function extend($alias);
/**
* Allows for manipulation of specific types on resolution.
*
* @param string $type
* @param callable|null $callback
* @return \WP_Rocket\Engine\Container\Inflector\Inflector|void
*/
public function inflector($type, callable $callback = null);
/**
* Invoke a callable via the container.
*
* @param callable $callable
* @param array $args
* @return mixed
*/
public function call(callable $callable, array $args = []);
}

View File

@@ -0,0 +1,62 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
use WP_Rocket\Engine\Container\Argument\ArgumentResolverInterface;
use WP_Rocket\Engine\Container\Argument\ArgumentResolverTrait;
use WP_Rocket\Engine\Container\ImmutableContainerAwareTrait;
abstract class AbstractDefinition implements ArgumentResolverInterface, DefinitionInterface
{
use ArgumentResolverTrait;
use ImmutableContainerAwareTrait;
/**
* @var string
*/
protected $alias;
/**
* @var mixed
*/
protected $concrete;
/**
* @var array
*/
protected $arguments = [];
/**
* Constructor.
*
* @param string $alias
* @param mixed $concrete
*/
public function __construct($alias, $concrete)
{
$this->alias = $alias;
$this->concrete = $concrete;
}
/**
* {@inheritdoc}
*/
public function withArgument($arg)
{
$this->arguments[] = $arg;
return $this;
}
/**
* {@inheritdoc}
*/
public function withArguments(array $args)
{
foreach ($args as $arg) {
$this->withArgument($arg);
}
return $this;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
class CallableDefinition extends AbstractDefinition
{
/**
* {@inheritdoc}
*/
public function build(array $args = [])
{
$args = (empty($args)) ? $this->arguments : $args;
$resolved = $this->resolveArguments($args);
if (is_array($this->concrete) && is_string($this->concrete[0])) {
$this->concrete[0] = ($this->getContainer()->has($this->concrete[0]))
? $this->getContainer()->get($this->concrete[0])
: $this->concrete[0];
}
return call_user_func_array($this->concrete, $resolved);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
use ReflectionClass;
class ClassDefinition extends AbstractDefinition implements ClassDefinitionInterface
{
/**
* @var array
*/
protected $methods = [];
/**
* {@inheritdoc}
*/
public function withMethodCall($method, array $args = [])
{
$this->methods[] = [
'method' => $method,
'arguments' => $args
];
return $this;
}
/**
* {@inheritdoc}
*/
public function withMethodCalls(array $methods = [])
{
foreach ($methods as $method => $args) {
$this->withMethodCall($method, $args);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function build(array $args = [])
{
$args = (empty($args)) ? $this->arguments : $args;
$resolved = $this->resolveArguments($args);
$reflection = new ReflectionClass($this->concrete);
$instance = $reflection->newInstanceArgs($resolved);
return $this->invokeMethods($instance);
}
/**
* Invoke methods on resolved instance.
*
* @param object $instance
* @return object
*/
protected function invokeMethods($instance)
{
foreach ($this->methods as $method) {
$args = $this->resolveArguments($method['arguments']);
call_user_func_array([$instance, $method['method']], $args);
}
return $instance;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
interface ClassDefinitionInterface extends DefinitionInterface
{
/**
* Add a method to be invoked
*
* @param string $method
* @param array $args
* @return $this
*/
public function withMethodCall($method, array $args = []);
/**
* Add multiple methods to be invoked
*
* @param array $methods
* @return $this
*/
public function withMethodCalls(array $methods = []);
}

View File

@@ -0,0 +1,28 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
use WP_Rocket\Engine\Container\ImmutableContainerAwareTrait;
class DefinitionFactory implements DefinitionFactoryInterface
{
use ImmutableContainerAwareTrait;
/**
* {@inheritdoc}
*/
public function getDefinition($alias, $concrete)
{
if (is_callable($concrete)) {
return (new CallableDefinition($alias, $concrete))->setContainer($this->getContainer());
}
if (is_string($concrete) && class_exists($concrete)) {
return (new ClassDefinition($alias, $concrete))->setContainer($this->getContainer());
}
// if the item is not definable we just return the value to be stored
// in the container as an arbitrary value/instance
return $concrete;
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
use WP_Rocket\Engine\Container\ImmutableContainerAwareInterface;
interface DefinitionFactoryInterface extends ImmutableContainerAwareInterface
{
/**
* Return a definition based on type of concrete.
*
* @param string $alias
* @param mixed $concrete
* @return mixed
*/
public function getDefinition($alias, $concrete);
}

View File

@@ -0,0 +1,30 @@
<?php
namespace WP_Rocket\Engine\Container\Definition;
interface DefinitionInterface
{
/**
* Handle instantiation and manipulation of value and return.
*
* @param array $args
* @return mixed
*/
public function build(array $args = []);
/**
* Add an argument to be injected.
*
* @param mixed $arg
* @return $this
*/
public function withArgument($arg);
/**
* Add multiple arguments to be injected.
*
* @param array $args
* @return $this
*/
public function withArguments(array $args);
}

View File

@@ -0,0 +1,10 @@
<?php
namespace WP_Rocket\Engine\Container\Exception;
use Psr\Container\NotFoundExceptionInterface;
use InvalidArgumentException;
class NotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
{
}

View File

@@ -0,0 +1,22 @@
<?php
namespace WP_Rocket\Engine\Container;
use Psr\Container\ContainerInterface as InteropContainerInterface;
interface ImmutableContainerAwareInterface
{
/**
* Set a container
*
* @param \Psr\Container\ContainerInterface $container
*/
public function setContainer(InteropContainerInterface $container);
/**
* Get the container
*
* @return \WP_Rocket\Engine\Container\ImmutableContainerInterface
*/
public function getContainer();
}

View File

@@ -0,0 +1,36 @@
<?php
namespace WP_Rocket\Engine\Container;
use Psr\Container\ContainerInterface as InteropContainerInterface;
trait ImmutableContainerAwareTrait
{
/**
* @var \Psr\Container\ContainerInterface
*/
protected $container;
/**
* Set a container.
*
* @param \Psr\Container\ContainerInterface $container
* @return $this
*/
public function setContainer(InteropContainerInterface $container)
{
$this->container = $container;
return $this;
}
/**
* Get the container.
*
* @return \WP_Rocket\Engine\Container\ImmutableContainerInterface
*/
public function getContainer()
{
return $this->container;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace WP_Rocket\Engine\Container;
use Psr\Container\ContainerInterface as InteropContainerInterface;
interface ImmutableContainerInterface extends InteropContainerInterface
{
}

View File

@@ -0,0 +1,103 @@
<?php
namespace WP_Rocket\Engine\Container\Inflector;
use WP_Rocket\Engine\Container\ImmutableContainerAwareTrait;
use WP_Rocket\Engine\Container\Argument\ArgumentResolverInterface;
use WP_Rocket\Engine\Container\Argument\ArgumentResolverTrait;
class Inflector implements ArgumentResolverInterface
{
use ArgumentResolverTrait;
use ImmutableContainerAwareTrait;
/**
* @var array
*/
protected $methods = [];
/**
* @var array
*/
protected $properties = [];
/**
* Defines a method to be invoked on the subject object.
*
* @param string $name
* @param array $args
* @return $this
*/
public function invokeMethod($name, array $args)
{
$this->methods[$name] = $args;
return $this;
}
/**
* Defines multiple methods to be invoked on the subject object.
*
* @param array $methods
* @return $this
*/
public function invokeMethods(array $methods)
{
foreach ($methods as $name => $args) {
$this->invokeMethod($name, $args);
}
return $this;
}
/**
* Defines a property to be set on the subject object.
*
* @param string $property
* @param mixed $value
* @return $this
*/
public function setProperty($property, $value)
{
$this->properties[$property] = $value;
return $this;
}
/**
* Defines multiple properties to be set on the subject object.
*
* @param array $properties
* @return $this
*/
public function setProperties(array $properties)
{
foreach ($properties as $property => $value) {
$this->setProperty($property, $value);
}
return $this;
}
/**
* Apply inflections to an object.
*
* @param object $object
* @return void
*/
public function inflect($object)
{
$properties = $this->resolveArguments(array_values($this->properties));
$properties = array_combine(array_keys($this->properties), $properties);
foreach ($properties as $property => $value) {
$object->{$property} = $value;
}
foreach ($this->methods as $method => $args) {
$args = $this->resolveArguments($args);
call_user_func_array([$object, $method], $args);
}
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace WP_Rocket\Engine\Container\Inflector;
use WP_Rocket\Engine\Container\ImmutableContainerAwareTrait;
class InflectorAggregate implements InflectorAggregateInterface
{
use ImmutableContainerAwareTrait;
/**
* @var array
*/
protected $inflectors = [];
/**
* {@inheritdoc}
*/
public function add($type, callable $callback = null)
{
if (is_null($callback)) {
$inflector = new Inflector;
$this->inflectors[$type] = $inflector;
return $inflector;
}
$this->inflectors[$type] = $callback;
}
/**
* {@inheritdoc}
*/
public function inflect($object)
{
foreach ($this->inflectors as $type => $inflector) {
if (! $object instanceof $type) {
continue;
}
if ($inflector instanceof Inflector) {
$inflector->setContainer($this->getContainer());
$inflector->inflect($object);
continue;
}
// must be dealing with a callable as the inflector
call_user_func_array($inflector, [$object]);
}
return $object;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace WP_Rocket\Engine\Container\Inflector;
use WP_Rocket\Engine\Container\ImmutableContainerAwareInterface;
interface InflectorAggregateInterface extends ImmutableContainerAwareInterface
{
/**
* Add an inflector to the aggregate.
*
* @param string $type
* @param callable $callback
* @return \WP_Rocket\Engine\Container\Inflector\Inflector
*/
public function add($type, callable $callback = null);
/**
* Applies all inflectors to an object.
*
* @param object $object
* @return object
*/
public function inflect($object);
}

View File

@@ -0,0 +1,87 @@
<?php
namespace WP_Rocket\Engine\Container;
use WP_Rocket\Engine\Container\Argument\ArgumentResolverInterface;
use WP_Rocket\Engine\Container\Argument\ArgumentResolverTrait;
use WP_Rocket\Engine\Container\Exception\NotFoundException;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
class ReflectionContainer implements
ArgumentResolverInterface,
ImmutableContainerInterface
{
use ArgumentResolverTrait;
use ImmutableContainerAwareTrait;
/**
* {@inheritdoc}
*/
public function get($alias, array $args = [])
{
if (! $this->has($alias)) {
throw new NotFoundException(
sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $alias)
);
}
$reflector = new ReflectionClass($alias);
$construct = $reflector->getConstructor();
if ($construct === null) {
return new $alias;
}
return $reflector->newInstanceArgs(
$this->reflectArguments($construct, $args)
);
}
/**
* {@inheritdoc}
*/
public function has($alias)
{
return class_exists($alias);
}
/**
* Invoke a callable via the container.
*
* @param callable $callable
* @param array $args
* @return mixed
*/
public function call(callable $callable, array $args = [])
{
if (is_string($callable) && strpos($callable, '::') !== false) {
$callable = explode('::', $callable);
}
if (is_array($callable)) {
if (is_string($callable[0])) {
$callable[0] = $this->getContainer()->get($callable[0]);
}
$reflection = new ReflectionMethod($callable[0], $callable[1]);
if ($reflection->isStatic()) {
$callable[0] = null;
}
return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
}
if (is_object($callable)) {
$reflection = new ReflectionMethod($callable, '__invoke');
return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
}
$reflection = new ReflectionFunction($callable);
return $reflection->invokeArgs($this->reflectArguments($reflection, $args));
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
use WP_Rocket\Engine\Container\ContainerAwareTrait;
abstract class AbstractServiceProvider implements ServiceProviderInterface
{
use ContainerAwareTrait;
/**
* @var array
*/
protected $provides = [];
/**
* {@inheritdoc}
*/
public function provides($alias = null)
{
if (! is_null($alias)) {
return (in_array($alias, $this->provides));
}
return $this->provides;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
abstract class AbstractSignatureServiceProvider
extends AbstractServiceProvider
implements SignatureServiceProviderInterface
{
/**
* @var string
*/
protected $signature;
/**
* {@inheritdoc}
*/
public function withSignature($signature)
{
$this->signature = $signature;
return $this;
}
/**
* {@inheritdoc}
*/
public function getSignature()
{
return (is_null($this->signature)) ? get_class($this) : $this->signature;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
interface BootableServiceProviderInterface extends ServiceProviderInterface
{
/**
* Method will be invoked on registration of a service provider implementing
* this interface. Provides ability for eager loading of Service Providers.
*
* @return void
*/
public function boot();
}

View File

@@ -0,0 +1,88 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
use WP_Rocket\Engine\Container\ContainerAwareInterface;
use WP_Rocket\Engine\Container\ContainerAwareTrait;
class ServiceProviderAggregate implements ServiceProviderAggregateInterface
{
use ContainerAwareTrait;
/**
* @var array
*/
protected $providers = [];
/**
* @var array
*/
protected $registered = [];
/**
* {@inheritdoc}
*/
public function add($provider)
{
if (is_string($provider) && class_exists($provider)) {
$provider = new $provider;
}
if ($provider instanceof ContainerAwareInterface) {
$provider->setContainer($this->getContainer());
}
if ($provider instanceof BootableServiceProviderInterface) {
$provider->boot();
}
if ($provider instanceof ServiceProviderInterface) {
foreach ($provider->provides() as $service) {
$this->providers[$service] = $provider;
}
return $this;
}
throw new \InvalidArgumentException(
'A service provider must be a fully qualified class name or instance ' .
'of (\WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderInterface)'
);
}
/**
* {@inheritdoc}
*/
public function provides($service)
{
return array_key_exists($service, $this->providers);
}
/**
* {@inheritdoc}
*/
public function register($service)
{
if (! array_key_exists($service, $this->providers)) {
throw new \InvalidArgumentException(
sprintf('(%s) is not provided by a service provider', $service)
);
}
$provider = $this->providers[$service];
$signature = get_class($provider);
if ($provider instanceof SignatureServiceProviderInterface) {
$signature = $provider->getSignature();
}
// ensure that the provider hasn't already been invoked by any other service request
if (in_array($signature, $this->registered)) {
return;
}
$provider->register();
$this->registered[] = $signature;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
use WP_Rocket\Engine\Container\ContainerAwareInterface;
interface ServiceProviderAggregateInterface extends ContainerAwareInterface
{
/**
* Add a service provider to the aggregate.
*
* @param string|\WP_Rocket\Engine\Container\ServiceProvider\ServiceProviderInterface $provider
* @return $this
*/
public function add($provider);
/**
* Determines whether a service is provided by the aggregate.
*
* @param string $service
* @return boolean
*/
public function provides($service);
/**
* Invokes the register method of a provider that provides a specific service.
*
* @param string $service
* @return void
*/
public function register($service);
}

View File

@@ -0,0 +1,26 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
use WP_Rocket\Engine\Container\ContainerAwareInterface;
interface ServiceProviderInterface extends ContainerAwareInterface
{
/**
* Returns a boolean if checking whether this provider provides a specific
* service or returns an array of provided services if no argument passed.
*
* @param string $service
* @return boolean|array
*/
public function provides($service = null);
/**
* Use the register method to register items with the container via the
* protected $this->container property or the `getContainer` method
* from the ContainerAwareTrait.
*
* @return void
*/
public function register();
}

View File

@@ -0,0 +1,24 @@
<?php
namespace WP_Rocket\Engine\Container\ServiceProvider;
interface SignatureServiceProviderInterface
{
/**
* Set a custom signature for the service provider. This enables
* registering the same service provider multiple times.
*
* @param string $signature
* @return self
*/
public function withSignature($signature);
/**
* The signature of the service provider uniquely identifies it, so
* that we can quickly determine if it has already been registered.
* Defaults to get_class($provider).
*
* @return string
*/
public function getSignature();
}