1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Spatie\Sheets\Facades\Sheets; |
8
|
|
|
use Spatie\Sheets\Repositories\FilesystemRepository; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class MakePageCommand extends GeneratorCommand |
13
|
|
|
{ |
14
|
|
|
protected $name = 'make:page'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
protected $description = 'Create a new page for stancy package.'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
public function handle() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$this->createSheet(); |
21
|
|
|
|
22
|
|
|
return parent::handle(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function createSheet(): void |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$name = $this->getNameInput(); |
28
|
|
|
$collection = $this->option('collection'); |
29
|
|
|
|
30
|
|
|
if (! $collection) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$repository = Sheets::collection($collection); |
35
|
|
|
|
36
|
|
|
if (! $repository instanceof FilesystemRepository) { |
37
|
|
|
$this->warn('can not create a sheet if collection is not instance of `'.FilesystemRepository::class.'`'); |
38
|
|
|
|
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$filename = Str::kebab(class_basename($name)).'.'.$repository->getExtension(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
if ( |
45
|
|
|
(! $this->hasOption('force') || ! $this->option('force')) |
46
|
|
|
&& $repository->getFilesystem()->exists($filename) |
|
|
|
|
47
|
|
|
) { |
48
|
|
|
$this->error('the sheet `'.$filename.'` already exists'); |
49
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$repository->getFilesystem()->put($filename, $this->getDefaultSheetContent($repository->getExtension())); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getDefaultSheetContent(string $extension): string |
57
|
|
|
{ |
58
|
|
|
if (! in_array($extension, ['md', 'json', 'yaml', 'yml'])) { |
59
|
|
|
return ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$data = [ |
63
|
|
|
'_pageData' => '\\'.$this->qualifyClass($this->getNameInput()), |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
if ($extension == 'json') { |
|
|
|
|
67
|
|
|
return json_encode($data); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (in_array($extension, ['yaml', 'yml'])) { |
71
|
|
|
return Yaml::dump($data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return implode(PHP_EOL, [ |
75
|
|
|
'---', |
76
|
|
|
trim(Yaml::dump(array_merge($data, ['_view' => null]))), |
77
|
|
|
'---', |
78
|
|
|
'', |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getStub(): string |
83
|
|
|
{ |
84
|
|
|
return __DIR__.'/../../../../stancy/resources/stubs/page.stub'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getDefaultNamespace($rootNamespace): string |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return rtrim($rootNamespace, '\\').'\Pages'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function replaceClass($stub, $name): string |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return parent::replaceClass($stub, $name); |
95
|
|
|
} |
|
|
|
|
96
|
|
|
|
97
|
|
|
public function getOptions() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return [ |
100
|
|
|
['collection', null, InputOption::VALUE_REQUIRED, 'The sheet collection to create the page in'], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|