diff --git a/src/Laravel/Console/Maker/AbstractMakeStateCommand.php b/src/Laravel/Console/Maker/AbstractMakeStateCommand.php index 31d8989f7b..dbadf7450b 100644 --- a/src/Laravel/Console/Maker/AbstractMakeStateCommand.php +++ b/src/Laravel/Console/Maker/AbstractMakeStateCommand.php @@ -14,7 +14,6 @@ namespace ApiPlatform\Laravel\Console\Maker; use ApiPlatform\Laravel\Console\Maker\Utils\AppServiceProviderTagger; -use ApiPlatform\Laravel\Console\Maker\Utils\StateDirectoryManager; use ApiPlatform\Laravel\Console\Maker\Utils\StateTemplateGenerator; use ApiPlatform\Laravel\Console\Maker\Utils\StateTypeEnum; use ApiPlatform\Laravel\Console\Maker\Utils\SuccessMessageTrait; @@ -30,7 +29,6 @@ public function __construct( private readonly Filesystem $filesystem, private readonly StateTemplateGenerator $stateTemplateGenerator, private readonly AppServiceProviderTagger $appServiceProviderTagger, - private readonly StateDirectoryManager $stateDirectoryManager, ) { parent::__construct(); } @@ -41,10 +39,12 @@ public function __construct( public function handle(): int { $stateName = $this->askForStateName(); - $directoryPath = $this->stateDirectoryManager->ensureStateDirectoryExists(); + + $directoryPath = base_path('src/State/'); + $this->filesystem->ensureDirectoryExists($directoryPath); $filePath = $this->stateTemplateGenerator->getFilePath($directoryPath, $stateName); - if ($this->stateTemplateGenerator->isFileExists($filePath)) { + if ($this->filesystem->exists($filePath)) { $this->error(\sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $filePath)); return self::FAILURE; diff --git a/src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php b/src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php index 68ed42fa15..0db5f80044 100644 --- a/src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php +++ b/src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php @@ -37,22 +37,17 @@ public function __construct(private Filesystem $filesystem) public function addTagToServiceProvider(string $providerName, StateTypeEnum $stateTypeEnum): void { $appServiceProviderPath = app_path(self::APP_SERVICE_PROVIDER_PATH); - $this->ensureServiceProviderExists($appServiceProviderPath); + if (!$this->filesystem->exists($appServiceProviderPath)) { + throw new \RuntimeException('The AppServiceProvider is missing!'); + } $serviceProviderContent = $this->filesystem->get($appServiceProviderPath); $this->addUseStatement($serviceProviderContent, $this->getStateTypeStatement($stateTypeEnum)); - $this->addUseStatement($serviceProviderContent, $this->geStateNamespace($providerName)); + $this->addUseStatement($serviceProviderContent, \sprintf('use App\\State\\%s;', $providerName)); $this->addTag($serviceProviderContent, $providerName, $appServiceProviderPath, $stateTypeEnum); } - private function ensureServiceProviderExists(string $path): void - { - if (!$this->filesystem->exists($path)) { - throw new \RuntimeException('The AppServiceProvider is missing!'); - } - } - private function addUseStatement(string &$content, string $useStatement): void { if (!str_contains($content, $useStatement)) { @@ -67,7 +62,7 @@ private function addUseStatement(string &$content, string $useStatement): void private function addTag(string &$content, string $stateName, string $serviceProviderPath, StateTypeEnum $stateTypeEnum): void { - $tagStatement = $this->formatTagStatement($stateName, $stateTypeEnum->name); + $tagStatement = \sprintf("\n\n\t\t\$this->app->tag(%s::class, %sInterface::class);", $stateName, $stateTypeEnum->name); if (!str_contains($content, $tagStatement)) { $content = preg_replace( @@ -80,17 +75,7 @@ private function addTag(string &$content, string $stateName, string $serviceProv } } - private function formatTagStatement(string $stateName, string $interface): string - { - return \sprintf("\n\n\t\t\$this->app->tag(%s::class, %sInterface::class);", $stateName, $interface); - } - - public function geStateNamespace(string $providerName): string - { - return \sprintf('use App\\State\\%s;', $providerName); - } - - public function getStateTypeStatement(StateTypeEnum $stateTypeEnum): string + private function getStateTypeStatement(StateTypeEnum $stateTypeEnum): string { return match ($stateTypeEnum) { StateTypeEnum::Provider => self::ITEM_PROVIDER_USE_STATEMENT, diff --git a/src/Laravel/Console/Maker/Utils/StateDirectoryManager.php b/src/Laravel/Console/Maker/Utils/StateDirectoryManager.php deleted file mode 100644 index 7d1464cff5..0000000000 --- a/src/Laravel/Console/Maker/Utils/StateDirectoryManager.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace ApiPlatform\Laravel\Console\Maker\Utils; - -use Illuminate\Filesystem\Filesystem; - -final readonly class StateDirectoryManager -{ - public function __construct(private Filesystem $filesystem) - { - } - - public function ensureStateDirectoryExists(): string - { - $directoryPath = base_path('src/State/'); - $this->filesystem->ensureDirectoryExists($directoryPath); - - return $directoryPath; - } -} diff --git a/src/Laravel/Console/Maker/Utils/StateTemplateGenerator.php b/src/Laravel/Console/Maker/Utils/StateTemplateGenerator.php index 48b21379eb..2082d512f7 100644 --- a/src/Laravel/Console/Maker/Utils/StateTemplateGenerator.php +++ b/src/Laravel/Console/Maker/Utils/StateTemplateGenerator.php @@ -27,11 +27,6 @@ public function getFilePath(string $directoryPath, string $stateFileName): strin return $directoryPath.$stateFileName.'.php'; } - public function isFileExists(string $filePath): bool - { - return $this->filesystem->exists($filePath); - } - /** * @throws FileNotFoundException */ @@ -40,7 +35,7 @@ public function generate(string $pathLink, string $stateClassName, StateTypeEnum $namespace = 'App\\State'; $template = $this->loadTemplate($stateTypeEnum); - $content = $this->replacePlaceholders($template, [ + $content = strtr($template, [ '{{ namespace }}' => $namespace, '{{ class_name }}' => $stateClassName, ]); @@ -62,9 +57,4 @@ private function loadTemplate(StateTypeEnum $stateTypeEnum): string return $this->filesystem->get($templatePath); } - - private function replacePlaceholders(string $template, array $variables): string - { - return strtr($template, $variables); - } }