| 148 |
lars |
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 |
/**
|
|
|
19 |
* Implementation of ServiceSubscriberInterface that determines subscribed services from
|
|
|
20 |
* method return types. Service ids are available as "ClassName::methodName".
|
|
|
21 |
*
|
|
|
22 |
* @author Kevin Bond <kevinbond@gmail.com>
|
|
|
23 |
*/
|
|
|
24 |
trait ServiceSubscriberTrait
|
|
|
25 |
{
|
|
|
26 |
/** @var ContainerInterface */
|
|
|
27 |
protected $container;
|
|
|
28 |
|
|
|
29 |
public static function getSubscribedServices(): array
|
|
|
30 |
{
|
|
|
31 |
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : [];
|
|
|
32 |
|
|
|
33 |
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) {
|
|
|
34 |
if (self::class !== $method->getDeclaringClass()->name) {
|
|
|
35 |
continue;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
if (!$attribute = $method->getAttributes(SubscribedService::class)[0] ?? null) {
|
|
|
39 |
continue;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
|
|
|
43 |
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));
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
if (!$returnType = $method->getReturnType()) {
|
|
|
47 |
throw new \LogicException(sprintf('Cannot use "%s" on methods without a return type in "%s::%s()".', SubscribedService::class, $method->name, self::class));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/* @var SubscribedService $attribute */
|
|
|
51 |
$attribute = $attribute->newInstance();
|
|
|
52 |
$attribute->key ??= self::class.'::'.$method->name;
|
|
|
53 |
$attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType;
|
|
|
54 |
$attribute->nullable = $returnType->allowsNull();
|
|
|
55 |
|
|
|
56 |
if ($attribute->attributes) {
|
|
|
57 |
$services[] = $attribute;
|
|
|
58 |
} else {
|
|
|
59 |
$services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
return $services;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
#[Required]
|
|
|
67 |
public function setContainer(ContainerInterface $container): ?ContainerInterface
|
|
|
68 |
{
|
| 688 |
lars |
69 |
$ret = null;
|
| 148 |
lars |
70 |
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) {
|
| 688 |
lars |
71 |
$ret = parent::setContainer($container);
|
| 148 |
lars |
72 |
}
|
|
|
73 |
|
| 688 |
lars |
74 |
$this->container = $container;
|
|
|
75 |
|
|
|
76 |
return $ret;
|
| 148 |
lars |
77 |
}
|
|
|
78 |
}
|