|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Support\Facades\File; |
| 8 | +use ReflectionClass; |
| 9 | + |
| 10 | +class ExportJsonsCommand extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'export:jsons'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Export all JSONs defined in HasJsonExports trait'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute the console command. |
| 28 | + */ |
| 29 | + public function handle(): void |
| 30 | + { |
| 31 | + $modelsPath = app_path('Models'); |
| 32 | + |
| 33 | + if (! File::exists($modelsPath)) { |
| 34 | + $this->error('Models directory not found.'); |
| 35 | + |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + $modelFiles = File::allFiles($modelsPath); |
| 40 | + |
| 41 | + foreach ($modelFiles as $file) { |
| 42 | + $className = 'App\\Models\\'.$file->getFilenameWithoutExtension(); |
| 43 | + |
| 44 | + if (! class_exists($className)) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + $reflection = new ReflectionClass($className); |
| 49 | + if (! $reflection->isInstantiable() || ! $reflection->isSubclassOf(Model::class)) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + $traits = class_uses_recursive($className); |
| 54 | + $usesHasJsonExports = false; |
| 55 | + |
| 56 | + foreach ($traits as $trait) { |
| 57 | + if (str_ends_with($trait, 'HasJsonExports')) { |
| 58 | + $usesHasJsonExports = true; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if ($usesHasJsonExports) { |
| 64 | + $this->info("Exporting for model: $className"); |
| 65 | + |
| 66 | + /** @var Model */ |
| 67 | + $model = new $className; |
| 68 | + $this->exportModel($model); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + protected function exportModel(Model $targetModel): void |
| 74 | + { |
| 75 | + // @phpstan-ignore method.notFound |
| 76 | + foreach ($targetModel->getJsonExportMethods() as $name => $method) { |
| 77 | + $this->line(" - Generating $name.json"); |
| 78 | + |
| 79 | + $data = $targetModel->$method(); |
| 80 | + |
| 81 | + $path = config('gemadigital.jsonExports.path', storage_path('data'))."/{$name}.json"; |
| 82 | + |
| 83 | + if (! file_exists(dirname($path))) { |
| 84 | + mkdir(dirname($path), 0755, true); |
| 85 | + } |
| 86 | + |
| 87 | + file_put_contents( |
| 88 | + $path, |
| 89 | + $data->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments