-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Contracts] Rename ServiceSubscriberTrait to ServiceMethodsSubscriberTrait #54496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabpot
merged 1 commit into
symfony:7.1
from
nicolas-grekas:service-methods-subscriber-trait
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,15 @@ | |
|
||
namespace Symfony\Contracts\Service\Attribute; | ||
|
||
use Symfony\Contracts\Service\ServiceMethodsSubscriberTrait; | ||
use Symfony\Contracts\Service\ServiceSubscriberInterface; | ||
use Symfony\Contracts\Service\ServiceSubscriberTrait; | ||
|
||
/** | ||
* For use as the return value for {@see ServiceSubscriberInterface}. | ||
* | ||
* @example new SubscribedService('http_client', HttpClientInterface::class, false, new Target('githubApi')) | ||
* | ||
* Use with {@see ServiceSubscriberTrait} to mark a method's return type | ||
* Use with {@see ServiceMethodsSubscriberTrait} to mark a method's return type | ||
* as a subscribed service. | ||
* | ||
* @author Kevin Bond <[email protected]> | ||
|
80 changes: 80 additions & 0 deletions
80
src/Symfony/Contracts/Service/ServiceMethodsSubscriberTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
|
||
/** | ||
* Implementation of ServiceSubscriberInterface that determines subscribed services | ||
* from methods that have the #[SubscribedService] attribute. | ||
* | ||
* Service ids are available as "ClassName::methodName" so that the implementation | ||
* of subscriber methods can be just `return $this->container->get(__METHOD__);`. | ||
* | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
trait ServiceMethodsSubscriberTrait | ||
{ | ||
protected ContainerInterface $container; | ||
|
||
public static function getSubscribedServices(): array | ||
{ | ||
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; | ||
|
||
foreach ((new \ReflectionClass(self::class))->getMethods() as $method) { | ||
if (self::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)); | ||
} | ||
|
||
/* @var SubscribedService $attribute */ | ||
$attribute = $attribute->newInstance(); | ||
$attribute->key ??= self::class.'::'.$method->name; | ||
$attribute->type ??= $returnType instanceof \ReflectionNamedType ? $returnType->getName() : (string) $returnType; | ||
$attribute->nullable = $returnType->allowsNull(); | ||
|
||
if ($attribute->attributes) { | ||
$services[] = $attribute; | ||
} else { | ||
$services[$attribute->key] = ($attribute->nullable ? '?' : '').$attribute->type; | ||
} | ||
} | ||
|
||
return $services; | ||
} | ||
|
||
#[Required] | ||
public function setContainer(ContainerInterface $container): ?ContainerInterface | ||
{ | ||
$ret = null; | ||
if (method_exists(get_parent_class(self::class) ?: '', __FUNCTION__)) { | ||
$ret = parent::setContainer($container); | ||
} | ||
|
||
$this->container = $container; | ||
|
||
return $ret; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,17 +15,23 @@ | |
use Symfony\Contracts\Service\Attribute\Required; | ||
use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
|
||
trigger_deprecation('symfony/contracts', 'v3.5', '"%s" is deprecated, use "ServiceMethodsSubscriberTrait" instead.', ServiceSubscriberTrait::class); | ||
|
||
/** | ||
* Implementation of ServiceSubscriberInterface that determines subscribed services from | ||
* method return types. Service ids are available as "ClassName::methodName". | ||
* Implementation of ServiceSubscriberInterface that determines subscribed services | ||
* from methods that have the #[SubscribedService] attribute. | ||
* | ||
* Service ids are available as "ClassName::methodName" so that the implementation | ||
* of subscriber methods can be just `return $this->container->get(__METHOD__);`. | ||
* | ||
* @property ContainerInterface $container | ||
* | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @deprecated since symfony/contracts v3.5, use ServiceMethodsSubscriberTrait instead | ||
*/ | ||
trait ServiceSubscriberTrait | ||
{ | ||
/** @var ContainerInterface */ | ||
protected $container; | ||
|
||
public static function getSubscribedServices(): array | ||
{ | ||
$services = method_exists(get_parent_class(self::class) ?: '', __FUNCTION__) ? parent::getSubscribedServices() : []; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Contracts\Tests\Service; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Contracts\Service\Attribute\Required; | ||
use Symfony\Contracts\Service\Attribute\SubscribedService; | ||
use Symfony\Contracts\Service\ServiceSubscriberInterface; | ||
use Symfony\Contracts\Service\ServiceSubscriberTrait; | ||
|
||
class LegacyParentTestService | ||
{ | ||
public function aParentService(): Service1 | ||
{ | ||
} | ||
|
||
public function setContainer(ContainerInterface $container): ?ContainerInterface | ||
{ | ||
return $container; | ||
} | ||
} | ||
|
||
class LegacyTestService extends LegacyParentTestService implements ServiceSubscriberInterface | ||
{ | ||
use ServiceSubscriberTrait; | ||
|
||
#[SubscribedService] | ||
public function aService(): Service2 | ||
{ | ||
return $this->container->get(__METHOD__); | ||
} | ||
|
||
#[SubscribedService] | ||
public function nullableService(): ?Service2 | ||
{ | ||
return $this->container->get(__METHOD__); | ||
} | ||
|
||
#[SubscribedService(attributes: new Required())] | ||
public function withAttribute(): ?Service2 | ||
{ | ||
return $this->container->get(__METHOD__); | ||
} | ||
} | ||
|
||
class LegacyChildTestService extends LegacyTestService | ||
{ | ||
#[SubscribedService()] | ||
public function aChildService(): LegacyService3 | ||
{ | ||
return $this->container->get(__METHOD__); | ||
} | ||
} | ||
|
||
class LegacyParentWithMagicCall | ||
{ | ||
public function __call($method, $args) | ||
{ | ||
throw new \BadMethodCallException('Should not be called.'); | ||
} | ||
|
||
public static function __callStatic($method, $args) | ||
{ | ||
throw new \BadMethodCallException('Should not be called.'); | ||
} | ||
} | ||
|
||
class LegacyService3 | ||
{ | ||
} | ||
|
||
class LegacyParentTestService2 | ||
{ | ||
/** @var ContainerInterface */ | ||
protected $container; | ||
|
||
public function setContainer(ContainerInterface $container) | ||
{ | ||
$previous = $this->container ?? null; | ||
$this->container = $container; | ||
|
||
return $previous; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.