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\Command\GetStreams; |
10
|
|
|
use Anomaly\Streams\Platform\Addon\Console\Command\WriteAddonSeeder; |
11
|
|
|
use Anomaly\Streams\Platform\Stream\Console\Command\WriteEntitySeeder; |
12
|
|
|
|
13
|
|
|
class SeederMakeCommand extends \Illuminate\Console\Command |
14
|
|
|
{ |
15
|
|
|
use DispatchesJobs; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command name. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'make:seeder'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Create a new seeder class for addon'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The Composer instance. |
33
|
|
|
* |
34
|
|
|
* @var \Illuminate\Support\Composer |
35
|
|
|
*/ |
36
|
|
|
protected $composer; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new command instance. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
42
|
|
|
* @param \Illuminate\Support\Composer $composer |
43
|
|
|
* @return void |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function __construct(Filesystem $files, Composer $composer) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($files); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$this->composer = $composer; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Execute the console command. |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function fire() |
58
|
|
|
{ |
59
|
|
|
if (!$this->confirm('Are you sure? This will overwrite existing files!', false)) |
60
|
|
|
{ |
61
|
|
|
return $this->info('Exit'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/* @var Addon $addon */ |
65
|
|
|
if (!$addon = $this->dispatch(new GetAddon($this->getAddonNamespace()))) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
throw new \Exception('Addon could not be found.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$path = $addon->getPath(); |
71
|
|
|
$type = $addon->getType(); |
72
|
|
|
$slug = $addon->getSlug(); |
73
|
|
|
$vendor = $addon->getVendor(); |
74
|
|
|
|
75
|
|
|
if ($type != 'module' && $type != 'extension') |
76
|
|
|
{ |
77
|
|
|
throw new \Exception('Only {module} and {extension} addon types are allowed!!!'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* @var StreamCollection $streams */ |
81
|
|
|
$streams = $this->getStreams($slug)->each( |
82
|
|
|
function ($stream) use ($addon) |
83
|
|
|
{ |
84
|
|
|
$this->dispatch(new WriteEntitySeeder( |
85
|
|
|
$addon, |
86
|
|
|
$stream->getSlug(), |
87
|
|
|
$stream->getNamespace() |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->dispatch(new WriteAddonSeeder($path, $type, $slug, $vendor, $streams)); |
93
|
|
|
|
94
|
|
|
$this->composer->dumpAutoloads(); |
95
|
|
|
|
96
|
|
|
$this->info('Seeders created successfully.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the options. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
protected function getOptions() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
return array_merge( |
107
|
|
|
parent::getOptions(), |
108
|
|
|
[ |
109
|
|
|
['stream', null, InputOption::VALUE_OPTIONAL, 'The stream slug.'], |
110
|
|
|
['shared', null, InputOption::VALUE_NONE, 'Indicates if the addon should be created in shared addons.'], |
111
|
|
|
] |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the console command arguments. |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function getArguments() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
['namespace', InputArgument::REQUIRED, 'The namespace of the addon'], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets the stream namespace. |
129
|
|
|
* |
130
|
|
|
* @throws \Exception |
131
|
|
|
* @return string The stream namespace. |
132
|
|
|
*/ |
133
|
|
|
public function getAddonNamespace() |
134
|
|
|
{ |
135
|
|
|
$namespace = $this->argument('namespace'); |
136
|
|
|
|
137
|
|
|
if (!str_is('*.*.*', $namespace)) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
throw new \Exception('The namespace should be snake case and formatted like: {vendor}.{type}.{slug}'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $namespace; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the streams. |
147
|
|
|
* |
148
|
|
|
* @param string $slug The addon slug |
149
|
|
|
* @return StreamCollection |
150
|
|
|
*/ |
151
|
|
|
public function getStreams($slug) |
152
|
|
|
{ |
153
|
|
|
return $this->dispatch(new GetStreams($slug))->filter( |
154
|
|
|
function ($stream) |
155
|
|
|
{ |
156
|
|
|
return !str_contains($stream->getSlug(), '_'); |
157
|
|
|
} |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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.