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