init
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace MailPoetVendor\Symfony\Contracts\Service;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
interface ResetInterface
|
||||
{
|
||||
public function reset();
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace MailPoetVendor\Symfony\Contracts\Service;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
use MailPoetVendor\Psr\Container\ContainerExceptionInterface;
|
||||
use MailPoetVendor\Psr\Container\NotFoundExceptionInterface;
|
||||
// Help opcache.preload discover always-needed symbols
|
||||
\class_exists(ContainerExceptionInterface::class);
|
||||
\class_exists(NotFoundExceptionInterface::class);
|
||||
trait ServiceLocatorTrait
|
||||
{
|
||||
private $factories;
|
||||
private $loading = [];
|
||||
private $providedTypes;
|
||||
public function __construct(array $factories)
|
||||
{
|
||||
$this->factories = $factories;
|
||||
}
|
||||
public function has($id)
|
||||
{
|
||||
return isset($this->factories[$id]);
|
||||
}
|
||||
public function get($id)
|
||||
{
|
||||
if (!isset($this->factories[$id])) {
|
||||
throw $this->createNotFoundException($id);
|
||||
}
|
||||
if (isset($this->loading[$id])) {
|
||||
$ids = \array_values($this->loading);
|
||||
$ids = \array_slice($this->loading, \array_search($id, $ids));
|
||||
$ids[] = $id;
|
||||
throw $this->createCircularReferenceException($id, $ids);
|
||||
}
|
||||
$this->loading[$id] = $id;
|
||||
try {
|
||||
return $this->factories[$id]($this);
|
||||
} finally {
|
||||
unset($this->loading[$id]);
|
||||
}
|
||||
}
|
||||
public function getProvidedServices() : array
|
||||
{
|
||||
if (null === $this->providedTypes) {
|
||||
$this->providedTypes = [];
|
||||
foreach ($this->factories as $name => $factory) {
|
||||
if (!\is_callable($factory)) {
|
||||
$this->providedTypes[$name] = '?';
|
||||
} else {
|
||||
$type = (new \ReflectionFunction($factory))->getReturnType();
|
||||
$this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->providedTypes;
|
||||
}
|
||||
private function createNotFoundException(string $id) : NotFoundExceptionInterface
|
||||
{
|
||||
if (!($alternatives = \array_keys($this->factories))) {
|
||||
$message = 'is empty...';
|
||||
} else {
|
||||
$last = \array_pop($alternatives);
|
||||
if ($alternatives) {
|
||||
$message = \sprintf('only knows about the "%s" and "%s" services.', \implode('", "', $alternatives), $last);
|
||||
} else {
|
||||
$message = \sprintf('only knows about the "%s" service.', $last);
|
||||
}
|
||||
}
|
||||
if ($this->loading) {
|
||||
$message = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', \end($this->loading), $id, $message);
|
||||
} else {
|
||||
$message = \sprintf('Service "%s" not found: the current service locator %s', $id, $message);
|
||||
}
|
||||
return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface
|
||||
{
|
||||
};
|
||||
}
|
||||
private function createCircularReferenceException(string $id, array $path) : ContainerExceptionInterface
|
||||
{
|
||||
return new class(\sprintf('Circular reference detected for service "%s", path: "%s".', $id, \implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface
|
||||
{
|
||||
};
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
namespace MailPoetVendor\Symfony\Contracts\Service;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
||||
interface ServiceProviderInterface extends ContainerInterface
|
||||
{
|
||||
public function getProvidedServices() : array;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace MailPoetVendor\Symfony\Contracts\Service;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
interface ServiceSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedServices();
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace MailPoetVendor\Symfony\Contracts\Service;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
||||
trait ServiceSubscriberTrait
|
||||
{
|
||||
protected $container;
|
||||
public static function getSubscribedServices() : array
|
||||
{
|
||||
static $services;
|
||||
if (null !== $services) {
|
||||
return $services;
|
||||
}
|
||||
$services = \is_callable(['parent', __FUNCTION__]) ? parent::getSubscribedServices() : [];
|
||||
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
|
||||
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
|
||||
continue;
|
||||
}
|
||||
if (self::class !== $method->getDeclaringClass()->name) {
|
||||
continue;
|
||||
}
|
||||
if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) {
|
||||
continue;
|
||||
}
|
||||
if ($returnType->isBuiltin()) {
|
||||
continue;
|
||||
}
|
||||
$services[self::class . '::' . $method->name] = '?' . $returnType->getName();
|
||||
}
|
||||
return $services;
|
||||
}
|
||||
public function setContainer(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
if (\is_callable(['parent', __FUNCTION__])) {
|
||||
return parent::setContainer($container);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace MailPoetVendor\Symfony\Contracts\Service\Test;
|
||||
if (!defined('ABSPATH')) exit;
|
||||
use MailPoetVendor\PHPUnit\Framework\TestCase;
|
||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
||||
use MailPoetVendor\Symfony\Contracts\Service\ServiceLocatorTrait;
|
||||
abstract class ServiceLocatorTest extends TestCase
|
||||
{
|
||||
protected function getServiceLocator(array $factories)
|
||||
{
|
||||
return new class($factories) implements ContainerInterface
|
||||
{
|
||||
use ServiceLocatorTrait;
|
||||
};
|
||||
}
|
||||
public function testHas()
|
||||
{
|
||||
$locator = $this->getServiceLocator(['foo' => function () {
|
||||
return 'bar';
|
||||
}, 'bar' => function () {
|
||||
return 'baz';
|
||||
}, function () {
|
||||
return 'dummy';
|
||||
}]);
|
||||
$this->assertTrue($locator->has('foo'));
|
||||
$this->assertTrue($locator->has('bar'));
|
||||
$this->assertFalse($locator->has('dummy'));
|
||||
}
|
||||
public function testGet()
|
||||
{
|
||||
$locator = $this->getServiceLocator(['foo' => function () {
|
||||
return 'bar';
|
||||
}, 'bar' => function () {
|
||||
return 'baz';
|
||||
}]);
|
||||
$this->assertSame('bar', $locator->get('foo'));
|
||||
$this->assertSame('baz', $locator->get('bar'));
|
||||
}
|
||||
public function testGetDoesNotMemoize()
|
||||
{
|
||||
$i = 0;
|
||||
$locator = $this->getServiceLocator(['foo' => function () use(&$i) {
|
||||
++$i;
|
||||
return 'bar';
|
||||
}]);
|
||||
$this->assertSame('bar', $locator->get('foo'));
|
||||
$this->assertSame('bar', $locator->get('foo'));
|
||||
$this->assertSame(2, $i);
|
||||
}
|
||||
public function testThrowsOnUndefinedInternalService()
|
||||
{
|
||||
if (!$this->getExpectedException()) {
|
||||
$this->expectException(\MailPoetVendor\Psr\Container\NotFoundExceptionInterface::class);
|
||||
$this->expectExceptionMessage('The service "foo" has a dependency on a non-existent service "bar". This locator only knows about the "foo" service.');
|
||||
}
|
||||
$locator = $this->getServiceLocator(['foo' => function () use(&$locator) {
|
||||
return $locator->get('bar');
|
||||
}]);
|
||||
$locator->get('foo');
|
||||
}
|
||||
public function testThrowsOnCircularReference()
|
||||
{
|
||||
$this->expectException(\MailPoetVendor\Psr\Container\ContainerExceptionInterface::class);
|
||||
$this->expectExceptionMessage('Circular reference detected for service "bar", path: "bar -> baz -> bar".');
|
||||
$locator = $this->getServiceLocator(['foo' => function () use(&$locator) {
|
||||
return $locator->get('bar');
|
||||
}, 'bar' => function () use(&$locator) {
|
||||
return $locator->get('baz');
|
||||
}, 'baz' => function () use(&$locator) {
|
||||
return $locator->get('bar');
|
||||
}]);
|
||||
$locator->get('foo');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
@@ -0,0 +1 @@
|
||||
<?php
|
||||
Reference in New Issue
Block a user