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