diff options
Diffstat (limited to 'vendor/symfony/service-contracts/ServiceSubscriberTrait.php')
-rw-r--r-- | vendor/symfony/service-contracts/ServiceSubscriberTrait.php | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/vendor/symfony/service-contracts/ServiceSubscriberTrait.php b/vendor/symfony/service-contracts/ServiceSubscriberTrait.php new file mode 100644 index 0000000..cc3bc32 --- /dev/null +++ b/vendor/symfony/service-contracts/ServiceSubscriberTrait.php | |||
@@ -0,0 +1,84 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
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\Required; | ||
16 | use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
17 | |||
18 | trigger_deprecation('symfony/contracts', 'v3.5', '"%s" is deprecated, use "ServiceMethodsSubscriberTrait" instead.', ServiceSubscriberTrait::class); | ||
19 | |||
20 | /** | ||
21 | * Implementation of ServiceSubscriberInterface that determines subscribed services | ||
22 | * from methods that have the #[SubscribedService] attribute. | ||
23 | * | ||
24 | * Service ids are available as "ClassName::methodName" so that the implementation | ||
25 | * of subscriber methods can be just `return $this->container->get(__METHOD__);`. | ||
26 | * | ||
27 | * @property ContainerInterface $container | ||
28 | * | ||
29 | * @author Kevin Bond <kevinbond@gmail.com> | ||
30 | * | ||
31 | * @deprecated since symfony/contracts v3.5, use ServiceMethodsSubscriberTrait instead | ||
32 | */ | ||
33 | trait ServiceSubscriberTrait | ||
34 | { | ||
35 | public static function getSubscribedServices(): array | ||
36 | { | ||
37 | $services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; | ||
38 | |||
39 | foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { | ||
40 | if (self::class !== $method->getDeclaringClass()->name) { | ||
41 | continue; | ||
42 | } | ||
43 | |||
44 | if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) { | ||
45 | continue; | ||
46 | } | ||
47 | |||
48 | if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) { | ||
49 | 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)); | ||
50 | } | ||
51 | |||
52 | if (!$returnType = $method->getReturnType()) { | ||
53 | throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class)); | ||
54 | } | ||
55 | |||
56 | /* @var SubscribedService $attribute */ | ||
57 | $attribute = $attribute->newInstance(); | ||
58 | $attribute->key ??= self::class.'::'.$method->name; | ||
59 | $attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; | ||
60 | $attribute->nullable = $returnType->allowsNull(); | ||
61 | |||
62 | if ($attribute->attributes) { | ||
63 | $services[] = $attribute; | ||
64 | } else { | ||
65 | $services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | return $services; | ||
70 | } | ||
71 | |||
72 | #[Required] | ||
73 | public function setContainer(ContainerInterface $container): ?ContainerInterface | ||
74 | { | ||
75 | $ret = null; | ||
76 | if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) { | ||
77 | $ret = parent::setContainer($container); | ||
78 | } | ||
79 | |||
80 | $this->container = $container; | ||
81 | |||
82 | return $ret; | ||
83 | } | ||
84 | } | ||