| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Contracts\Service; |
| 13 |
|
| 14 |
use Psr\Container\ContainerInterface; |
| 15 |
use Symfony\Contracts\Service\Attribute\SubscribedService; |
| 16 |
|
| 17 |
/** |
| 18 |
* Implementation of ServiceSubscriberInterface that determines subscribed services from |
| 19 |
* method return types. Service ids are available as "ClassName::methodName". |
| 20 |
* |
| 21 |
* @author Kevin Bond <[email protected]> |
| 22 |
*/ |
| 23 |
trait ServiceSubscriberTrait |
| 24 |
{ |
| 25 |
/** @var ContainerInterface */ |
| 26 |
protected $container; |
| 27 |
|
| 28 |
/** |
| 29 |
* {@inheritdoc} |
| 30 |
*/ |
| 31 |
public static function getSubscribedServices(): array |
| 32 |
{ |
| 33 |
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; |
| 34 |
$attributeOptIn = false; |
| 35 |
|
| 36 |
if (\PHP_VERSION_ID >= 80000) { |
| 37 |
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 38 |
if (self::class !== $method->getDeclaringClass()->name) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 47 |
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)); |
| 48 |
} |
| 49 |
|
| 50 |
if (!$returnType = $method->getReturnType()) { |
| 51 |
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class)); |
| 52 |
} |
| 53 |
|
| 54 |
$serviceId = $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; |
| 55 |
|
| 56 |
if ($returnType->allowsNull()) { |
| 57 |
$serviceId = '?'.$serviceId; |
| 58 |
} |
| 59 |
|
| 60 |
$services[$attribute->newInstance()->key ?? self::class.'::'.$method->name] = $serviceId; |
| 61 |
$attributeOptIn = true; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if (!$attributeOptIn) { |
| 66 |
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { |
| 67 |
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
if (self::class !== $method->getDeclaringClass()->name) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
if ($returnType->isBuiltin()) { |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
if (\PHP_VERSION_ID >= 80000) { |
| 84 |
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); |
| 85 |
} |
| 86 |
|
| 87 |
$services[self::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return $services; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @required |
| 96 |
* |
| 97 |
* @return ContainerInterface|null |
| 98 |
*/ |
| 99 |
public function setContainer(ContainerInterface $container) |
| 100 |
{ |
| 101 |
$this->container = $container; |
| 102 |
|
| 103 |
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) { |
| 104 |
return parent::setContainer($container); |
| 105 |
} |
| 106 |
|
| 107 |
return null; |
| 108 |
} |
| 109 |
} |
| 110 |
|