1
|
|
|
<?php namespace Anomaly\Streams\Platform\Database\Seeder\Console; |
2
|
|
|
|
3
|
|
|
use \Illuminate\Support\Composer; |
4
|
|
|
use \Illuminate\Filesystem\Filesystem; |
5
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Anomaly\Streams\Platform\Addon\Command\GetAddon; |
9
|
|
|
use Anomaly\Streams\Platform\Stream\StreamCollection; |
10
|
|
|
use Anomaly\Streams\Platform\Stream\Command\GetStreams; |
11
|
|
|
use Anomaly\Streams\Platform\Addon\Console\Command\WriteAddonSeeder; |
12
|
|
|
use Anomaly\Streams\Platform\Stream\Console\Command\WriteEntitySeeder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @TODO alarm about replacing |
16
|
|
|
*/ |
17
|
|
|
class SeederMakeCommand extends \Illuminate\Console\Command |
18
|
|
|
{ |
19
|
|
|
use DispatchesJobs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name = 'make:seeder'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Create a new seeder class for addon'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* All streams string value |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $allChoice = 'All streams'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The Composer instance. |
44
|
|
|
* |
45
|
|
|
* @var \Illuminate\Support\Composer |
46
|
|
|
*/ |
47
|
|
|
protected $composer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new command instance. |
51
|
|
|
* |
52
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
53
|
|
|
* @param \Illuminate\Support\Composer $composer |
54
|
|
|
* @return void |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
public function __construct(Filesystem $files, Composer $composer) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($files); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->composer = $composer; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the console command. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function fire() |
69
|
|
|
{ |
70
|
|
|
/* @var Addon $addon */ |
71
|
|
|
if (!$addon = $this->dispatch(new GetAddon($this->getAddonNamespace()))) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
throw new \Exception('Addon could not be found.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$path = $addon->getPath(); |
77
|
|
|
$type = $addon->getType(); |
78
|
|
|
$slug = $addon->getSlug(); |
79
|
|
|
$vendor = $addon->getVendor(); |
80
|
|
|
|
81
|
|
|
if ($type != 'module' && $type != 'extension') |
82
|
|
|
{ |
83
|
|
|
throw new \Exception('Only {module} and {extension} addon types are allowed!!!'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!$this->confirm('Next action will OVERWRITE existing seeders which you would set! Do you agree?')) |
87
|
|
|
{ |
88
|
|
|
$this->error('Exiting...'); |
89
|
|
|
|
90
|
|
|
exit; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* @var StreamCollection $streams */ |
94
|
|
|
$streams = $this->getStreams($slug); |
95
|
|
|
|
96
|
|
|
$answers = $this->makeQuestion($streams); |
97
|
|
|
|
98
|
|
|
if (array_search($this->getAllChoice(), $answers) === false) |
99
|
|
|
{ |
100
|
|
|
$streams = $streams->filter( |
101
|
|
|
function ($stream) use ($answers) |
102
|
|
|
{ |
103
|
|
|
return array_search(ucfirst($stream->getSlug()), $answers) !== false; |
104
|
|
|
} |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$streams->each( |
109
|
|
|
function ($stream) use ($addon) |
110
|
|
|
{ |
111
|
|
|
$slug = $stream->getSlug(); |
112
|
|
|
|
113
|
|
|
$this->dispatch(new WriteEntitySeeder( |
114
|
|
|
$addon, |
115
|
|
|
$slug, |
116
|
|
|
$stream->getNamespace() |
117
|
|
|
)); |
118
|
|
|
|
119
|
|
|
$singular = ucfirst(str_singular($slug)); |
120
|
|
|
$path = "{$addon->getPath()}/src/{$singular}/{$singular}Seeder.php"; |
121
|
|
|
|
122
|
|
|
$this->comment("Seeder for {$slug} created successfully."); |
123
|
|
|
$this->line("Path: {$path}"); |
124
|
|
|
$this->line(''); |
125
|
|
|
} |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->dispatch(new WriteAddonSeeder($path, $type, $slug, $vendor, $streams)); |
129
|
|
|
|
130
|
|
|
$this->composer->dumpAutoloads(); |
131
|
|
|
|
132
|
|
|
$this->info('Seeders created successfully.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets the addon's stream namespace. |
137
|
|
|
* |
138
|
|
|
* @throws \Exception |
139
|
|
|
* @return string The stream namespace. |
140
|
|
|
*/ |
141
|
|
|
public function getAddonNamespace() |
142
|
|
|
{ |
143
|
|
|
$namespace = $this->argument('namespace'); |
144
|
|
|
|
145
|
|
|
if (!str_is('*.*.*', $namespace)) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
throw new \Exception('The namespace should be snake case and formatted like: {vendor}.{type}.{slug}'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $namespace; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Gets the root streams of addon. |
155
|
|
|
* |
156
|
|
|
* @param string $slug The addon slug |
157
|
|
|
* @return StreamCollection |
158
|
|
|
*/ |
159
|
|
|
public function getStreams($slug) |
160
|
|
|
{ |
161
|
|
|
return $this->dispatch(new GetStreams($slug))->filter( |
162
|
|
|
function ($stream) |
163
|
|
|
{ |
164
|
|
|
return !str_contains($stream->getSlug(), '_'); |
165
|
|
|
} |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get `all` value. |
171
|
|
|
* |
172
|
|
|
* @return string All value. |
173
|
|
|
*/ |
174
|
|
|
public function getAllChoice() |
175
|
|
|
{ |
176
|
|
|
return $this->allChoice; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Makes a question about streams to make seeders for. |
181
|
|
|
* |
182
|
|
|
* @param StreamCollection $streams The streams |
183
|
|
|
* @return array Answers |
184
|
|
|
*/ |
185
|
|
|
public function makeQuestion(StreamCollection $streams) |
186
|
|
|
{ |
187
|
|
|
$choices = $streams->map( |
188
|
|
|
function ($stream) |
189
|
|
|
{ |
190
|
|
|
return ucfirst($stream->getSlug()); |
191
|
|
|
} |
192
|
|
|
)->prepend($this->getAllChoice())->toArray(); |
193
|
|
|
|
194
|
|
|
return $this->choice( |
195
|
|
|
'Please choose the addon\'s streams to create seeders for (common separated if multiple)', |
196
|
|
|
$choices, 0, null, true |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the options. |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
View Code Duplication |
protected function getOptions() |
|
|
|
|
206
|
|
|
{ |
207
|
|
|
return array_merge( |
208
|
|
|
parent::getOptions(), |
209
|
|
|
[ |
210
|
|
|
['stream', null, InputOption::VALUE_OPTIONAL, 'The stream slug.'], |
211
|
|
|
['shared', null, InputOption::VALUE_NONE, 'Indicates if the addon should be created in shared addons.'], |
212
|
|
|
] |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get the console command arguments. |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
protected function getArguments() |
222
|
|
|
{ |
223
|
|
|
return [ |
224
|
|
|
['namespace', InputArgument::REQUIRED, 'The namespace of the addon'], |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.