fengketrade/vendor/symfony/service-contracts/ServiceSubscriberTrait.php

78 lines
2.5 KiB
PHP
Raw Normal View History

2025-10-16 21:07:43 +08:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Contracts\Service;
use Psr\Container\ContainerInterface;
2025-10-20 09:23:30 +08:00
use Symfony\Contracts\Service\Attribute\SubscribedService;
2025-10-16 21:07:43 +08:00
/**
2025-11-14 15:54:35 +08:00
* Implementation of ServiceSubscriberInterface that determines subscribed services from
* method return types. Service ids are available as "ClassName::methodName".
2025-10-16 21:07:43 +08:00
*
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait ServiceSubscriberTrait
{
2025-11-14 15:54:35 +08:00
/** @var ContainerInterface */
protected $container;
/**
* {@inheritdoc}
*/
2025-10-16 21:07:43 +08:00
public static function getSubscribedServices(): array
{
2025-10-20 09:23:30 +08:00
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
2025-10-16 21:07:43 +08:00
2025-10-20 09:23:30 +08:00
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
if (self::class !== $method->getDeclaringClass()->name) {
continue;
}
2025-10-16 21:07:43 +08:00
2025-10-20 09:23:30 +08:00
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
continue;
}
2025-10-16 21:07:43 +08:00
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
2025-11-14 15:54:35 +08:00
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));
2025-10-16 21:07:43 +08:00
}
2025-10-20 09:23:30 +08:00
if (!$returnType = $method->getReturnType()) {
2025-11-14 15:54:35 +08:00
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
2025-10-20 09:23:30 +08:00
}
2025-11-14 15:54:35 +08:00
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
2025-10-20 09:23:30 +08:00
2025-11-14 15:54:35 +08:00
if ($returnType->allowsNull()) {
$serviceId = '?'.$serviceId;
2025-10-16 21:07:43 +08:00
}
2025-11-14 15:54:35 +08:00
$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId;
2025-10-16 21:07:43 +08:00
}
return $services;
}
2025-11-14 15:54:35 +08:00
/**
* @required
*/
2025-10-20 09:23:30 +08:00
public function setContainer(ContainerInterface $container): ?ContainerInterface
2025-10-16 21:07:43 +08:00
{
2025-11-14 15:54:35 +08:00
$this->container = $container;
2025-10-20 09:23:30 +08:00
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
2025-11-14 15:54:35 +08:00
return parent::setContainer($container);
2025-10-20 09:23:30 +08:00
}
2025-11-14 15:54:35 +08:00
return null;
2025-10-16 21:07:43 +08:00
}
}