1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\Storyblok\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Facades\Cache; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
|
10
|
|
|
class BlockMakeCommand extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
protected $name = 'ls:block'; |
13
|
|
|
|
14
|
|
|
protected $description = 'Create a new block class'; |
15
|
|
|
|
16
|
|
|
protected $type = 'Block'; |
17
|
|
|
|
18
|
|
|
protected function getStub() |
19
|
|
|
{ |
20
|
|
|
return __DIR__ . '/stubs/block.stub'; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function getDefaultNamespace($rootNamespace) |
24
|
|
|
{ |
25
|
|
|
return $rootNamespace . '\Storyblok\Blocks'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function handle() |
29
|
|
|
{ |
30
|
|
|
parent::handle(); |
31
|
|
|
|
32
|
|
|
$this->doOtherOperations(); |
33
|
|
|
|
34
|
|
|
if ($this->option('blade')) { |
35
|
|
|
$this->createBlade(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($this->option('scss')) { |
39
|
|
|
$this->createScss(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function doOtherOperations() |
44
|
|
|
{ |
45
|
|
|
$class = $this->qualifyClass($this->getNameInput()); |
46
|
|
|
$path = $this->getPath($class); |
47
|
|
|
$content = file_get_contents($path); |
48
|
|
|
|
49
|
|
|
$components = $this->getComponentFields(Str::kebab($this->getNameInput())); |
|
|
|
|
50
|
|
|
|
51
|
|
|
$content = str_replace('DummyPhpDoc', $this->getComponentFields(Str::kebab($this->getNameInput())), $content); |
52
|
|
|
|
53
|
|
|
file_put_contents($path, $content); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function createBlade() { |
57
|
|
|
// call API to get details on the Block and name comments of vars available |
58
|
|
|
|
59
|
|
|
$name = Str::kebab($this->getNameInput()); |
60
|
|
|
|
61
|
|
|
if (!file_exists($this->viewPath('storyblok/components/') . $name . '.blade.php')) { |
62
|
|
|
$content = file_get_contents(__DIR__ . '/stubs/block.blade.stub'); |
63
|
|
|
|
64
|
|
|
$find = ['DummyClass', 'DummyCssClass']; |
65
|
|
|
$replace = [$this->getNameInput(), $name]; |
66
|
|
|
|
67
|
|
|
$content = str_replace($find, $replace, $content); |
68
|
|
|
|
69
|
|
|
file_put_contents($this->viewPath('storyblok/components/') . $name . '.blade.php', $content); |
70
|
|
|
} else { |
71
|
|
|
$this->error('Blade already exists!'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function createScss() { |
76
|
|
|
$name = Str::kebab($this->getNameInput()); |
77
|
|
|
|
78
|
|
|
if (!file_exists(resource_path('sass/blocks/') . '_' . $name . '.scss')) { |
79
|
|
|
$content = file_get_contents(__DIR__ . '/stubs/block.scss.stub'); |
80
|
|
|
$content = str_replace('DummyClass', $name, $content); |
81
|
|
|
|
82
|
|
|
file_put_contents(resource_path('sass/blocks/') . '_' . $name . '.scss', $content); |
83
|
|
|
} else { |
84
|
|
|
$this->error('SCSS already exists!'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$appContent = file_get_contents(resource_path('sass/app.scss')); |
88
|
|
|
|
89
|
|
|
preg_match_all("/^@import \"blocks(.*)\r$/m", $appContent, $matches); |
90
|
|
|
|
91
|
|
|
$files = $matches[0]; |
92
|
|
|
$files[] = '@import "blocks/' . $name . '";'; |
93
|
|
|
asort($files); |
94
|
|
|
|
95
|
|
|
$allFilesString = implode("\n", $files); |
96
|
|
|
|
97
|
|
|
$allButFirstFile = $files; |
98
|
|
|
|
99
|
|
|
array_shift($allButFirstFile); |
100
|
|
|
|
101
|
|
|
$appContent = str_replace($allButFirstFile, '', $appContent); |
102
|
|
|
|
103
|
|
|
// clean up all the empty lines left over from the replacement |
104
|
|
|
$appContent = preg_replace("/\n\n+/s","\n",$appContent); |
105
|
|
|
|
106
|
|
|
$appContent = str_replace($files[0], $allFilesString, $appContent); |
107
|
|
|
|
108
|
|
|
file_put_contents(resource_path('sass/app.scss'), $appContent); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getComponentFields($name) { |
112
|
|
|
$components = Cache::remember('lsb-compontent-list', 600, function() { |
113
|
|
|
$managementClient = new \Storyblok\ManagementClient(config('storyblok.oauth_token')); |
114
|
|
|
|
115
|
|
|
return collect($managementClient->get('spaces/' . config('storyblok.space_id') . '/components')->getBody()['components']); |
|
|
|
|
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$schema = $components->filter(function ($component) use ($name) { |
119
|
|
|
return $component['name'] === $name; |
120
|
|
|
})->first()['schema']; |
121
|
|
|
|
122
|
|
|
$fields = array_map(function($field) { |
123
|
|
|
return ' * @property-read string ' . $field . "\n"; |
124
|
|
|
}, array_keys($schema)); |
125
|
|
|
|
126
|
|
|
$phpDoc = <<<'PHPDOC' |
127
|
|
|
/** |
128
|
|
|
DummyFields |
129
|
|
|
*/ |
130
|
|
|
PHPDOC; |
131
|
|
|
|
132
|
|
|
return str_replace('DummyFields', implode($fields), $phpDoc); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the console command options. |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function getOptions() |
141
|
|
|
{ |
142
|
|
|
return [ |
143
|
|
|
['blade', 'b', InputOption::VALUE_NONE, 'Create stub Blade view'], |
144
|
|
|
['scss', 's', InputOption::VALUE_NONE, 'Create stub SCSS view'], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|