3
3
namespace CloudCreativity \LaravelJsonApi \Console \Commands ;
4
4
5
5
use Illuminate \Console \Command ;
6
+ use CloudCreativity \LaravelJsonApi \Console \Commands \RequestMakeCommand ;
7
+ use CloudCreativity \LaravelJsonApi \Console \Commands \ValidatorsMakeCommand ;
8
+ use CloudCreativity \LaravelJsonApi \Console \Commands \HydratorMakeCommand ;
9
+ use CloudCreativity \LaravelJsonApi \Console \Commands \SchemaMakeCommand ;
10
+ use CloudCreativity \LaravelJsonApi \Console \Commands \SearchMakeCommand ;
11
+ use Illuminate \Support \Collection ;
12
+ use Symfony \Component \Console \Input \InputOption ;
13
+ use Symfony \Component \Console \Input \InputArgument ;
6
14
7
15
class ResourceMakeCommand extends Command
8
16
{
9
17
/**
10
18
* The name and signature of the console command.
11
19
*
12
- * @var string
13
- */
14
- protected $ signature = 'make:json-api:resource
15
- {resource : The resource to create files for}
16
- {--eloquent : Use eloquent as adapter} ' ;
20
+ * @var string */
21
+ protected $ name = 'make:json-api:resource ' ;
17
22
18
23
/**
19
24
* The console command description.
@@ -22,23 +27,19 @@ class ResourceMakeCommand extends Command
22
27
*/
23
28
protected $ description = 'Create a full Json Api resource. ' ;
24
29
25
- private $ commands = [
26
- 'make:json-api:hydrator ' ,
27
- 'make:json-api:schema ' ,
28
- 'make:json-api:search ' ,
29
- 'make:json-api:request ' ,
30
- 'make:json-api:validators ' ,
31
- ];
32
-
33
30
/**
34
- * Create a new command instance .
31
+ * The available generator commands .
35
32
*
36
- * @return void
33
+ * @var array
37
34
*/
38
- public function __construct ()
39
- {
40
- parent ::__construct ();
41
- }
35
+ private $ commands = [
36
+ 'make:json-api:request ' => RequestMakeCommand::class,
37
+ 'make:json-api:validators ' => ValidatorsMakeCommand::class,
38
+
39
+ 'make:json-api:hydrator ' => HydratorMakeCommand::class,
40
+ 'make:json-api:schema ' => SchemaMakeCommand::class,
41
+ 'make:json-api:search ' => SearchMakeCommand::class,
42
+ ];
42
43
43
44
/**
44
45
* Execute the console command.
@@ -50,17 +51,123 @@ public function handle()
50
51
$ resourceParameter = [
51
52
'resource ' => $ this ->argument ('resource ' ),
52
53
];
53
- $ eloquentParameter = [
54
+ $ adapterParameters = array_merge ( $ resourceParameter , [
54
55
'--eloquent ' => $ this ->option ('eloquent ' ),
55
- ];
56
+ '--no-eloquent ' => $ this ->option ('no-eloquent ' ),
57
+ ]);
58
+
59
+ $ commands = collect ($ this ->commands );
60
+
61
+ // Filter out any commands the user asked os to.
62
+ if ($ this ->option ('only ' ) || $ this ->option ('except ' )) {
63
+ $ type = $ this ->option ('only ' ) ? 'only ' : 'except ' ;
64
+
65
+ $ commands = $ this ->filterCommands ($ commands , $ type );
66
+ }
56
67
57
- // Call independent generators
58
- $ this ->call ('make:json-api:validators ' , $ resourceParameter );
59
- $ this ->call ('make:json-api:request ' , $ resourceParameter );
68
+ // The search unit is only for eloquent.
69
+ if ( ! $ this ->isEloquent ()) {
70
+ $ commands ->forget ('make:json-api:search ' );
71
+ }
72
+
73
+ // Run independent commands.
74
+ $ this ->runCommandsWithParameters ($ commands ->only ([
75
+ 'make:json-api:request ' ,
76
+ 'make:json-api:validators ' ,
77
+ ]), $ resourceParameter );
78
+
79
+ // Run adapter commands.
80
+ $ this ->runCommandsWithParameters ($ commands ->only ([
81
+ 'make:json-api:hydrator ' ,
82
+ 'make:json-api:schema ' ,
83
+ 'make:json-api:search ' ,
84
+ ]), $ adapterParameters );
85
+
86
+ // Just tell the user, if no files are created
87
+ if ($ commands ->isEmpty ()) {
88
+ $ this ->info ('No files created. ' );
89
+ }
90
+
91
+ // Give the user a digial high-five.
92
+ $ this ->comment ('All done, keep doing what you do. ' );
93
+ }
94
+
95
+ /**
96
+ * Filters out commands using either 'except' or 'only' filter.
97
+ *
98
+ * @param Collection $commands
99
+ * @param string $type
100
+ *
101
+ * @return Collection
102
+ */
103
+ private function filterCommands (Collection $ commands , $ type )
104
+ {
105
+ $ baseCommandName = 'make:json-api: ' ;
106
+ $ filterValues = explode (', ' , $ this ->option ($ type ));
107
+
108
+ $ targetCommands = collect ($ filterValues )
109
+ ->map (function ($ target ) use ($ baseCommandName ) {
110
+ return $ baseCommandName . strtolower (trim ($ target ));
111
+ });
112
+
113
+ return $ commands ->{$ type }($ targetCommands ->toArray ());
114
+ }
60
115
61
- // Call configurable commands
62
- $ this ->call ('make:json-api:hydrator ' , array_merge ($ resourceParameter , $ eloquentParameter ));
63
- $ this ->call ('make:json-api:schema ' , array_merge ($ resourceParameter , $ eloquentParameter ));
64
- $ this ->call ('make:json-api:search ' , array_merge ($ resourceParameter , $ eloquentParameter ));
116
+ /**
117
+ * Runs the given commands and paases them all the given parameters.
118
+ *
119
+ * @param Collection $commands
120
+ * @param array $parameters
121
+ *
122
+ * @return void
123
+ */
124
+ private function runCommandsWithParameters (Collection $ commands , array $ parameters )
125
+ {
126
+ $ commands ->keys ()->each (function ($ command ) use ($ parameters ) {
127
+ $ this ->call ($ command , $ parameters );
128
+ });
129
+ }
130
+
131
+ /**
132
+ * Determine whether the generator should use eloquent or not.
133
+ *
134
+ * @return boolean
135
+ */
136
+ private function isEloquent ()
137
+ {
138
+ $ useEloquent = config ('json-api.generator.use-eloquent ' , true );
139
+
140
+ if ($ this ->option ('no-eloquent ' )) {
141
+ return false ;
142
+ }
143
+
144
+ return $ this ->option ('eloquent ' ) ?: $ useEloquent ;
145
+ }
146
+
147
+ /**
148
+ * Get the console command arguments.
149
+ *
150
+ * @return array
151
+ */
152
+ protected function getArguments ()
153
+ {
154
+ return [
155
+ ['resource ' , InputArgument::REQUIRED , 'The resource to create files for ' ],
156
+ ];
157
+ }
158
+
159
+ /**
160
+ * Get the console command options.
161
+ *
162
+ * @return array
163
+ */
164
+ protected function getOptions ()
165
+ {
166
+ return [
167
+ ['eloquent ' , 'e` ' , InputOption::VALUE_NONE , 'Use eloquent as adapter ' ],
168
+ ['no-eloquent ' , 'ne ' , InputOption::VALUE_NONE , 'Use an abstract adapter ' ],
169
+ ['only ' , 'o ' , InputOption::VALUE_OPTIONAL , 'Specifiy the exact resources you \'d like. ' ],
170
+ ['except ' , 'ex ' , InputOption::VALUE_OPTIONAL , 'Specifiy the resources you \'d like to skip. ' ],
171
+ ];
65
172
}
66
173
}
0 commit comments