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