|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Laravel\Console\Maker\Utils; |
| 15 | + |
| 16 | +use Illuminate\Contracts\Filesystem\FileNotFoundException; |
| 17 | +use Illuminate\Filesystem\Filesystem; |
| 18 | + |
| 19 | +final readonly class AppServiceProviderTagger |
| 20 | +{ |
| 21 | + /** @var string */ |
| 22 | + private const APP_SERVICE_PROVIDER_PATH = 'Providers/AppServiceProvider.php'; |
| 23 | + |
| 24 | + /** @var string */ |
| 25 | + private const ITEM_PROVIDER_USE_STATEMENT = 'use ApiPlatform\State\ProviderInterface;'; |
| 26 | + |
| 27 | + /** @var string */ |
| 28 | + private const ITEM_PROCESSOR_USE_STATEMENT = 'use ApiPlatform\State\ProcessorInterface;'; |
| 29 | + |
| 30 | + public function __construct(private Filesystem $filesystem) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @throws FileNotFoundException |
| 36 | + */ |
| 37 | + public function addTagToServiceProvider(string $providerName, StateTypeEnum $stateTypeEnum): void |
| 38 | + { |
| 39 | + $appServiceProviderPath = app_path(self::APP_SERVICE_PROVIDER_PATH); |
| 40 | + if (!$this->filesystem->exists($appServiceProviderPath)) { |
| 41 | + throw new \RuntimeException('The AppServiceProvider is missing!'); |
| 42 | + } |
| 43 | + |
| 44 | + $serviceProviderContent = $this->filesystem->get($appServiceProviderPath); |
| 45 | + |
| 46 | + $this->addUseStatement($serviceProviderContent, $this->getStateTypeStatement($stateTypeEnum)); |
| 47 | + $this->addUseStatement($serviceProviderContent, \sprintf('use App\\State\\%s;', $providerName)); |
| 48 | + $this->addTag($serviceProviderContent, $providerName, $appServiceProviderPath, $stateTypeEnum); |
| 49 | + } |
| 50 | + |
| 51 | + private function addUseStatement(string &$content, string $useStatement): void |
| 52 | + { |
| 53 | + if (!str_contains($content, $useStatement)) { |
| 54 | + $content = preg_replace( |
| 55 | + '/^(namespace\s[^;]+;\s*)(\n)/m', |
| 56 | + "$1\n$useStatement$2", |
| 57 | + $content, |
| 58 | + 1 |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private function addTag(string &$content, string $stateName, string $serviceProviderPath, StateTypeEnum $stateTypeEnum): void |
| 64 | + { |
| 65 | + $tagStatement = \sprintf("\n\n\t\t\$this->app->tag(%s::class, %sInterface::class);", $stateName, $stateTypeEnum->name); |
| 66 | + |
| 67 | + if (!str_contains($content, $tagStatement)) { |
| 68 | + $content = preg_replace( |
| 69 | + '/(public function register\(\)[^{]*{)(.*?)(\s*}\s*})/s', |
| 70 | + "$1$2$tagStatement$3", |
| 71 | + $content |
| 72 | + ); |
| 73 | + |
| 74 | + $this->filesystem->put($serviceProviderPath, $content); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private function getStateTypeStatement(StateTypeEnum $stateTypeEnum): string |
| 79 | + { |
| 80 | + return match ($stateTypeEnum) { |
| 81 | + StateTypeEnum::Provider => self::ITEM_PROVIDER_USE_STATEMENT, |
| 82 | + StateTypeEnum::Processor => self::ITEM_PROCESSOR_USE_STATEMENT, |
| 83 | + }; |
| 84 | + } |
| 85 | +} |
0 commit comments