[make:decorator] Add new maker to create decorator - #1613
Conversation
c606c72 to
dbbc516
Compare
c815fab to
2fb5feb
Compare
…ity/onInvalid Also fix: - ignore `__construct` - fix static method on decorate by implements - Fix description
| } | ||
|
|
||
| $shortClass = Str::getShortClassName($class); | ||
| $shortNameMap[$shortClass][] = $id; |
There was a problem hiding this comment.
| $shortNameMap[$shortClass][] = $id; | |
| $shortNameMap[$shortClass] ??= []; | |
| $shortNameMap[$shortClass][] = $id; |
I know it works without but I d'ont find it very readable to not explictly create the array. Maybe it is acceptable in the maker bundle.
| $serviceClasses[$id] = $class; | ||
| } | ||
|
|
||
| $shortNameMap = array_map(array_unique(...), $shortNameMap); |
There was a problem hiding this comment.
Maybe you meant to use https://www.php.net/manual/en/function.array-walk.php instead ?
|
|
||
| public function getRealId(string $id): ?string | ||
| { | ||
| if (\in_array($id, $this->ids)) { |
There was a problem hiding this comment.
| if (\in_array($id, $this->ids)) { | |
| if (\in_array($id, $this->ids, true)) { |
| } else { | ||
| $input->setArgument( | ||
| 'id', | ||
| $id = $io->choice(\sprintf('Multiple services found for "%s", choice which one you want to decorate?', $id), $guessRealIds), |
There was a problem hiding this comment.
| $id = $io->choice(\sprintf('Multiple services found for "%s", choice which one you want to decorate?', $id), $guessRealIds), | |
| $id = $io->choice(\sprintf('Multiple services found for "%s", choose which one you want to decorate:', $id), $guessRealIds), |
| foreach ($ref->getConstants(\ReflectionClassConstant::IS_PUBLIC) as $name => $value) { | ||
| if ($onInvalid === $value) { | ||
| $this->onInvalid = \sprintf('ContainerInterface::%s', $name); | ||
| $ok = true; | ||
| break; | ||
| } | ||
| $allowedValues[] = $value; | ||
| } |
There was a problem hiding this comment.
should probably be a static list IMO. IT didn't changed for quite some time and won't be changed anytime soon I think. Keep it simple.
| } elseif ($parameter->isDefaultValueAvailable()) { | ||
| $defaultValue = $parameter->getDefaultValue(); | ||
|
|
||
| if (\is_string($defaultValue)) { |
| return implode('&', array_map($this->parseType(...), $type->getTypes())); | ||
| } | ||
|
|
||
| throw new RuntimeCommandException('Should never be reach.'); |
There was a problem hiding this comment.
| throw new RuntimeCommandException('Should never be reach.'); | |
| throw new RuntimeCommandException('Should never be reached.'); |
|
Thank you for this maker, it fits well within the project's scope for a feature that's genuinely tricky to get right by hand. Since this branch conflicts with the config changes merged into Feel free to have a look, and let me know if you'd rather continue on this branch instead. |
…geSama) This PR was merged into the 1.x branch. Discussion ---------- [make:decorator] Add new maker to create decorator Rebase and continuation of #1613 by `@WedgeSama`, updated for the current `1.x` codebase (config moved from XML to PHP, help files restructured) and with the remaining review feedback from `@Neirda24` applied: - Fixed the `getRealId()` strict comparison (`in_array` with strict mode). - Initialized the short name map entries explicitly instead of relying on implicit array creation, and replaced the array_map/array_unique callable-first-class syntax with a plain foreach. - Fixed the choice prompt wording ("choose which one you want to decorate:"). - Replaced the `ReflectionClass` lookup of `ContainerInterface` constants with a static map for the `onInvalid` option. - Added support for variadic parameters when parsing methods to decorate (previously produced an incorrect signature and a broken forwarding call). - Fixed a typo in an internal exception message. Add a `make:decorator` command that generates a decorator class for an existing service. The maker asks for the service to decorate and the class name of the decorator, then generates a class that either implements the decorated service interfaces or extends its class. ```shell bin/console make:decorator Symfony\Component\Routing\Generator\UrlGeneratorInterface MyUrlGenerator ``` ```php <?php namespace App; use Symfony\Component\DependencyInjection\Attribute\AsDecorator; use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RequestContext; #[AsDecorator(UrlGeneratorInterface::class)] final class MyUrlGenerator implements UrlGeneratorInterface { public function __construct( #[AutowireDecorated] private readonly UrlGeneratorInterface $inner, ) { } public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string { return $this->inner->generate($name, $parameters, $referenceType); } public function setContext(RequestContext $context): void { $this->inner->setContext($context); } public function getContext(): RequestContext { return $this->inner->getContext(); } } ``` Closes #1401 Commits ------- ca9ce46 [make:decorator] Add new maker to create decorator
|
Sorry, I was in holiday 😅 |
Add new maker to create decorator.
#1401
Allow to create new decorator for existing services.
It try first to decorate by implements:
If cannot implements, it will fallback to extends:
e.g.