|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Riclep\StoryblokCli\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Riclep\StoryblokCli\Traits\GetsComponents; |
| 8 | +use Storyblok\ManagementClient; |
| 9 | + |
| 10 | +class ComponentListCommand extends Command |
| 11 | +{ |
| 12 | + use GetsComponents; |
| 13 | + |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'ls:component-list |
| 20 | + {--additional-fields= : Additional fields to pull form Storyblok Management API}'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The console command description. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $description = 'List all Storyblok components for the space.'; |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * @var ManagementClient |
| 32 | + */ |
| 33 | + protected ManagementClient $managementClient; |
| 34 | + |
| 35 | + |
| 36 | + public function __construct() |
| 37 | + { |
| 38 | + parent::__construct(); |
| 39 | + |
| 40 | + $this->managementClient = new ManagementClient(config('storyblok-cli.oauth_token')); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Execute the console command. |
| 45 | + * |
| 46 | + * @return void |
| 47 | + */ |
| 48 | + public function handle() |
| 49 | + { |
| 50 | + $this->requestComponents(); |
| 51 | + |
| 52 | + $additionalFields = $this->option('additional-fields') ? |
| 53 | + Str::of($this->option('additional-fields'))->explode(',') |
| 54 | + : collect(); |
| 55 | + |
| 56 | + $rows = $this->sbComponents->map(function ($c) use ($additionalFields) { |
| 57 | + $mapped = [ |
| 58 | + 'name' => $c['name'], |
| 59 | + 'display_name' => $c['display_name'], |
| 60 | + 'has_image' => $c['image'] ? "<fg=green>true</>" : '<fg=red>false</>', |
| 61 | + 'has_template' => $c['preview_tmpl'] ? "<fg=green>true</>" : '<fg=red>false</>', |
| 62 | + ]; |
| 63 | + |
| 64 | + $mappedAdditional = collect($c)->only($additionalFields); |
| 65 | + |
| 66 | + return array_merge($mapped, $mappedAdditional->toArray()); |
| 67 | + }); |
| 68 | + |
| 69 | + $this->table( |
| 70 | + array_keys($rows->first()), |
| 71 | + $rows |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments