Skip to content

Commit

Permalink
Add generic method to get the module name from a namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
carakas committed Jul 24, 2018
1 parent 72c138f commit d5fb4e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/CLI/Console/Generate/Domain/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down
18 changes: 18 additions & 0 deletions src/CLI/Console/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit d5fb4e0

Please sign in to comment.