16
16
use Lucid \Console \Filesystem ;
17
17
use Illuminate \Console \GeneratorCommand ;
18
18
use Illuminate \Config \Repository as Config ;
19
+ use Lucid \Console \Generators \ServiceGenerator ;
19
20
use Symfony \Component \Console \Input \InputOption ;
20
21
use Symfony \Component \Console \Input \InputInterface ;
21
22
use Symfony \Component \Console \Output \OutputInterface ;
@@ -56,35 +57,14 @@ class ServiceMakeCommand extends GeneratorCommand
56
57
*/
57
58
protected $ description = 'Create a new Service ' ;
58
59
59
- /**
60
- * The directories to be created under the service directory.
61
- *
62
- * @var array
63
- */
64
- protected $ directories = [
65
- 'Console/ ' ,
66
- 'Database/ ' ,
67
- 'Database/Migrations/ ' ,
68
- 'Database/Seeds/ ' ,
69
- 'Http/ ' ,
70
- 'Http/Controllers/ ' ,
71
- 'Http/Middleware/ ' ,
72
- 'Http/Requests/ ' ,
73
- 'Providers/ ' ,
74
- 'Features/ ' ,
75
- 'resources/ ' ,
76
- 'resources/lang/ ' ,
77
- 'resources/views/ ' ,
78
- ];
79
-
80
60
/**
81
61
* Get the stub file for the generator.
82
62
*
83
63
* @return string
84
64
*/
85
65
protected function getStub ()
86
66
{
87
- return __DIR__ .'/stubs/service.stub ' ;
67
+ return __DIR__ .'/../Generators/ stubs/service.stub ' ;
88
68
}
89
69
90
70
/**
@@ -94,37 +74,19 @@ protected function getStub()
94
74
*/
95
75
public function fire ()
96
76
{
97
- $ name = Str::service ($ this ->getNameInput ());
98
-
99
- $ slug = snake_case ($ name );
100
-
101
- $ path = $ this ->getPath ($ name );
102
-
103
- if ($ this ->files ->exists ($ path )) {
104
- $ this ->error ('Service already exists! ' );
105
-
106
- return false ;
107
- }
108
-
109
- // create service directory
110
- $ this ->createDirectory ($ path );
111
- // create .gitkeep file in it
112
- $ this ->createFile ($ path .'/.gitkeep ' );
113
-
114
- $ this ->createServiceDirectories ($ path );
115
-
116
- $ this ->addServiceProviders ($ name , $ slug , $ path );
77
+ try {
117
78
118
- $ this -> addRoutesFile ( $ name, $ slug , $ path );
79
+ $ name = $ this -> getNameInput ( );
119
80
120
- $ this ->addWelcomeViewFile ($ path );
81
+ $ generator = app (ServiceGenerator::class);
82
+ $ service = $ generator ->generate ($ name );
121
83
122
- $ this ->info ('Service ' .$ name .' created successfully. ' ."\n" );
84
+ $ this ->info ('Service ' .$ service -> name .' created successfully. ' ."\n" );
123
85
124
86
$ rootNamespace = $ this ->findRootNamespace ();
125
- $ serviceNamespace = $ this ->findServiceNamespace ($ name );
87
+ $ serviceNamespace = $ this ->findServiceNamespace ($ service -> name );
126
88
127
- $ serviceProvider = $ serviceNamespace .'\\Providers \\' .$ name .'ServiceProvider ' ;
89
+ $ serviceProvider = $ serviceNamespace .'\\Providers \\' .$ service -> name .'ServiceProvider ' ;
128
90
129
91
$ this ->info ('Activate it by registering ' .
130
92
'<comment> ' .$ serviceProvider .'</comment> ' .
@@ -135,117 +97,9 @@ public function fire()
135
97
);
136
98
137
99
$ this ->info ('<comment>$this->app->register( \'' .$ serviceProvider .'\');</comment> ' ."\n" );
138
- }
139
-
140
- /**
141
- * {@inheritdoc}
142
- */
143
- public function getPath ($ name )
144
- {
145
- return $ this ->findServicePath ($ name );
146
- }
147
-
148
- /**
149
- * Create the default directories at the given service path.
150
- *
151
- * @param string $path
152
- *
153
- * @return void
154
- */
155
- public function createServiceDirectories ($ path )
156
- {
157
- foreach ($ this ->directories as $ directory ) {
158
- $ this ->createDirectory ($ path .'/ ' .$ directory );
159
- $ this ->createFile ($ path .'/ ' .$ directory .'/.gitkeep ' );
100
+ } catch (\Exception $ e ) {
101
+ dd ($ e ->getMessage (), $ e ->getFile (), $ e ->getLine ());
160
102
}
161
103
}
162
104
163
- /**
164
- * Add the corresponding service provider for the created service.
165
- *
166
- * @param string $name
167
- * @param string $path
168
- *
169
- * @return bool
170
- */
171
- public function addServiceProviders ($ name , $ slug , $ path )
172
- {
173
- $ namespace = $ this ->findServiceNamespace ($ name ).'\\Providers ' ;
174
-
175
- $ this ->createRegistrationServiceProvider ($ name , $ path , $ slug , $ namespace );
176
-
177
- $ this ->createRouteServiceProvider ($ name , $ path , $ slug , $ namespace );
178
- }
179
-
180
- /**
181
- * Create the service provider that registers this service.
182
- *
183
- * @param string $name
184
- * @param string $path
185
- */
186
- public function createRegistrationServiceProvider ($ name , $ path , $ slug , $ namespace )
187
- {
188
- $ content = file_get_contents (__DIR__ .'/stubs/serviceprovider.stub ' );
189
- $ content = str_replace (
190
- ['{{name}} ' , '{{slug}} ' , '{{namespace}} ' ],
191
- [$ name , $ slug , $ namespace ],
192
- $ content
193
- );
194
-
195
- $ this ->createFile ($ path .'/Providers/ ' .$ name .'ServiceProvider.php ' , $ content );
196
- }
197
-
198
- /**
199
- * Create the routes service provider file.
200
- *
201
- * @param string $name
202
- * @param string $path
203
- * @param string $slug
204
- * @param string $namespace
205
- */
206
- public function createRouteServiceProvider ($ name , $ path , $ slug , $ namespace )
207
- {
208
- $ serviceNamespace = $ this ->findServiceNamespace ($ name );
209
- $ controllers = $ serviceNamespace .'\Http\Controllers ' ;
210
- $ foundation = $ this ->findFoundationNamespace ();
211
-
212
- $ content = file_get_contents (__DIR__ .'/stubs/routeserviceprovider.stub ' );
213
- $ content = str_replace (
214
- ['{{name}} ' , '{{namespace}} ' , '{{controllers_namespace}} ' , '{{foundation_namespace}} ' ],
215
- [$ name , $ namespace , $ controllers , $ foundation ],
216
- $ content
217
- );
218
-
219
- $ this ->createFile ($ path .'/Providers/RouteServiceProvider.php ' , $ content );
220
- }
221
-
222
- /**
223
- * Add the routes file.
224
- *
225
- * @param string $name
226
- * @param string $slug
227
- * @param string $path
228
- */
229
- public function addRoutesFile ($ name , $ slug , $ path )
230
- {
231
- $ controllers = 'src/Services/ ' .$ name .'/Http/Controllers ' ;
232
-
233
- $ content = file_get_contents (__DIR__ .'/stubs/routes.stub ' );
234
- $ content = str_replace (['{{slug}} ' , '{{controllers_path}} ' ], [$ slug , $ controllers ], $ content );
235
-
236
- $ this ->createFile ($ path .'/Http/routes.php ' , $ content );
237
- }
238
-
239
- /**
240
- * Add the welcome view file.
241
- *
242
- * @param string $path
243
- */
244
- public function addWelcomeViewFile ($ path )
245
- {
246
- $ this ->createFile (
247
- $ path .'/resources/views/welcome.blade.php ' ,
248
- file_get_contents (__DIR__ .'/stubs/welcome.blade.stub ' )
249
- );
250
- }
251
105
}
0 commit comments