-
-
Notifications
You must be signed in to change notification settings - Fork 404
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
[make:decorator] Add new maker to create decorator #1613
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
The <info>%command.name%</info> command generates a new service decorator class. | ||
|
||
<info>php %command.full_name%</info> | ||
<info>php %command.full_name% My\Decorated\Service\Class</info> | ||
<info>php %command.full_name% My\Decorated\Service\Class MyServiceDecorator</info> | ||
<info>php %command.full_name% My\Decorated\Service\Class Service\MyServiceDecorator</info> | ||
<info>php %command.full_name% my_decorated.service.id MyServiceDecorator</info> | ||
<info>php %command.full_name% my_decorated.service.id MyServiceDecorator</info> | ||
|
||
If one argument is missing, the command will ask for it interactively. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Benjamin Georgeault <git@wedgesama.fr> | ||
*/ | ||
class MakeDecoratorPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasDefinition('maker.maker.make_decorator')) { | ||
return; | ||
} | ||
|
||
$container->getDefinition('maker.maker.make_decorator') | ||
->replaceArgument(0, ServiceLocatorTagPass::register($container, $ids = $container->getServiceIds())) | ||
->replaceArgument(1, $ids) | ||
; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Maker; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Bundle\MakerBundle\ConsoleStyle; | ||
use Symfony\Bundle\MakerBundle\DependencyBuilder; | ||
use Symfony\Bundle\MakerBundle\Generator; | ||
use Symfony\Bundle\MakerBundle\InputConfiguration; | ||
use Symfony\Bundle\MakerBundle\Str; | ||
use Symfony\Bundle\MakerBundle\Util\DecoratorInfo; | ||
use Symfony\Bundle\MakerBundle\Validator; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
use Symfony\Component\DependencyInjection\Attribute\AsDecorator; | ||
|
||
/** | ||
* @author Benjamin Georgeault <git@wedgesama.fr> | ||
*/ | ||
final class MakeDecorator extends AbstractMaker | ||
{ | ||
/** | ||
* @param array<string> $ids | ||
*/ | ||
public function __construct( | ||
private readonly ContainerInterface $container, | ||
private readonly array $ids, | ||
) { | ||
} | ||
|
||
public static function getCommandName(): string | ||
{ | ||
return 'make:decorator'; | ||
} | ||
|
||
public static function getCommandDescription(): string | ||
{ | ||
return 'Create CRUD for Doctrine entity class'; | ||
} | ||
|
||
public function configureCommand(Command $command, InputConfiguration $inputConfig): void | ||
{ | ||
$command | ||
->addArgument('id', InputArgument::OPTIONAL, 'The ID of the service to decorate.') | ||
->addArgument('decorator-class', InputArgument::OPTIONAL, \sprintf('The class name of the service to create (e.g. <fg=yellow>%sDecorator</>)', Str::asClassName(Str::getRandomTerm()))) | ||
->setHelp($this->getHelpFileContents('MakeDecorator.txt')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
; | ||
} | ||
|
||
public function configureDependencies(DependencyBuilder $dependencies): void | ||
{ | ||
$dependencies->addClassDependency( | ||
AsDecorator::class, | ||
'dependency-injection', | ||
); | ||
} | ||
|
||
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void | ||
{ | ||
// Ask for service id. | ||
if (null === $input->getArgument('id')) { | ||
$argument = $command->getDefinition()->getArgument('id'); | ||
|
||
($question = new Question($argument->getDescription())) | ||
->setAutocompleterValues($this->ids) | ||
->setValidator(fn ($answer) => Validator::serviceExists($answer, $this->ids)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. based on the issue I'd suggest that the command accept not only the id but a classname and tries to guess the id based on it (if the service does not exists for example) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can try something like that:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3 yes with fault tolerance maybe ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I also did guess from class/interface short name. |
||
->setMaxAttempts(3); | ||
|
||
$input->setArgument('id', $io->askQuestion($question)); | ||
} | ||
|
||
$id = $input->getArgument('id'); | ||
|
||
// Ask for decorator classname. | ||
if (null === $input->getArgument('decorator-class')) { | ||
$argument = $command->getDefinition()->getArgument('decorator-class'); | ||
|
||
$basename = Str::getShortClassName(match (true) { | ||
interface_exists($id) => Str::removeSuffix($id, 'Interface'), | ||
class_exists($id) => $id, | ||
default => Str::asClassName($id), | ||
}); | ||
|
||
$defaultClass = Str::asClassName(\sprintf('%s Decorator', $basename)); | ||
|
||
($question = new Question($argument->getDescription(), $defaultClass)) | ||
->setValidator(fn ($answer) => Validator::validateClassName(Validator::classDoesNotExist($answer))) | ||
->setMaxAttempts(3); | ||
|
||
$input->setArgument('decorator-class', $io->askQuestion($question)); | ||
} | ||
} | ||
|
||
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void | ||
{ | ||
$id = $input->getArgument('id'); | ||
|
||
$classNameDetails = $generator->createClassNameDetails( | ||
Validator::validateClassName(Validator::classDoesNotExist($input->getArgument('decorator-class'))), | ||
'', | ||
); | ||
|
||
$decoratedInfo = $this->createDecoratorInfo($id, $classNameDetails->getFullName()); | ||
$classData = $decoratedInfo->getClassData(); | ||
|
||
$generator->generateClassFromClassData( | ||
$classData, | ||
'decorator/Decorator.tpl.php', | ||
[ | ||
'decorated_info' => $decoratedInfo, | ||
], | ||
); | ||
|
||
$generator->writeChanges(); | ||
|
||
$this->writeSuccessMessage($io); | ||
} | ||
|
||
private function createDecoratorInfo(string $id, string $decoratorClass): DecoratorInfo | ||
{ | ||
return new DecoratorInfo( | ||
$decoratorClass, | ||
match (true) { | ||
class_exists($id), interface_exists($id) => $id, | ||
default => $this->container->get($id)::class, | ||
}, | ||
$id, | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\MakerBundle\Util\ClassSource\Model; | ||
|
||
/** | ||
* @author Benjamin Georgeault <git@wedgesama.fr> | ||
* | ||
* @internal | ||
*/ | ||
final class ClassMethod | ||
{ | ||
/** | ||
* @param MethodArgument[] $arguments | ||
*/ | ||
public function __construct( | ||
private readonly string $name, | ||
private readonly array $arguments = [], | ||
private readonly ?string $returnType = null, | ||
private readonly bool $isStatic = false, | ||
) { | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function isReturnVoid(): bool | ||
{ | ||
return 'void' === $this->returnType; | ||
} | ||
|
||
public function isStatic(): bool | ||
{ | ||
return $this->isStatic; | ||
} | ||
|
||
public function getDeclaration(): string | ||
{ | ||
return \sprintf('public %sfunction %s(%s)%s', | ||
$this->isStatic ? 'static ' : '', | ||
$this->name, | ||
implode(', ', array_map(fn (MethodArgument $arg) => $arg->getDeclaration(), $this->arguments)), | ||
$this->returnType ? ': '.$this->returnType : '', | ||
); | ||
} | ||
|
||
public function getArgumentsUse(): string | ||
{ | ||
return implode(', ', array_map(fn (MethodArgument $arg) => $arg->getVariable(), $this->arguments)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done