-
-
Notifications
You must be signed in to change notification settings - Fork 947
feat(laravel): command to generate state providers/processors #6708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
soyuka
merged 5 commits into
api-platform:4.0
from
vinceAmstoutz:feat-add-laravel-make-commands-for-states
Oct 11, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c2cecf
feat(laravel): add make provider and processor commands
vinceAmstoutz cb03f73
docs(CHANGELOG): Add feature to v4.0.4
vinceAmstoutz 5d39b7b
refactor: remove StateDirectoryManager and methods
vinceAmstoutz ed65aed
Apply suggestions from code review
soyuka 6170165
Discard changes to CHANGELOG.md
soyuka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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; | ||
|
|
||
| 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; | ||
| use Illuminate\Console\Command; | ||
| use Illuminate\Contracts\Filesystem\FileNotFoundException; | ||
| use Illuminate\Filesystem\Filesystem; | ||
|
|
||
| abstract class AbstractMakeStateCommand extends Command | ||
| { | ||
| use SuccessMessageTrait; | ||
|
|
||
| public function __construct( | ||
| private readonly Filesystem $filesystem, | ||
| private readonly StateTemplateGenerator $stateTemplateGenerator, | ||
| private readonly AppServiceProviderTagger $appServiceProviderTagger, | ||
| private readonly StateDirectoryManager $stateDirectoryManager, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| /** | ||
| * @throws FileNotFoundException | ||
| */ | ||
| public function handle(): int | ||
| { | ||
| $stateName = $this->askForStateName(); | ||
| $directoryPath = $this->stateDirectoryManager->ensureStateDirectoryExists(); | ||
|
|
||
| $filePath = $this->stateTemplateGenerator->getFilePath($directoryPath, $stateName); | ||
| if ($this->stateTemplateGenerator->isFileExists($filePath)) { | ||
| $this->error(\sprintf('[ERROR] The file "%s" can\'t be generated because it already exists.', $filePath)); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| $this->stateTemplateGenerator->generate($filePath, $stateName, $this->getStateType()); | ||
| if (!$this->filesystem->exists($filePath)) { | ||
| $this->error(\sprintf('[ERROR] The file "%s" could not be created.', $filePath)); | ||
|
|
||
| return self::FAILURE; | ||
| } | ||
|
|
||
| $this->appServiceProviderTagger->addTagToServiceProvider($stateName, $this->getStateType()); | ||
|
|
||
| $this->writeSuccessMessage($filePath, $this->getStateType()); | ||
|
|
||
| return self::SUCCESS; | ||
| } | ||
|
|
||
| protected function askForStateName(): string | ||
| { | ||
| do { | ||
| $stateType = $this->getStateType()->name; | ||
| $stateName = $this->ask(\sprintf('Choose a class name for your state %s (e.g. <fg=yellow>AwesomeState%s</>)', mb_strtolower($stateType), mb_ucfirst($stateType))); | ||
| if (empty($stateName)) { | ||
| $this->error('[ERROR] This value cannot be blank.'); | ||
| } | ||
| } while (empty($stateName)); | ||
|
|
||
| return $stateName; | ||
| } | ||
|
|
||
| abstract protected function getStateType(): StateTypeEnum; | ||
| } | ||
This file contains hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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; | ||
|
|
||
| use ApiPlatform\Laravel\Console\Maker\Utils\StateTypeEnum; | ||
|
|
||
| final class MakeStateProcessorCommand extends AbstractMakeStateCommand | ||
| { | ||
| protected $signature = 'make:state-processor'; | ||
| protected $description = 'Creates an API Platform state processor'; | ||
|
|
||
| protected function getStateType(): StateTypeEnum | ||
| { | ||
| return StateTypeEnum::Processor; | ||
| } | ||
| } |
This file contains hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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; | ||
|
|
||
| use ApiPlatform\Laravel\Console\Maker\Utils\StateTypeEnum; | ||
|
|
||
| final class MakeStateProviderCommand extends AbstractMakeStateCommand | ||
| { | ||
| protected $signature = 'make:state-provider'; | ||
| protected $description = 'Creates an API Platform state provider'; | ||
|
|
||
| protected function getStateType(): StateTypeEnum | ||
| { | ||
| return StateTypeEnum::Provider; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Laravel/Console/Maker/Resources/skeleton/StateProcessor.tpl.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace {{ namespace }}; | ||
|
|
||
| use ApiPlatform\Metadata\Operation; | ||
| use ApiPlatform\State\ProcessorInterface; | ||
|
|
||
| final class {{ class_name }} implements ProcessorInterface | ||
| { | ||
| public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): void | ||
| { | ||
| // Handle the state | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/Laravel/Console/Maker/Resources/skeleton/StateProvider.tpl.php
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace {{ namespace }}; | ||
|
|
||
| use ApiPlatform\Metadata\Operation; | ||
| use ApiPlatform\State\ProviderInterface; | ||
|
|
||
| final class {{ class_name }} implements ProviderInterface | ||
| { | ||
| public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
| { | ||
| // Retrieve the state from somewhere | ||
| } | ||
| } |
100 changes: 100 additions & 0 deletions
100
src/Laravel/Console/Maker/Utils/AppServiceProviderTagger.php
This file contains hidden or 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,100 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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\Contracts\Filesystem\FileNotFoundException; | ||
| use Illuminate\Filesystem\Filesystem; | ||
|
|
||
| final readonly class AppServiceProviderTagger | ||
| { | ||
| /** @var string */ | ||
| private const APP_SERVICE_PROVIDER_PATH = 'Providers/AppServiceProvider.php'; | ||
|
|
||
| /** @var string */ | ||
| private const ITEM_PROVIDER_USE_STATEMENT = 'use ApiPlatform\State\ProviderInterface;'; | ||
|
|
||
| /** @var string */ | ||
| private const ITEM_PROCESSOR_USE_STATEMENT = 'use ApiPlatform\State\ProcessorInterface;'; | ||
|
|
||
| public function __construct(private Filesystem $filesystem) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @throws FileNotFoundException | ||
| */ | ||
| public function addTagToServiceProvider(string $providerName, StateTypeEnum $stateTypeEnum): void | ||
| { | ||
| $appServiceProviderPath = app_path(self::APP_SERVICE_PROVIDER_PATH); | ||
| $this->ensureServiceProviderExists($appServiceProviderPath); | ||
|
|
||
| $serviceProviderContent = $this->filesystem->get($appServiceProviderPath); | ||
|
|
||
| $this->addUseStatement($serviceProviderContent, $this->getStateTypeStatement($stateTypeEnum)); | ||
| $this->addUseStatement($serviceProviderContent, $this->geStateNamespace($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!'); | ||
| } | ||
| } | ||
vinceAmstoutz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private function addUseStatement(string &$content, string $useStatement): void | ||
| { | ||
| if (!str_contains($content, $useStatement)) { | ||
| $content = preg_replace( | ||
| '/^(namespace\s[^;]+;\s*)(\n)/m', | ||
| "$1\n$useStatement$2", | ||
| $content, | ||
| 1 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private function addTag(string &$content, string $stateName, string $serviceProviderPath, StateTypeEnum $stateTypeEnum): void | ||
| { | ||
| $tagStatement = $this->formatTagStatement($stateName, $stateTypeEnum->name); | ||
|
|
||
| if (!str_contains($content, $tagStatement)) { | ||
| $content = preg_replace( | ||
| '/(public function register\(\)[^{]*{)(.*?)(\s*}\s*})/s', | ||
| "$1$2$tagStatement$3", | ||
| $content | ||
| ); | ||
|
|
||
| $this->filesystem->put($serviceProviderPath, $content); | ||
| } | ||
| } | ||
|
|
||
| private function formatTagStatement(string $stateName, string $interface): string | ||
| { | ||
| return \sprintf("\n\n\t\t\$this->app->tag(%s::class, %sInterface::class);", $stateName, $interface); | ||
| } | ||
vinceAmstoutz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public function geStateNamespace(string $providerName): string | ||
| { | ||
| return \sprintf('use App\\State\\%s;', $providerName); | ||
| } | ||
vinceAmstoutz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public function getStateTypeStatement(StateTypeEnum $stateTypeEnum): string | ||
| { | ||
| return match ($stateTypeEnum) { | ||
| StateTypeEnum::Provider => self::ITEM_PROVIDER_USE_STATEMENT, | ||
| StateTypeEnum::Processor => self::ITEM_PROCESSOR_USE_STATEMENT, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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,31 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <[email protected]> | ||
| * | ||
| * 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; | ||
vinceAmstoutz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.