From 5d7813036d9953bdbea7ff6ba225b75e05d1cbaf Mon Sep 17 00:00:00 2001 From: janvt Date: Fri, 10 Mar 2023 12:26:23 +0100 Subject: [PATCH 1/2] add maker command for command entity / handler --- config/services.yaml | 5 + src/Maker/AbstractBaseMakeQueryCommand.php | 148 ++++++++++++++++++ src/Maker/MakeCommand.php | 61 ++++++++ src/Maker/MakeQuery.php | 113 ++----------- .../skeleton/command/Command.tpl.php | 9 ++ .../skeleton/command/CommandHandler.tpl.php | 13 ++ 6 files changed, 251 insertions(+), 98 deletions(-) create mode 100644 src/Maker/AbstractBaseMakeQueryCommand.php create mode 100644 src/Maker/MakeCommand.php create mode 100644 src/Resources/skeleton/command/Command.tpl.php create mode 100644 src/Resources/skeleton/command/CommandHandler.tpl.php diff --git a/config/services.yaml b/config/services.yaml index 9873c17..48126f2 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -30,6 +30,11 @@ services: tags: - { name: maker.command } + GeekCell\DddBundle\Maker\MakeCommand: + class: GeekCell\DddBundle\Maker\MakeCommand + tags: + - { name: maker.command } + GeekCell\DddBundle\Maker\Doctrine\DoctrineConfigUpdater: class: GeekCell\DddBundle\Maker\Doctrine\DoctrineConfigUpdater public: false diff --git a/src/Maker/AbstractBaseMakeQueryCommand.php b/src/Maker/AbstractBaseMakeQueryCommand.php new file mode 100644 index 0000000..44767dd --- /dev/null +++ b/src/Maker/AbstractBaseMakeQueryCommand.php @@ -0,0 +1,148 @@ +getTarget()); + } + + /** + * @return string + */ + function getNamespacePrefix(): string + { + return 'Application\\' . $this->getClassSuffix() . '\\'; + } + + /** + * @inheritDoc + */ + public function configureCommand(Command $command, InputConfiguration $inputConfig): void + { + $command + ->addArgument( + 'name', + InputArgument::REQUIRED, + 'The name of the ' . $this->getTarget() . ' class (e.g. Customer)', + ) + ; + } + + /** + * @inheritDoc + */ + public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null): void + { + } + + /** + * @inheritDoc + */ + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void + { + } + + /** + * @inheritDoc + */ + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void + { + $entityClassNameDetails = $generator->createClassNameDetails( + $input->getArgument('name'), + $this->getNamespacePrefix(), + $this->getClassSuffix(), + ); + + $this->generateEntity($entityClassNameDetails, $generator); + $this->generateHandler($entityClassNameDetails, $generator); + + $this->writeSuccessMessage($io); + } + + /** + * @param ClassNameDetails $queryClassNameDetails + * @param Generator $generator + * @return void + */ + private function generateEntity(ClassNameDetails $queryClassNameDetails, Generator $generator): void + { + $templateVars = [ + 'use_statements' => new UseStatementGenerator($this->getEntityUseStatements()), + ]; + + $templatePath = __DIR__."/../Resources/skeleton/{$this->getTarget()}/{$this->getClassSuffix()}.tpl.php"; + $generator->generateClass( + $queryClassNameDetails->getFullName(), + $templatePath, + $templateVars, + ); + + $generator->writeChanges(); + } + + /** + * @param ClassNameDetails $queryClassNameDetails + * @param Generator $generator + * @return void + */ + private function generateHandler(ClassNameDetails $queryClassNameDetails, Generator $generator): void + { + $classNameDetails = $generator->createClassNameDetails( + $queryClassNameDetails->getShortName(), + $this->getNamespacePrefix(), + 'Handler', + ); + + $templateVars = [ + 'use_statements' => new UseStatementGenerator($this->getEntityHandlerUseStatements()), + 'query_class_name' => $queryClassNameDetails->getShortName() + ]; + + $templatePath = __DIR__."/../Resources/skeleton/{$this->getTarget()}/{$this->getClassSuffix()}Handler.tpl.php"; + $generator->generateClass( + $classNameDetails->getFullName(), + $templatePath, + $templateVars, + ); + + $generator->writeChanges(); + } +} diff --git a/src/Maker/MakeCommand.php b/src/Maker/MakeCommand.php new file mode 100644 index 0000000..0ff7204 --- /dev/null +++ b/src/Maker/MakeCommand.php @@ -0,0 +1,61 @@ +addArgument( - 'name', - InputArgument::REQUIRED, - 'The name of the query class (e.g. Customer)', - ) - ; - } - - /** - * @inheritDoc - */ - public function configureDependencies(DependencyBuilder $dependencies, InputInterface $input = null): void - { + return 'Creates a new ' . self::TARGET . ' class and handler'; } /** * @inheritDoc */ - public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void + function getTarget(): string { + return self::TARGET; } /** * @inheritDoc */ - public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void + function getEntityUseStatements(): array { - $queryClassNameDetails = $generator->createClassNameDetails( - $input->getArgument('name'), - NAMESPACE_PREFIX, - 'Query', - ); - - $this->generateQuery($queryClassNameDetails, $generator); - $this->generateQueryHandler($queryClassNameDetails, $generator); - - $this->writeSuccessMessage($io); - } - - /** - * @param ClassNameDetails $queryClassNameDetails - * @param Generator $generator - * @return void - */ - private function generateQuery(ClassNameDetails $queryClassNameDetails, Generator $generator): void - { - $templateVars = [ - 'use_statements' => new UseStatementGenerator([ - Query::class, - ]), + return [ + Query::class ]; - - $templatePath = __DIR__.'/../Resources/skeleton/query/Query.tpl.php'; - $generator->generateClass( - $queryClassNameDetails->getFullName(), - $templatePath, - $templateVars, - ); - - $generator->writeChanges(); } /** - * @param ClassNameDetails $queryClassNameDetails - * @param Generator $generator - * @return void + * @inheritDoc */ - private function generateQueryHandler(ClassNameDetails $queryClassNameDetails, Generator $generator): void + function getEntityHandlerUseStatements(): array { - $classNameDetails = $generator->createClassNameDetails( - $queryClassNameDetails->getShortName(), - NAMESPACE_PREFIX, - 'Handler', - ); - - $templateVars = [ - 'use_statements' => new UseStatementGenerator([ - QueryHandler::class, - Collection::class, - AsMessageHandler::class - ]), - 'query_class_name' => $queryClassNameDetails->getShortName() + return [ + QueryHandler::class, + Collection::class, + AsMessageHandler::class ]; - - $templatePath = __DIR__.'/../Resources/skeleton/query/QueryHandler.tpl.php'; - $generator->generateClass( - $classNameDetails->getFullName(), - $templatePath, - $templateVars, - ); - - $generator->writeChanges(); } } diff --git a/src/Resources/skeleton/command/Command.tpl.php b/src/Resources/skeleton/command/Command.tpl.php new file mode 100644 index 0000000..1ee97fe --- /dev/null +++ b/src/Resources/skeleton/command/Command.tpl.php @@ -0,0 +1,9 @@ + + +namespace ; + + + +class implements Command +{ +} diff --git a/src/Resources/skeleton/command/CommandHandler.tpl.php b/src/Resources/skeleton/command/CommandHandler.tpl.php new file mode 100644 index 0000000..8725a84 --- /dev/null +++ b/src/Resources/skeleton/command/CommandHandler.tpl.php @@ -0,0 +1,13 @@ + + +namespace ; + + + +#[AsMessageHandler] +class implements CommandHandler +{ + public function __invoke( $query): Collection + { + } +} From 6f2e293047ef1fa4744817b8bd8970779e44efa1 Mon Sep 17 00:00:00 2001 From: janvt Date: Fri, 10 Mar 2023 15:12:58 +0100 Subject: [PATCH 2/2] change command return type to void --- src/Maker/MakeCommand.php | 1 - src/Resources/skeleton/command/CommandHandler.tpl.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Maker/MakeCommand.php b/src/Maker/MakeCommand.php index 0ff7204..bdaa81f 100644 --- a/src/Maker/MakeCommand.php +++ b/src/Maker/MakeCommand.php @@ -54,7 +54,6 @@ function getEntityHandlerUseStatements(): array { return [ CommandHandler::class, - Collection::class, AsMessageHandler::class ]; } diff --git a/src/Resources/skeleton/command/CommandHandler.tpl.php b/src/Resources/skeleton/command/CommandHandler.tpl.php index 8725a84..eed3eed 100644 --- a/src/Resources/skeleton/command/CommandHandler.tpl.php +++ b/src/Resources/skeleton/command/CommandHandler.tpl.php @@ -7,7 +7,7 @@ #[AsMessageHandler] class implements CommandHandler { - public function __invoke( $query): Collection + public function __invoke( $query): void { } }