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