| 1 | <?php |
||
| 2 | |||
| 3 | namespace Riclep\Storyblok\Console; |
||
| 4 | |||
| 5 | use Illuminate\Console\Command; |
||
| 6 | use Illuminate\Support\Facades\File; |
||
| 7 | use Riclep\Storyblok\Traits\HasChildClasses; |
||
| 8 | use Storyblok\ApiException; |
||
| 9 | use Storyblok\ManagementClient; |
||
| 10 | |||
| 11 | class StubViewsCommand extends Command |
||
| 12 | { |
||
| 13 | use HasChildClasses; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The name and signature of the console command. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $signature = 'ls:stub-views {--O|overwrite}'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The console command description. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $description = 'Command description'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new command instance. |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 35 | { |
||
| 36 | parent::__construct(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Execute the console command. |
||
| 41 | * |
||
| 42 | * @return void |
||
| 43 | * @throws ApiException |
||
| 44 | */ |
||
| 45 | public function handle(): void |
||
| 46 | { |
||
| 47 | $this->makeDirectories(); |
||
| 48 | |||
| 49 | $client = new ManagementClient(config('storyblok.oauth_token')); |
||
| 50 | |||
| 51 | $components = collect($client->get('spaces/' . config('storyblok.space_id') . '/components/')->getBody()['components']); |
||
|
0 ignored issues
–
show
|
|||
| 52 | |||
| 53 | $components->each(function ($component) { |
||
| 54 | $path = resource_path('views/' . str_replace('.', '/', config('storyblok.view_path')) . 'blocks/'); |
||
| 55 | $filename = $component['name'] . '.blade.php'; |
||
| 56 | |||
| 57 | if ($this->option('overwrite') || !file_exists($path . $filename)) { |
||
| 58 | $content = file_get_contents(__DIR__ . '/stubs/blade.stub'); |
||
| 59 | $content = str_replace([ |
||
| 60 | '#NAME#', |
||
| 61 | '#CLASS#' |
||
| 62 | ], [ |
||
| 63 | $component['name'], |
||
| 64 | $this->getChildClassName('Block', $component['name']) |
||
| 65 | ], $content); |
||
| 66 | |||
| 67 | $body = ''; |
||
| 68 | |||
| 69 | foreach ($component['schema'] as $name => $field) { |
||
| 70 | $body = $this->writeBlade($field, $name, $body); |
||
| 71 | } |
||
| 72 | |||
| 73 | $content = str_replace('#BODY#', $body, $content); |
||
| 74 | |||
| 75 | file_put_contents($path . $filename, $content); |
||
| 76 | |||
| 77 | $this->info('Created: '. $component['name'] . '.blade.php'); |
||
| 78 | } |
||
| 79 | }); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | protected function makeDirectories(): void |
||
| 86 | { |
||
| 87 | if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.')))) { |
||
| 88 | File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.'))); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/blocks'))) { |
||
| 92 | File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/blocks')); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $field |
||
| 98 | * @param int|string $name |
||
| 99 | * @param string $body |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | protected function writeBlade($field, int|string $name, string $body): string |
||
| 103 | { |
||
| 104 | if (!str_starts_with($name, 'tab-')) { |
||
| 105 | switch ($field['type']) { |
||
| 106 | case 'options': |
||
| 107 | case 'bloks': |
||
| 108 | $body .= "\t" . '@foreach ($block->' . $name . ' as $childBlock)' . "\n"; |
||
| 109 | $body .= "\t\t" . '{{ $childBlock->render() }}' . "\n"; |
||
| 110 | $body .= "\t" . '@endforeach' . "\n"; |
||
| 111 | break; |
||
| 112 | case 'datetime': |
||
| 113 | $body .= "\t" . '<time datetime="{{ $block->' . $name . '->content()->toIso8601String() }}">{{ $block->' . $name . ' }}</time>' . "\n"; |
||
| 114 | break; |
||
| 115 | case 'number': |
||
| 116 | case 'text': |
||
| 117 | $body .= "\t" . '<p>{{ $block->' . $name . ' }}</p>' . "\n"; |
||
| 118 | break; |
||
| 119 | case 'multilink': |
||
| 120 | $body .= "\t" . '<a href="{{ $block->' . $name . '->cached_url }}"></a>' . "\n"; |
||
| 121 | break; |
||
| 122 | case 'textarea': |
||
| 123 | case 'richtext': |
||
| 124 | $body .= "\t" . '<div>{!! $block->' . $name . ' !!}</div>' . "\n"; |
||
| 125 | break; |
||
| 126 | case 'asset': |
||
| 127 | if (in_array('images', $field['filetypes'], true)) { |
||
| 128 | $body .= "\t" . '@if ($block->' . $name . '->hasFile())' . "\n"; |
||
| 129 | $body .= "\t\t" . '<img src="{{ $block->' . $name . '->transform()->resize(100, 100) }}" alt>' . "\n"; |
||
| 130 | $body .= "\t" . '@endif' . "\n"; |
||
| 131 | } else { |
||
| 132 | $body .= "\t" . '{{ $block->' . $name . ' }}' . "\n"; |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | default: |
||
| 136 | $body .= "\t" . '{{ $block->' . $name . ' }}' . "\n"; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | $body .= "\n"; |
||
| 141 | return $body; |
||
| 142 | } |
||
| 143 | } |
||
| 144 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.