|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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; |
| 15 | + |
| 16 | +use ApiPlatform\Laravel\Eloquent\Metadata\MetadataDumpFingerprint; |
| 17 | +use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata; |
| 18 | +use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
| 19 | +use Illuminate\Console\Command; |
| 20 | +use Illuminate\Database\Eloquent\Model; |
| 21 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 22 | + |
| 23 | +#[AsCommand(name: 'api-platform:metadata:dump')] |
| 24 | +final class DumpMetadataCommand extends Command |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $signature = 'api-platform:metadata:dump {--path= : Where to write the dumped metadata file (defaults to the api-platform.metadata_dump config value)}'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $description = 'Dump the Eloquent model metadata to a file so the app can boot without hitting the database'; |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, |
| 38 | + private readonly ModelMetadata $modelMetadata, |
| 39 | + ) { |
| 40 | + parent::__construct(); |
| 41 | + } |
| 42 | + |
| 43 | + public function handle(): int |
| 44 | + { |
| 45 | + $path = $this->option('path'); |
| 46 | + if (null === $path || '' === $path) { |
| 47 | + $path = config('api-platform.metadata_dump'); |
| 48 | + } |
| 49 | + |
| 50 | + if (!\is_string($path) || '' === $path) { |
| 51 | + $this->error('No dump path configured. Pass --path or set the "api-platform.metadata_dump" config value.'); |
| 52 | + |
| 53 | + return self::FAILURE; |
| 54 | + } |
| 55 | + |
| 56 | + // This command is bound to a live ModelMetadata (contextual binding) so introspection reads |
| 57 | + // the database rather than a previously dumped, possibly stale, cache. |
| 58 | + $attributes = []; |
| 59 | + $relations = []; |
| 60 | + foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
| 61 | + try { |
| 62 | + $model = (new \ReflectionClass($resourceClass))->newInstanceWithoutConstructor(); |
| 63 | + } catch (\ReflectionException) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + if (!$model instanceof Model) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + $attributes[$resourceClass] = $this->modelMetadata->getAttributes($model); |
| 72 | + $relations[$resourceClass] = $this->modelMetadata->getRelations($model); |
| 73 | + } |
| 74 | + |
| 75 | + $directory = \dirname($path); |
| 76 | + if (!is_dir($directory) && !mkdir($directory, 0o755, true) && !is_dir($directory)) { |
| 77 | + $this->error(\sprintf('Unable to create directory "%s".', $directory)); |
| 78 | + |
| 79 | + return self::FAILURE; |
| 80 | + } |
| 81 | + |
| 82 | + // Write to a temporary file then rename so a concurrent boot never reads a half-written dump. |
| 83 | + $payload = serialize([ |
| 84 | + 'fingerprint' => MetadataDumpFingerprint::fromMigrations($this->laravel->databasePath('migrations')), |
| 85 | + 'attributes' => $attributes, |
| 86 | + 'relations' => $relations, |
| 87 | + ]); |
| 88 | + $temporaryPath = $path.'.'.getmypid().'.tmp'; |
| 89 | + if (false === file_put_contents($temporaryPath, $payload) || !rename($temporaryPath, $path)) { |
| 90 | + @unlink($temporaryPath); |
| 91 | + $this->error(\sprintf('Unable to write the metadata dump to "%s".', $path)); |
| 92 | + |
| 93 | + return self::FAILURE; |
| 94 | + } |
| 95 | + |
| 96 | + $this->info(\sprintf('Dumped metadata for %d model(s) to "%s".', \count($attributes), $path)); |
| 97 | + |
| 98 | + return self::SUCCESS; |
| 99 | + } |
| 100 | +} |
0 commit comments