From d5fb4e064d6cb211d5afa9941794c1ed4c9c62ce Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Tue, 24 Jul 2018 16:09:20 +0200 Subject: [PATCH] Add generic method to get the module name from a namespace --- src/CLI/Console/Generate/Domain/Repository.php | 11 +++-------- src/CLI/Console/GenerateCommand.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/CLI/Console/Generate/Domain/Repository.php b/src/CLI/Console/Generate/Domain/Repository.php index f257d24..dc1c1ea 100644 --- a/src/CLI/Console/Generate/Domain/Repository.php +++ b/src/CLI/Console/Generate/Domain/Repository.php @@ -32,16 +32,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->getTargetPhpVersion() ); - $matches = []; - preg_match( - '|^Backend\\\\Modules\\\\(.*)\\\\Domain\\\\|', - $repositoryClass->getClassName()->getNamespace(), - $matches - ); - if (\count($matches) === 2) { + $moduleName = $this->extractModuleName($repositoryClass->getClassName()->getNamespace()); + if ($moduleName instanceof ModuleName) { $this->generateService->generateFile( new Repositories( - new ModuleName($matches[1]), + $moduleName, [$repositoryClass->getClassName()->getForUseStatement()], true ), diff --git a/src/CLI/Console/GenerateCommand.php b/src/CLI/Console/GenerateCommand.php index f0b212d..bbae847 100644 --- a/src/CLI/Console/GenerateCommand.php +++ b/src/CLI/Console/GenerateCommand.php @@ -3,6 +3,8 @@ namespace ModuleGenerator\CLI\Console; use ModuleGenerator\CLI\Service\Generate\Generate; +use ModuleGenerator\PhpGenerator\ModuleName\ModuleName; +use ModuleGenerator\PhpGenerator\PhpNamespace\PhpNamespace; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -59,4 +61,20 @@ public static function getOutput() { return self::$output; } + + public function extractModuleName(PhpNamespace $namespace): ?ModuleName + { + $matches = []; + preg_match( + '|^Backend\\\\Modules\\\\(.*?)\\\\|', + $namespace, + $matches + ); + + if (\count($matches) === 2) { + return new ModuleName($matches[1]); + } + + return null; + } }