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