1 <?php
2
3 namespace Scaffolder\Commands;
4
5 use Illuminate\Support\Facades\File;
6 use Illuminate\Support\Facades\Log;
7 use Scaffolder\Compilers\Core\Api\ApiControllerCompiler;
8 use Scaffolder\Compilers\Core\Api\ApiModelCompiler;
9 use Scaffolder\Compilers\Core\Api\ApiRouteCompiler;
10 use Scaffolder\Compilers\Core\ControllerCompiler;
11 use Scaffolder\Compilers\Core\MigrationCompiler;
12 use Scaffolder\Compilers\Core\ModelCompiler;
13 use Scaffolder\Compilers\Core\RouteCompiler;
14 use Scaffolder\Compilers\Layout\CreateLayoutCompiler;
15 use Scaffolder\Compilers\Layout\EditLayoutCompiler;
16 use Scaffolder\Compilers\Layout\PageLayoutCompiler;
17 use Scaffolder\Compilers\View\CreateViewCompiler;
18 use Scaffolder\Compilers\View\DashboardViewCompiler;
19 use Scaffolder\Compilers\View\EditViewCompiler;
20 use Scaffolder\Compilers\View\IndexViewCompiler;
21 use Scaffolder\Compilers\View\LoginViewCompiler;
22 use Scaffolder\Compilers\View\WelcomeViewCompiler;
23 use Scaffolder\Support\Contracts\ScaffolderExtensionInterface;
24 use Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface;
25 use Scaffolder\Support\Contracts\ScaffolderThemeLayoutsInterface;
26 use Scaffolder\Support\Contracts\ScaffolderThemeViewsInterface;
27 use Scaffolder\Support\Directory;
28 use Scaffolder\Support\Json;
29
30 class GeneratorCommand extends BaseCommand
31 {
32 33 34 35
36 protected $signature = 'scaffolder:generate {--api} {--webExecution}';
37
38 39 40 41
42 protected $description = 'Scaffold an application';
43
44 45 46 47
48 protected $stubsDirectory;
49
50 51 52 53
54 protected $themeViews;
55
56 57 58 59
60 protected $themeLayouts;
61
62 63 64 65
66 protected $themeExtension;
67
68 69 70 71
72 protected $extensions;
73
74 75 76 77 78 79 80 81 82 83
84 public function __construct(ScaffolderThemeViewsInterface $themeViews, ScaffolderThemeLayoutsInterface $themeLayouts, ScaffolderThemeExtensionInterface $themeExtension, array $extensions)
85 {
86 parent::__construct();
87
88 foreach ($extensions as $extension)
89 {
90 if (!$extension instanceof ScaffolderExtensionInterface)
91 {
92 throw new \Exception('Scaffolder extensions must implement \Scaffolder\Support\Contracts\ScaffolderExtensionInterface');
93 }
94 }
95
96 $this->themeViews = $themeViews;
97 $this->themeLayouts = $themeLayouts;
98 $this->themeExtension = $themeExtension;
99 $this->extensions = $extensions;
100 $this->stubsDirectory = __DIR__ . '/../../../stubs/';
101 }
102
103 104 105
106 public function handle()
107 {
108 $scaffoldApi = $this->option('api');
109 $webExecution = $this->option('webExecution');
110
111 try
112 {
113
114 $modelFiles = File::allFiles(base_path('scaffolder-config/models/'));
115
116
117 $scaffolderConfig = Json::decodeFile(base_path('scaffolder-config/app.json'));
118
119 $apiDirectory = strtolower(str_replace(' ', '-', $scaffolderConfig->name . '-api'));
120
121
122 $modelCompiler = new ModelCompiler();
123 $apiModelCompiler = new ApiModelCompiler();
124 $migrationCompiler = new MigrationCompiler();
125 $controllerCompiler = new ControllerCompiler();
126 $apiControllerCompiler = new ApiControllerCompiler();
127 $indexViewCompiler = new IndexViewCompiler();
128 $createViewCompiler = new CreateViewCompiler();
129 $editViewCompiler = new EditViewCompiler();
130 $dashboardViewCompiler = new DashboardViewCompiler();
131 $welcomeViewCompiler = new WelcomeViewCompiler();
132 $loginViewCompiler = new LoginViewCompiler();
133 $pageLayoutViewCompiler = new PageLayoutCompiler();
134 $createLayoutCompiler = new CreateLayoutCompiler();
135 $editLayoutCompiler = new EditLayoutCompiler();
136 $routeCompiler = new RouteCompiler();
137 $apiRouteCompiler = new ApiRouteCompiler();
138
139
140 $modelCompilerOutput = [];
141 $apiModelCompilerOutput = [];
142 $controllerCompilerOutput = [];
143 $apiControllerCompilerOutput = [];
144 $viewCompilerOutput = [];
145 $migrationCompilerOutput = [];
146
147
148 $sidenavLinks = [];
149
150
151 $compiledRoutes = '';
152 $compiledApiRoutes = '';
153
154
155 $modelStub = File::get($this->stubsDirectory . 'Model.php');
156 $apiModelStub = File::get($this->stubsDirectory . 'api/Model.php');
157 $migrationStub = File::get($this->stubsDirectory . 'Migration.php');
158 $controllerStub = File::get($this->stubsDirectory . 'Controller.php');
159 $apiControllerStub = File::get($this->stubsDirectory . 'api/Controller.php');
160 $indexViewStub = File::get($this->themeViews->getIndexPath());
161 $createViewStub = File::get($this->themeViews->getCreatePath());
162 $editViewStub = File::get($this->themeViews->getEditPath());
163 $routeStub = File::get($this->stubsDirectory . 'ResourceRoute.php');
164 $apiRouteStub = File::get($this->stubsDirectory . 'api/ResourceRoute.php');
165
166
167 Directory::createIfNotExists(app_path('Models'));
168
169
170 if ($scaffoldApi)
171 {
172 if (!(new \FilesystemIterator(base_path('../' . $apiDirectory)))->valid())
173 {
174 $this->writeStatus('Initializing API ...', $webExecution);
175
176 $this->call('scaffolder:api-initialize', [
177 'name' => $scaffolderConfig->name,
178 'domain' => $scaffolderConfig->api->domain,
179 '--webExecution' => $webExecution
180 ]);
181
182 $this->output->newLine();
183 }
184 else
185 {
186 $this->writeStatus('API already initialized', $webExecution);
187 }
188
189
190 Directory::createIfNotExists(base_path('../' . $apiDirectory . '/app/Models'));
191 }
192
193 $this->writeStatus('Compiling ...', $webExecution);
194 $this->output->newLine();
195
196
197 $this->output->progressStart(count($modelFiles));
198
199
200 foreach ($modelFiles as $modelFile)
201 {
202
203 $modelName = ucwords($modelFile->getBasename('.' . $modelFile->getExtension()));
204
205
206 $modelData = Json::decodeFile($modelFile->getRealPath());
207
208
209 Directory::createIfNotExists(base_path('resources/views/' . strtolower($modelName)));
210
211 $modelHash = md5_file($modelFile->getRealPath());
212
213
214 array_push($modelCompilerOutput, $modelCompiler->compile($modelStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
215 if ($scaffoldApi) array_push($apiModelCompilerOutput, $apiModelCompiler->compile($apiModelStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
216 array_push($controllerCompilerOutput, $controllerCompiler->compile($controllerStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
217 if ($scaffoldApi) array_push($apiControllerCompilerOutput, $apiControllerCompiler->compile($apiControllerStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
218 array_push($migrationCompilerOutput, $migrationCompiler->compile($migrationStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
219 array_push($viewCompilerOutput, $indexViewCompiler->compile($indexViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->themeExtension, $this->extensions));
220 array_push($viewCompilerOutput, $createViewCompiler->compile($createViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->themeExtension, $this->extensions));
221 array_push($viewCompilerOutput, $editViewCompiler->compile($editViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->themeExtension, $this->extensions));
222 $compiledRoutes .= $routeCompiler->compile($routeStub, $modelName, $modelData, $scaffolderConfig, null, $this->extensions);
223 if ($scaffoldApi) $compiledApiRoutes .= $apiRouteCompiler->compile($apiRouteStub, $modelName, $modelData, $scaffolderConfig, null, $this->extensions);
224
225
226 array_push($sidenavLinks, ['modelName' => $modelName, 'modelLabel' => $modelData->modelLabel]);
227
228
229 $this->output->progressAdvance();
230 }
231
232
233 $this->output->progressFinish();
234
235
236 $routeCompiler->compileGroup(File::get($this->stubsDirectory . 'Routes.php'), $compiledRoutes, $scaffolderConfig);
237 if ($scaffoldApi) $apiRouteCompiler->compileGroup(File::get($this->stubsDirectory . 'api/Routes.php'), $compiledApiRoutes, $scaffolderConfig);
238
239
240 Directory::createIfNotExists(base_path('resources/views/layouts'));
241
242
243 array_push($viewCompilerOutput, $pageLayoutViewCompiler->compile(File::get($this->themeLayouts->getPagePath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions, ['links' => $sidenavLinks]));
244
245
246 array_push($viewCompilerOutput, $createLayoutCompiler->compile(File::get($this->themeLayouts->getCreatePath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
247
248
249 array_push($viewCompilerOutput, $editLayoutCompiler->compile(File::get($this->themeLayouts->getEditPath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
250
251
252 array_push($viewCompilerOutput, $dashboardViewCompiler->compile(File::get($this->themeViews->getDashboardPath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
253
254
255 array_push($viewCompilerOutput, $welcomeViewCompiler->compile(File::get($this->themeViews->getWelcomePath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
256
257
258 array_push($viewCompilerOutput, $loginViewCompiler->compile(File::get($this->themeViews->getLoginPath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
259
260
261 $this->comment('- Files created');
262
263 $this->comment('- - Views');
264 foreach ($viewCompilerOutput as $viewFile)
265 {
266 $this->info('- - - ' . $viewFile);
267 }
268
269 $this->comment('- - Controllers');
270 foreach ($controllerCompilerOutput as $controllerFile)
271 {
272 $this->info('- - - ' . $controllerFile);
273 }
274
275 $this->comment('- - Migrations');
276 foreach ($migrationCompilerOutput as $migrationFile)
277 {
278 $this->info('- - - ' . $migrationFile);
279 }
280
281 $this->comment('- - Models');
282 foreach ($modelCompilerOutput as $modelFile)
283 {
284 $this->info('- - - ' . $modelFile);
285 }
286
287 if ($scaffoldApi)
288 {
289 $this->comment('- - API Controllers');
290 foreach ($apiControllerCompilerOutput as $apiControllerFile)
291 {
292 $this->info('- - - ' . $apiControllerFile);
293 }
294
295 $this->comment('- - API Models');
296 foreach ($apiModelCompilerOutput as $apiModelFile)
297 {
298 $this->info('- - - ' . $apiModelFile);
299 }
300 }
301
302 $this->writeStatus('Done', $webExecution);
303 }
304 catch (\Exception $exception)
305 {
306 $this->writeStatus('Error', $webExecution);
307 Log::error($exception->getMessage());
308 }
309 }
310 }
311