1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\Storyblok\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
|
8
|
|
|
class StubViewsCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'ls:stub-views {--O|overwrite}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Command description'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new command instance. |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function handle(): void |
40
|
|
|
{ |
41
|
|
|
$this->makeDirectories(); |
42
|
|
|
|
43
|
|
|
$client = new \Storyblok\ManagementClient(config('storyblok.oauth_token')); |
44
|
|
|
|
45
|
|
|
$components = collect($client->get('spaces/' . config('storyblok.space_id') . '/components/')->getBody()['components']); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$components->each(function ($component) { |
48
|
|
|
$path = resource_path('views/' . str_replace('.', '/', config('storyblok.view_path')) . 'blocks/'); |
49
|
|
|
$filename = $component['name'] . '.blade.php'; |
50
|
|
|
|
51
|
|
|
if ($this->option('overwrite') || !file_exists($path . $filename)) { |
52
|
|
|
$content = file_get_contents(__DIR__ . '/stubs/blade.stub'); |
53
|
|
|
$content = str_replace('#NAME#', $component['name'], $content); |
54
|
|
|
|
55
|
|
|
$body = ''; |
56
|
|
|
|
57
|
|
|
foreach ($component['schema'] as $name => $field) { |
58
|
|
|
$body = $this->writeBlade($field['type'], $name, $body); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$content = str_replace('#BODY#', $body, $content); |
62
|
|
|
|
63
|
|
|
file_put_contents($path . $filename, $content); |
64
|
|
|
|
65
|
|
|
$this->info('Created: '. $component['name'] . '.blade.php'); |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function makeDirectories(): void |
74
|
|
|
{ |
75
|
|
|
if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.')))) { |
76
|
|
|
File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.'))); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/blocks'))) { |
80
|
|
|
File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/blocks')); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $type |
86
|
|
|
* @param int|string $name |
87
|
|
|
* @param string $body |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function writeBlade($type, int|string $name, string $body): string |
91
|
|
|
{ |
92
|
|
|
switch ($type) { |
93
|
|
|
case 'bloks': |
94
|
|
|
$body .= "\t" . '@foreach ($block->' . $name . ' as $childBlock)' . "\n"; |
95
|
|
|
$body .= "\t\t" . '{{ $childBlock->render() }}' . "\n"; |
96
|
|
|
$body .= "\t" . '@endforeach' . "\n"; |
97
|
|
|
break; |
98
|
|
|
case 'datetime': |
99
|
|
|
case 'number': |
100
|
|
|
case 'text': |
101
|
|
|
$body .= "\t" . '<p>{{ $block->' . $name . ' }}</p>' . "\n"; |
102
|
|
|
break; |
103
|
|
|
case 'multilink': |
104
|
|
|
$body .= "\t" . '<a href="{{ $block->' . $name . '->cached_url }}"></a>' . "\n"; |
105
|
|
|
break; |
106
|
|
|
case 'options': |
107
|
|
|
$body .= "\t" . '@foreach ($block->' . $name . ' as $childBlock)' . "\n"; |
108
|
|
|
$body .= "\t\t\n"; |
109
|
|
|
$body .= "\t" . '@endforeach' . "\n"; |
110
|
|
|
break; |
111
|
|
|
case 'textarea': |
112
|
|
|
case 'richtext': |
113
|
|
|
$body .= "\t" . '<div>{!! $block->' . $name . ' !!}</div>' . "\n"; |
114
|
|
|
break; |
115
|
|
|
default: |
116
|
|
|
$body .= "\t" . '{{ $block->' . $name . ' }}' . "\n"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$body .= "\n"; |
120
|
|
|
return $body; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.