|
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(): string |
|
19
|
|
|
{ |
|
20
|
|
|
return file_exists(resource_path('stubs/storyblok/block.stub')) ? resource_path('stubs/storyblok/block.stub') : __DIR__ . '/stubs/block.stub'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function getDefaultNamespace($rootNamespace): string |
|
24
|
|
|
{ |
|
25
|
|
|
return $rootNamespace . '\Storyblok\Blocks'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function handle(): void |
|
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(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$class = $this->qualifyClass($this->getNameInput()); |
|
46
|
|
|
$path = $this->getPath($class); |
|
47
|
|
|
$content = file_get_contents($path); |
|
48
|
|
|
|
|
49
|
|
|
file_put_contents($path, $content); |
|
50
|
|
|
|
|
51
|
|
|
$this->getComponentFields($this->getNameInput()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function createBlade(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$name = Str::kebab($this->getNameInput()); |
|
57
|
|
|
$path = $this->viewPath( str_replace( '.' , '/' , config('storyblok.view_path') . 'blocks.' ) ); |
|
58
|
|
|
$stub = file_exists(resource_path('stubs/storyblok/block.blade.stub')) ? resource_path('stubs/storyblok/block.blade.stub') : __DIR__ . '/stubs/block.blade.stub'; |
|
59
|
|
|
|
|
60
|
|
|
if (!file_exists($path . $name . '.blade.php')) { |
|
61
|
|
|
$content = file_get_contents($stub); |
|
62
|
|
|
|
|
63
|
|
|
$find = ['DummyClass', 'DummyCssClass']; |
|
64
|
|
|
$replace = [$this->getNameInput(), $name]; |
|
65
|
|
|
|
|
66
|
|
|
$content = str_replace($find, $replace, $content); |
|
67
|
|
|
|
|
68
|
|
|
if (!$this->files->exists($path)) { |
|
69
|
|
|
$this->files->makeDirectory($path); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
file_put_contents($path . $name . '.blade.php', $content); |
|
73
|
|
|
$this->info('Blade created successfully.'); |
|
74
|
|
|
} else { |
|
75
|
|
|
$this->error('Blade already exists!'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function createScss(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$name = Str::kebab($this->getNameInput()); |
|
82
|
|
|
$path = resource_path('sass/blocks/'); |
|
83
|
|
|
$stub = file_exists(resource_path('stubs/storyblok/block.scss.stub')) ? resource_path('stubs/storyblok/block.scss.stub') : __DIR__ . '/stubs/block.scss.stub'; |
|
84
|
|
|
|
|
85
|
|
|
if (!file_exists($path . '_' . $name . '.scss')) { |
|
86
|
|
|
$content = file_get_contents($stub); |
|
87
|
|
|
$content = str_replace('DummyClass', $name, $content); |
|
88
|
|
|
|
|
89
|
|
|
if (!$this->files->exists($path)) { |
|
90
|
|
|
$this->files->makeDirectory($path); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
file_put_contents($path . '_' . $name . '.scss', $content); |
|
94
|
|
|
$this->info('SCSS created successfully.'); |
|
95
|
|
|
} else { |
|
96
|
|
|
$this->error('SCSS already exists!'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$appContent = file_get_contents(resource_path('sass/app.scss')); |
|
100
|
|
|
|
|
101
|
|
|
preg_match_all("/^@import \"blocks(.*)\r$/m", $appContent, $matches); |
|
102
|
|
|
|
|
103
|
|
|
$files = $matches[0]; |
|
104
|
|
|
$files[] = '@import "blocks/' . $name . '";'; |
|
105
|
|
|
asort($files); |
|
106
|
|
|
|
|
107
|
|
|
$allFilesString = implode("\n", $files); |
|
108
|
|
|
|
|
109
|
|
|
$allButFirstFile = $files; |
|
110
|
|
|
|
|
111
|
|
|
array_shift($allButFirstFile); |
|
112
|
|
|
|
|
113
|
|
|
$appContent = str_replace($allButFirstFile, '', $appContent); |
|
114
|
|
|
|
|
115
|
|
|
// clean up all the empty lines left over from the replacement |
|
116
|
|
|
$appContent = preg_replace("/\n\n+/s","\n",$appContent); |
|
117
|
|
|
|
|
118
|
|
|
$appContent = str_replace($files[0], $allFilesString, $appContent); |
|
119
|
|
|
|
|
120
|
|
|
file_put_contents(resource_path('sass/app.scss'), $appContent); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function getComponentFields($name): void |
|
124
|
|
|
{ |
|
125
|
|
|
if (config('storyblok.oauth_token')) { |
|
126
|
|
|
$this->call('ls:sync', [ |
|
127
|
|
|
'component' => Str::studly($name), |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get the console command options. |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getOptions(): array |
|
138
|
|
|
{ |
|
139
|
|
|
return [ |
|
140
|
|
|
['blade', 'b', InputOption::VALUE_NONE, 'Create stub Blade view'], |
|
141
|
|
|
['scss', 's', InputOption::VALUE_NONE, 'Create stub SCSS view'], |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|