package and depencies
This commit is contained in:
3
vendor/symfony/service-contracts/.gitignore
vendored
3
vendor/symfony/service-contracts/.gitignore
vendored
@@ -1,3 +0,0 @@
|
||||
vendor/
|
||||
composer.lock
|
||||
phpunit.xml
|
@@ -11,9 +11,14 @@
|
||||
|
||||
namespace Symfony\Contracts\Service\Attribute;
|
||||
|
||||
use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
||||
use Symfony\Contracts\Service\ServiceSubscriberTrait;
|
||||
|
||||
/**
|
||||
* For use as the return value for {@see ServiceSubscriberInterface}.
|
||||
*
|
||||
* @example new SubscribedService('http_client', HttpClientInterface::class, false, new Target('githubApi'))
|
||||
*
|
||||
* Use with {@see ServiceSubscriberTrait} to mark a method's return type
|
||||
* as a subscribed service.
|
||||
*
|
||||
@@ -22,12 +27,21 @@ use Symfony\Contracts\Service\ServiceSubscriberTrait;
|
||||
#[\Attribute(\Attribute::TARGET_METHOD)]
|
||||
final class SubscribedService
|
||||
{
|
||||
/** @var object[] */
|
||||
public array $attributes;
|
||||
|
||||
/**
|
||||
* @param string|null $key The key to use for the service
|
||||
* If null, use "ClassName::methodName"
|
||||
* @param string|null $key The key to use for the service
|
||||
* @param class-string|null $type The service class
|
||||
* @param bool $nullable Whether the service is optional
|
||||
* @param object|object[] $attributes One or more dependency injection attributes to use
|
||||
*/
|
||||
public function __construct(
|
||||
public ?string $key = null
|
||||
public ?string $key = null,
|
||||
public ?string $type = null,
|
||||
public bool $nullable = false,
|
||||
array|object $attributes = [],
|
||||
) {
|
||||
$this->attributes = \is_array($attributes) ? $attributes : [$attributes];
|
||||
}
|
||||
}
|
||||
|
@@ -26,9 +26,9 @@ class_exists(NotFoundExceptionInterface::class);
|
||||
*/
|
||||
trait ServiceLocatorTrait
|
||||
{
|
||||
private $factories;
|
||||
private $loading = [];
|
||||
private $providedTypes;
|
||||
private array $factories;
|
||||
private array $loading = [];
|
||||
private array $providedTypes;
|
||||
|
||||
/**
|
||||
* @param callable[] $factories
|
||||
@@ -38,22 +38,12 @@ trait ServiceLocatorTrait
|
||||
$this->factories = $factories;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $id)
|
||||
public function has(string $id): bool
|
||||
{
|
||||
return isset($this->factories[$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $id)
|
||||
public function get(string $id): mixed
|
||||
{
|
||||
if (!isset($this->factories[$id])) {
|
||||
throw $this->createNotFoundException($id);
|
||||
@@ -75,12 +65,9 @@ trait ServiceLocatorTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getProvidedServices(): array
|
||||
{
|
||||
if (null === $this->providedTypes) {
|
||||
if (!isset($this->providedTypes)) {
|
||||
$this->providedTypes = [];
|
||||
|
||||
foreach ($this->factories as $name => $factory) {
|
||||
|
@@ -18,9 +18,18 @@ use Psr\Container\ContainerInterface;
|
||||
*
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
* @author Mateusz Sip <mateusz.sip@gmail.com>
|
||||
*
|
||||
* @template T of mixed
|
||||
*/
|
||||
interface ServiceProviderInterface extends ContainerInterface
|
||||
{
|
||||
/**
|
||||
* @return T
|
||||
*/
|
||||
public function get(string $id): mixed;
|
||||
|
||||
public function has(string $id): bool;
|
||||
|
||||
/**
|
||||
* Returns an associative array of service types keyed by the identifiers provided by the current container.
|
||||
*
|
||||
|
@@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Contracts\Service;
|
||||
|
||||
use Symfony\Contracts\Service\Attribute\SubscribedService;
|
||||
|
||||
/**
|
||||
* A ServiceSubscriber exposes its dependencies via the static {@link getSubscribedServices} method.
|
||||
*
|
||||
@@ -29,7 +31,8 @@ namespace Symfony\Contracts\Service;
|
||||
interface ServiceSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* Returns an array of service types required by such instances, optionally keyed by the service names used internally.
|
||||
* Returns an array of service types (or {@see SubscribedService} objects) required
|
||||
* by such instances, optionally keyed by the service names used internally.
|
||||
*
|
||||
* For mandatory dependencies:
|
||||
*
|
||||
@@ -47,7 +50,13 @@ interface ServiceSubscriberInterface
|
||||
* * ['?Psr\Log\LoggerInterface'] is a shortcut for
|
||||
* * ['Psr\Log\LoggerInterface' => '?Psr\Log\LoggerInterface']
|
||||
*
|
||||
* @return string[] The required service types, optionally keyed by service names
|
||||
* additionally, an array of {@see SubscribedService}'s can be returned:
|
||||
*
|
||||
* * [new SubscribedService('logger', Psr\Log\LoggerInterface::class)]
|
||||
* * [new SubscribedService(type: Psr\Log\LoggerInterface::class, nullable: true)]
|
||||
* * [new SubscribedService('http_client', HttpClientInterface::class, attributes: new Target('githubApi'))]
|
||||
*
|
||||
* @return string[]|SubscribedService[] The required service types, optionally keyed by service names
|
||||
*/
|
||||
public static function getSubscribedServices();
|
||||
public static function getSubscribedServices(): array;
|
||||
}
|
||||
|
@@ -12,6 +12,7 @@
|
||||
namespace Symfony\Contracts\Service;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Contracts\Service\Attribute\Required;
|
||||
use Symfony\Contracts\Service\Attribute\SubscribedService;
|
||||
|
||||
/**
|
||||
@@ -25,78 +26,45 @@ trait ServiceSubscriberTrait
|
||||
/** @var ContainerInterface */
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedServices(): array
|
||||
{
|
||||
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
|
||||
$attributeOptIn = false;
|
||||
|
||||
if (\PHP_VERSION_ID >= 80000) {
|
||||
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
|
||||
if (self::class !== $method->getDeclaringClass()->name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
|
||||
throw new \LogicException(sprintf('Cannot use "%s" on method "%s::%s()" (can only be used on non-static, non-abstract methods with no parameters).', SubscribedService::class, self::class, $method->name));
|
||||
}
|
||||
|
||||
if (!$returnType = $method->getReturnType()) {
|
||||
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
|
||||
}
|
||||
|
||||
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
|
||||
|
||||
if ($returnType->allowsNull()) {
|
||||
$serviceId = '?'.$serviceId;
|
||||
}
|
||||
|
||||
$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId;
|
||||
$attributeOptIn = true;
|
||||
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
|
||||
if (self::class !== $method->getDeclaringClass()->name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$attributeOptIn) {
|
||||
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
|
||||
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
|
||||
continue;
|
||||
}
|
||||
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (self::class !== $method->getDeclaringClass()->name) {
|
||||
continue;
|
||||
}
|
||||
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
|
||||
throw new \LogicException(sprintf('Cannot use "%s" on method "%s::%s()" (can only be used on non-static, non-abstract methods with no parameters).', SubscribedService::class, self::class, $method->name));
|
||||
}
|
||||
|
||||
if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) {
|
||||
continue;
|
||||
}
|
||||
if (!$returnType = $method->getReturnType()) {
|
||||
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
|
||||
}
|
||||
|
||||
if ($returnType->isBuiltin()) {
|
||||
continue;
|
||||
}
|
||||
/* @var SubscribedService $attribute */
|
||||
$attribute = $attribute->newInstance();
|
||||
$attribute->key ??= self::class.'::'.$method->name;
|
||||
$attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
|
||||
$attribute->nullable = $returnType->allowsNull();
|
||||
|
||||
if (\PHP_VERSION_ID >= 80000) {
|
||||
trigger_deprecation('symfony/service-contracts', '2.5', 'Using "%s" in "%s" without using the "%s" attribute on any method is deprecated.', ServiceSubscriberTrait::class, self::class, SubscribedService::class);
|
||||
}
|
||||
|
||||
$services[self::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType);
|
||||
if ($attribute->attributes) {
|
||||
$services[] = $attribute;
|
||||
} else {
|
||||
$services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type;
|
||||
}
|
||||
}
|
||||
|
||||
return $services;
|
||||
}
|
||||
|
||||
/**
|
||||
* @required
|
||||
*
|
||||
* @return ContainerInterface|null
|
||||
*/
|
||||
public function setContainer(ContainerInterface $container)
|
||||
#[Required]
|
||||
public function setContainer(ContainerInterface $container): ?ContainerInterface
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
|
@@ -17,10 +17,7 @@ use Symfony\Contracts\Service\ServiceLocatorTrait;
|
||||
|
||||
abstract class ServiceLocatorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return ContainerInterface
|
||||
*/
|
||||
protected function getServiceLocator(array $factories)
|
||||
protected function getServiceLocator(array $factories): ContainerInterface
|
||||
{
|
||||
return new class($factories) implements ContainerInterface {
|
||||
use ServiceLocatorTrait;
|
||||
|
12
vendor/symfony/service-contracts/composer.json
vendored
12
vendor/symfony/service-contracts/composer.json
vendored
@@ -16,9 +16,8 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=7.2.5",
|
||||
"psr/container": "^1.1",
|
||||
"symfony/deprecation-contracts": "^2.1|^3"
|
||||
"php": ">=8.1",
|
||||
"psr/container": "^2.0"
|
||||
},
|
||||
"conflict": {
|
||||
"ext-psr": "<1.1|>=2"
|
||||
@@ -27,12 +26,15 @@
|
||||
"symfony/service-implementation": ""
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Symfony\\Contracts\\Service\\": "" }
|
||||
"psr-4": { "Symfony\\Contracts\\Service\\": "" },
|
||||
"exclude-from-classmap": [
|
||||
"/Test/"
|
||||
]
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-main": "2.5-dev"
|
||||
"dev-main": "3.3-dev"
|
||||
},
|
||||
"thanks": {
|
||||
"name": "symfony/contracts",
|
||||
|
Reference in New Issue
Block a user