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']); |
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 Block: '. $component['name'] . '.blade.php'); |
78
|
|
|
} |
79
|
|
|
}); |
80
|
|
|
|
81
|
|
|
if ($this->option('overwrite') || !file_exists(resource_path('views/storyblok/pages') . '/page.blade.php')) { |
82
|
|
|
File::copy(__DIR__ . '/stubs/page.blade.stub', resource_path('views/storyblok/pages') . '/page.blade.php'); |
83
|
|
|
|
84
|
|
|
$this->info('Created Page: page.blade.php'); |
85
|
|
|
|
86
|
|
|
$this->info('Files created in your views' . DIRECTORY_SEPARATOR . 'storyblok folder.'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
protected function makeDirectories(): void |
94
|
|
|
{ |
95
|
|
|
if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.')))) { |
96
|
|
|
File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.'))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/blocks'))) { |
100
|
|
|
File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/blocks')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (!file_exists(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/pages'))) { |
104
|
|
|
File::makeDirectory(resource_path('views/' . rtrim(config('storyblok.view_path'), '.') . '/pages')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $field |
110
|
|
|
* @param int|string $name |
111
|
|
|
* @param string $body |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function writeBlade($field, int|string $name, string $body): string |
115
|
|
|
{ |
116
|
|
|
if (!str_starts_with($name, 'tab-')) { |
117
|
|
|
switch ($field['type']) { |
118
|
|
|
case 'options': |
119
|
|
|
case 'bloks': |
120
|
|
|
$body .= "\t" . '@foreach ($block->' . $name . ' as $childBlock)' . "\n"; |
121
|
|
|
$body .= "\t\t" . '{{ $childBlock->render() }}' . "\n"; |
122
|
|
|
$body .= "\t" . '@endforeach' . "\n"; |
123
|
|
|
break; |
124
|
|
|
case 'datetime': |
125
|
|
|
$body .= "\t" . '<time datetime="{{ $block->' . $name . '->content()->toIso8601String() }}">{{ $block->' . $name . ' }}</time>' . "\n"; |
126
|
|
|
break; |
127
|
|
|
case 'number': |
128
|
|
|
case 'text': |
129
|
|
|
$body .= "\t" . '<p>{{ $block->' . $name . ' }}</p>' . "\n"; |
130
|
|
|
break; |
131
|
|
|
case 'multilink': |
132
|
|
|
$body .= "\t" . '<a href="{{ $block->' . $name . '->cached_url }}"></a>' . "\n"; |
133
|
|
|
break; |
134
|
|
|
case 'textarea': |
135
|
|
|
case 'richtext': |
136
|
|
|
$body .= "\t" . '<div>{!! $block->' . $name . ' !!}</div>' . "\n"; |
137
|
|
|
break; |
138
|
|
|
case 'asset': |
139
|
|
|
if (array_key_exists('filetypes', $field) && in_array('images', $field['filetypes'], true)) { |
140
|
|
|
$body .= "\t" . '@if ($block->' . $name . '->hasFile())' . "\n"; |
141
|
|
|
$body .= "\t\t" . '<img src="{{ $block->' . $name . '->transform()->resize(100, 100) }}" alt>' . "\n"; |
142
|
|
|
$body .= "\t" . '@endif' . "\n"; |
143
|
|
|
} else { |
144
|
|
|
$body .= "\t" . '<a href="{{ $block->' . $name . ' }}">Download</a>' . "\n"; |
145
|
|
|
} |
146
|
|
|
break; |
147
|
|
|
case 'image': |
148
|
|
|
$body .= "\t" . '@if ($block->' . $name . '->hasFile())' . "\n"; |
149
|
|
|
$body .= "\t\t" . '<img src="{{ $block->' . $name . '->transform()->resize(100, 100) }}" alt>' . "\n"; |
150
|
|
|
$body .= "\t" . '@endif' . "\n"; |
151
|
|
|
break; |
152
|
|
|
case 'file': |
153
|
|
|
$body .= "\t" . '@if ($block->' . $name . '->hasFile())' . "\n"; |
154
|
|
|
$body .= "\t\t" . '<a href="{{ $block->' . $name . ' }}">{{ $block->' . $name . '->filename }}</a>' . "\n"; |
155
|
|
|
$body .= "\t" . '@endif' . "\n"; |
156
|
|
|
break; |
157
|
|
|
default: |
158
|
|
|
$body .= "\t" . '{{ $block->' . $name . ' }}' . "\n"; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$body .= "\n"; |
163
|
|
|
return $body; |
164
|
|
|
} |
165
|
|
|
} |