<?php
namespace App\Contracts\Service;
use Symfony\Contracts\Service\Attribute\SubscribedService;
/**
* TODO: this is a copy of another file as the signatures are not compatible; see if this is fixed in later updates...
*/
trait CustomServiceSubscriberTrait
{
/**
* {@inheritdoc}
*/
public static function getSubscribedServices(): array
{
$parent = get_parent_class(static::class);
$services = ($parent && method_exists($parent, __FUNCTION__)) ? call_user_func([$parent, 'getSubscribedServices']) : [];
$attributeOptIn = false;
if (\PHP_VERSION_ID >= 80000) {
foreach ((new \ReflectionClass(static::class))->getMethods() as $method) {
if (static::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 ?? static::class.'::'.$method->name] = $serviceId;
$attributeOptIn = true;
}
}
if (!$attributeOptIn) {
foreach ((new \ReflectionClass(static::class))->getMethods() as $method) {
if ($method->isStatic() || $method->isAbstract() || $method->isGenerator() || $method->isInternal() || $method->getNumberOfRequiredParameters()) {
continue;
}
if (static::class !== $method->getDeclaringClass()->name) {
continue;
}
if (!($returnType = $method->getReturnType()) instanceof \ReflectionNamedType) {
continue;
}
if ($returnType->isBuiltin()) {
continue;
}
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.', CustomServiceSubscriberTrait::class, self::class, SubscribedService::class);
}
$services[static::class.'::'.$method->name] = '?'.($returnType instanceof \ReflectionNamedType ? $returnType->getName() : $returnType);
}
}
return $services;
}
}