initial commit
This commit is contained in:
@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\ServiceProvider;
|
||||
|
||||
use Automattic\WooCommerce\Vendor\League\Container\ContainerAwareTrait;
|
||||
|
||||
abstract class AbstractServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $provides = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $identifier;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function provides(string $alias) : bool
|
||||
{
|
||||
return in_array($alias, $this->provides, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setIdentifier(string $id) : ServiceProviderInterface
|
||||
{
|
||||
$this->identifier = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getIdentifier() : string
|
||||
{
|
||||
return $this->identifier ?? get_class($this);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\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,106 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\ServiceProvider;
|
||||
|
||||
use Generator;
|
||||
use Automattic\WooCommerce\Vendor\League\Container\{ContainerAwareInterface, ContainerAwareTrait};
|
||||
use Automattic\WooCommerce\Vendor\League\Container\Exception\ContainerException;
|
||||
|
||||
class ServiceProviderAggregate implements ServiceProviderAggregateInterface
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var ServiceProviderInterface[]
|
||||
*/
|
||||
protected $providers = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $registered = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function add($provider) : ServiceProviderAggregateInterface
|
||||
{
|
||||
if (is_string($provider) && $this->getContainer()->has($provider)) {
|
||||
$provider = $this->getContainer()->get($provider);
|
||||
} elseif (is_string($provider) && class_exists($provider)) {
|
||||
$provider = new $provider;
|
||||
}
|
||||
|
||||
if (in_array($provider, $this->providers, true)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($provider instanceof ContainerAwareInterface) {
|
||||
$provider->setLeagueContainer($this->getLeagueContainer());
|
||||
}
|
||||
|
||||
if ($provider instanceof BootableServiceProviderInterface) {
|
||||
$provider->boot();
|
||||
}
|
||||
|
||||
if ($provider instanceof ServiceProviderInterface) {
|
||||
$this->providers[] = $provider;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
throw new ContainerException(
|
||||
'A service provider must be a fully qualified class name or instance ' .
|
||||
'of (\Automattic\WooCommerce\Vendor\League\Container\ServiceProvider\ServiceProviderInterface)'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function provides(string $service) : bool
|
||||
{
|
||||
foreach ($this->getIterator() as $provider) {
|
||||
if ($provider->provides($service)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getIterator() : Generator
|
||||
{
|
||||
$count = count($this->providers);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
yield $this->providers[$i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function register(string $service)
|
||||
{
|
||||
if (false === $this->provides($service)) {
|
||||
throw new ContainerException(
|
||||
sprintf('(%s) is not provided by a service provider', $service)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($this->getIterator() as $provider) {
|
||||
if (in_array($provider->getIdentifier(), $this->registered, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($provider->provides($service)) {
|
||||
$provider->register();
|
||||
$this->registered[] = $provider->getIdentifier();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\ServiceProvider;
|
||||
|
||||
use IteratorAggregate;
|
||||
use Automattic\WooCommerce\Vendor\League\Container\ContainerAwareInterface;
|
||||
|
||||
interface ServiceProviderAggregateInterface extends ContainerAwareInterface, IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* Add a service provider to the aggregate.
|
||||
*
|
||||
* @param string|ServiceProviderInterface $provider
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function add($provider) : ServiceProviderAggregateInterface;
|
||||
|
||||
/**
|
||||
* Determines whether a service is provided by the aggregate.
|
||||
*
|
||||
* @param string $service
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function provides(string $service) : bool;
|
||||
|
||||
/**
|
||||
* Invokes the register method of a provider that provides a specific service.
|
||||
*
|
||||
* @param string $service
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(string $service);
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Automattic\WooCommerce\Vendor\League\Container\ServiceProvider;
|
||||
|
||||
use Automattic\WooCommerce\Vendor\League\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
|
||||
*/
|
||||
public function provides(string $service) : bool;
|
||||
|
||||
/**
|
||||
* Use the register method to register items with the container via the
|
||||
* protected $this->leagueContainer property or the `getLeagueContainer` method
|
||||
* from the ContainerAwareTrait.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register();
|
||||
|
||||
/**
|
||||
* Set a custom id for the service provider. This enables
|
||||
* registering the same service provider multiple times.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setIdentifier(string $id) : ServiceProviderInterface;
|
||||
|
||||
/**
|
||||
* The id 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 getIdentifier() : string;
|
||||
}
|
Reference in New Issue
Block a user