-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[make:decorator] Add new maker to create decorator
- Loading branch information
Showing
39 changed files
with
1,733 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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,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. |
This file contains 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
34 changes: 34 additions & 0 deletions
34
src/DependencyInjection/CompilerPass/MakeDecoratorPass.php
This file contains 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,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle 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\Bundle\MakerBundle\DependencyInjection\CompilerPass; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Benjamin Georgeault <[email protected]> | ||
*/ | ||
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) | ||
; | ||
} | ||
} |
This file contains 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,141 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle 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\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 <[email protected]> | ||
*/ | ||
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')) | ||
; | ||
} | ||
|
||
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)) | ||
->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, | ||
); | ||
} | ||
} |
This file contains 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 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 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,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony MakerBundle 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\Bundle\MakerBundle\Util\ClassSource\Model; | ||
|
||
/** | ||
* @author Benjamin Georgeault <[email protected]> | ||
* | ||
* @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)); | ||
} | ||
} |
Oops, something went wrong.