-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MakeModuleCommand + FilenameSanitizer
- Loading branch information
1 parent
726beac
commit 2ab151d
Showing
8 changed files
with
261 additions
and
39 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
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\CodeGenerator\Domain; | ||
|
||
use RuntimeException; | ||
|
||
final class FilenameSanitizer | ||
{ | ||
public const FACADE = 'Facade'; | ||
public const FACTORY = 'Factory'; | ||
public const CONFIG = 'Config'; | ||
public const DEPENDENCY_PROVIDER = 'DependencyProvider'; | ||
|
||
private const EXPECTED_FILENAMES = [ | ||
self::FACADE, | ||
self::FACTORY, | ||
self::CONFIG, | ||
self::DEPENDENCY_PROVIDER, | ||
]; | ||
|
||
public function sanitize(string $filename): string | ||
{ | ||
$percents = []; | ||
foreach (self::EXPECTED_FILENAMES as $expected) { | ||
$percents[$expected] = similar_text($expected, $filename, $percent); | ||
} | ||
|
||
$maxVal = max($percents); | ||
$maxValKeys = array_keys($percents, $maxVal, true); | ||
|
||
if (count($maxValKeys) > 1) { | ||
throw new RuntimeException(sprintf( | ||
'Which filename do you mean [%s]?', | ||
implode(' or ', $maxValKeys) | ||
)); | ||
} | ||
|
||
return reset($maxValKeys); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/CodeGenerator/Infrastructure/Command/MakeModuleCommand.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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gacela\CodeGenerator\Infrastructure\Command; | ||
|
||
use Gacela\CodeGenerator\Domain\CommandArgumentsParser; | ||
use Gacela\CodeGenerator\Domain\FileContentGenerator; | ||
use RuntimeException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use function in_array; | ||
use function json_encode; | ||
|
||
final class MakeModuleCommand extends Command | ||
{ | ||
private const FILENAMES = ['Facade', 'Factory', 'Config', 'DependencyProvider']; | ||
|
||
private CommandArgumentsParser $argumentsParser; | ||
private FileContentGenerator $fileContentGenerator; | ||
|
||
public function __construct( | ||
CommandArgumentsParser $argumentsParser, | ||
FileContentGenerator $fileContentGenerator | ||
) { | ||
parent::__construct('make:module'); | ||
$this->argumentsParser = $argumentsParser; | ||
$this->fileContentGenerator = $fileContentGenerator; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setDescription('Generate a basic module with an empty Facade|Factory|Config|DependencyProvider.') | ||
->addArgument('path', InputArgument::REQUIRED, 'The file path. For example "App/TestModule/TestSubModule"'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
/** @var string $path */ | ||
$path = $input->getArgument('path'); | ||
$commandArguments = $this->argumentsParser->parse($path); | ||
|
||
foreach (self::FILENAMES as $filename) { | ||
$this->fileContentGenerator->generate($commandArguments, $filename); | ||
$output->writeln("> Path '$path/$filename' created successfully"); | ||
} | ||
|
||
$pieces = explode('/', $commandArguments->directory()); | ||
$moduleName = end($pieces); | ||
$output->writeln("Module '$moduleName' created successfully"); | ||
|
||
return 0; | ||
} | ||
|
||
private function verifyFilename(string $filename): void | ||
{ | ||
if (!in_array($filename, self::FILENAMES)) { | ||
throw new RuntimeException(sprintf( | ||
'Filename must be one of these values: %s', | ||
json_encode(self::FILENAMES, JSON_THROW_ON_ERROR) | ||
)); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.