add wp-rocket
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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 = []);
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
@@ -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);
|
||||
}
|
Reference in New Issue
Block a user