GeneratorCommand::getAllModelsData()   F
last analyzed

Complexity

Conditions 16
Paths 441

Size

Total Lines 130
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 3.4909
c 0
b 0
f 0
cc 16
eloc 67
nc 441
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Scaffolder\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\File;
7
// API classes
8
use Scaffolder\Compilers\Core\ControllerCompiler;
9
use Scaffolder\Compilers\Core\MigrationCompiler;
10
use Scaffolder\Compilers\Core\ModelCompiler;
11
use Scaffolder\Compilers\Core\RouteCompiler;
12
13
// BLADE classes
14
use Scaffolder\Compilers\Blade\CreateViewCompiler;
15
use Scaffolder\Compilers\Blade\EditViewCompiler;
16
use Scaffolder\Compilers\Blade\IndexViewCompiler;
17
use Scaffolder\Compilers\Blade\PageLayoutCompiler;
18
// AngularJs classes
19
use Scaffolder\Compilers\AngularJs\ResourceCompiler;
20
use Scaffolder\Compilers\AngularJs\ModuleCompiler;
21
use Scaffolder\Compilers\AngularJs\TranslateCompiler;
22
use Scaffolder\Compilers\AngularJs\IndexModuleCompiler;
23
use Scaffolder\Compilers\AngularJs\IndexApiCompiler;
24
25
// register
26
use Scaffolder\Compilers\AngularJs\RegisterModuleCompiler;
27
use Scaffolder\Compilers\AngularJs\RegisterControllerCompiler;
28
use Scaffolder\Compilers\AngularJs\RegisterTemplateCompiler;
29
30
// list
31
use Scaffolder\Compilers\AngularJs\ListModuleCompiler;
32
use Scaffolder\Compilers\AngularJs\ListControllerCompiler;
33
use Scaffolder\Compilers\AngularJs\ListTemplateCompiler;
34
use Scaffolder\Compilers\AngularJs\ListDetailCompiler;
35
use Scaffolder\Compilers\AngularJs\ListChooseColumnsCompiler;
36
37
// Support classes
38
use Scaffolder\Support\Directory;
39
use Scaffolder\Support\Json;
40
use Scaffolder\Support\Arrays;
41
use Scaffolder\Support\CamelCase;
42
use Scaffolder\Themes\IScaffolderThemeLayouts;
43
use Scaffolder\Themes\IScaffolderThemeViews;
44
use Scaffolder\Compilers\Support\PathParser;
45
46
use stdClass ;
47
48
class GeneratorCommand extends Command
49
{
50
	protected $signature = 'scaffolder:generate {app=laravel} {--c|clear-all : Clears cache and drafts before generate}';
51
52
	protected $description = 'Scaffold an application';
53
54
	protected $stubsDirectory;
55
56
	public function __construct()
57
	{
58
		parent::__construct();
59
60
		$this->stubsDirectory = __DIR__ . '/../../../stubs/';
61
	}
62
63
	/**
64
	 * Execute the Command.
65
	 * @return void
66
	 */
67
	public function handle()
68
	{
69
		// check if is to clear cache
70
		if($this->option('clear-all')){
71
			$this->call('scaffolder:clear');
72
		}
73
74
		// Create drafs directory
75
		Directory::createIfNotExists(base_path('drafts'));
76
77
		switch ($this->argument('app')) {
78
			case 'api':
79
				$this->handleApi();
80
				break;
81
82
			case 'angularjs':
83
				$this->handleAngularJs();
84
				break;
85
86
			case 'ionic':
87
				# code...
88
				break;
89
90
			case 'android':
91
				#TODO implement code...
92
				break;
93
94
			case 'ios':
95
				#TODO implement code...
96
				break;
97
			
98
			case 'blade':
99
				$this->handleBlade();
100
				break;
101
				
102
			default:
103
				$this->handleLaravel();
104
				break;
105
		}
106
107
		
108
	}
109
110
	/**
111
	 * API Generation command for API files.  
112
	 * @return void
113
	 */
114
	private function handleApi(){
115
		// Get all the models
116
		$modelsData = $this->getAllModelsData();
117
118
		// Start progress bar
119
		$this->output->progressStart(count($modelsData));
120
121
		// Get app config
122
		$scaffolderConfig = $this->getScaffolderConfig();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $scaffolderConfig is correct as $this->getScaffolderConfig() (which targets Scaffolder\Commands\Gene...::getScaffolderConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
123
124
		// Compiler output
125
		$modelCompilerOutput = [];
126
		$controllerCompilerOutput = [];
127
		$migrationCompilerOutput = [];
128
129
		// Sidenav links
130
		$sidenavLinks = [];
131
132
		// Compiled routes
133
		$compiledRoutes = '';
134
135
		// Create route compiler
136
		$routeCompiler = new RouteCompiler($scaffolderConfig);
137
138
		// Create models directory
139
		Directory::createIfNotExists(app_path('Models'));
140
141
		// Create drafts directory
142
		// migrations
143
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->migrations), 0755, true);
144
		// models
145
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->models), 0755, true);
146
		// repositories
147
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->repositories), 0755, true);
148
		// controllers
149
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->controllers), 0755, true);
150
		// routes
151
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->routes), 0755, true);
152
		
153
		// Iterate over models data
154
		foreach ($modelsData as $modelData)
155
		{
156
			
157
			// Create Compilers
158
			$stubModel = null;
159
			$stubController = null;
160
			
161
			if ($modelData->tableName == "file") {
162
				$stubModel = 'Model/FileModel.php';
163
				$stubController = 'Controller/FileController.php';
164
			}
165
166
			$modelCompiler = new ModelCompiler($scaffolderConfig, $modelData, $stubModel);
167
			$migrationCompiler = new MigrationCompiler($scaffolderConfig, $modelData);
168
			$controllerCompiler = new ControllerCompiler($scaffolderConfig, $modelData, $stubController);
169
170
			// Compile stubs
171
			array_push($modelCompilerOutput, $modelCompiler->compile());
172
			array_push($controllerCompilerOutput, $controllerCompiler->compile());
173
			array_push($migrationCompilerOutput, $migrationCompiler->compile());
174
			
175
			$compiledRoutes .= $routeCompiler->replaceResource($modelData);
176
			//
177
178
			// Add entity link
179
			array_push($sidenavLinks, $modelData->modelName);
180
181
			// Advance progress
182
			$this->output->progressAdvance();
183
		}
184
185
		// Store compiled routes
186
		$routeCompiler->compileGroup($compiledRoutes);
187
188
		// Finish progress
189
		$this->output->progressFinish();
190
191
		// Summary
192
		$this->comment('- Files created');
193
194
		$this->comment('- - Controllers');
195
		foreach ($controllerCompilerOutput as $controllerFile)
196
		{
197
			$this->info('- - - ' . $controllerFile);
198
		}
199
200
		$this->comment('- - Migrations');
201
		foreach ($migrationCompilerOutput as $migrationFile)
202
		{
203
			$this->info('- - - ' . $migrationFile);
204
		}
205
206
		$this->comment('- - Models');
207
		foreach ($modelCompilerOutput as $modelFile)
208
		{
209
			$this->info('- - - ' . $modelFile);
210
		}
211
	}
212
213
	/**
214
	 * Code Generation command for AngularJs Material design files.  
215
	 * @return void
216
	 */
217
	private function handleAngularJs(){
218
		// Get all the models
219
		$modelsData = $this->getAllModelsData();
220
221
		// Start progress bar
222
		$this->output->progressStart((count($modelsData) * 2) + (count($modelsData) * 2 * 4) + 1);
223
224
		// Get app config
225
		$scaffolderConfig = $this->getScaffolderConfig();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $scaffolderConfig is correct as $this->getScaffolderConfig() (which targets Scaffolder\Commands\Gene...::getScaffolderConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
226
227
		// Compiler output
228
		$resourceCompilerOutput = [];
229
		$moduleCompilerOutput = [];
230
		$translateCompilerOutput = [];
231
		// register
232
		$registerModuleCompilerOutput = [];
233
		$registerControllerCompilerOutput = [];
234
		$registerTemplateCompilerOutput = [];
235
		
236
		// list
237
		$listModuleCompilerOutput = [];
238
		$listControllerCompilerOutput = [];
239
		$listTemplateCompilerOutput = [];
240
		//$listDetailCompilerOutput = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
241
		//$listChooseColumnsCompilerOutput = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
242
243
		// Compiled indexes
244
		$compiledIndexes = '';
245
	
246
		// Create index compiler
247
		$indexModuleCompiler = new IndexModuleCompiler($scaffolderConfig);
248
249
		$indexApiCompiler = new IndexApiCompiler($scaffolderConfig);
250
251
		// Create drafts directory
252
		// pages
253
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->pages), 0755, true);
254
		
255
		$intKey = 1;
256
		// Iterate over models data
257
		foreach ($modelsData as $modelData)
258
		{
259
			
260
			// Create Compilers
261
			$resourceCompiler = new ResourceCompiler($scaffolderConfig, $modelData);
262
			$moduleCompiler = new ModuleCompiler($scaffolderConfig, $modelData);
263
			// register
264
			$registerModuleCompiler = new RegisterModuleCompiler($scaffolderConfig, $modelData);
265
			$registerControllerCompiler = new RegisterControllerCompiler($scaffolderConfig, $modelData);
266
			$registerTemplateCompiler = new RegisterTemplateCompiler($scaffolderConfig, $modelData);
267
			// translate
268
			$translateCompiler = new TranslateCompiler($scaffolderConfig, $modelData);
269
			// list
270
			$listModuleCompiler = new ListModuleCompiler($scaffolderConfig, $modelData);
271
			$listControllerCompiler = new ListControllerCompiler($scaffolderConfig, $modelData);
272
			$listTemplateCompiler = new ListTemplateCompiler($scaffolderConfig, $modelData);
273
			//$listDetailCompiler = new ListDetailCompiler($scaffolderConfig, $modelData);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
274
			//$listChooseColumnsCompiler = new ListChooseColumnsCompiler($scaffolderConfig, $modelData);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
275
			
276
			// Compile stubs
277
			array_push($resourceCompilerOutput, $resourceCompiler->compile());
278
			array_push($moduleCompilerOutput, $moduleCompiler->compile());
279
			array_push($translateCompilerOutput, $translateCompiler->compile());
280
			// register
281
			array_push($registerModuleCompilerOutput, $registerModuleCompiler->compile());
282
			array_push($registerControllerCompilerOutput, $registerControllerCompiler->compile());
283
			array_push($registerTemplateCompilerOutput, $registerTemplateCompiler->compile());
284
			// list
285
			array_push($listModuleCompilerOutput, $listModuleCompiler->compile());
286
			array_push($listControllerCompilerOutput, $listControllerCompiler->compile());
287
			array_push($listTemplateCompilerOutput, $listTemplateCompiler->compile());
288
			//array_push($listDetailCompilerOutput, $listDetailCompiler->compile());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
289
			//array_push($listChooseColumnsCompilerOutput, $listChooseColumnsCompiler->compile());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
290
291
			$compiledIndexes .= $indexModuleCompiler->replaceResource($modelData);
292
			if ($intKey < count($modelsData))
293
				$compiledIndexes .= ",";
294
295
			$intKey++;
296
			// Advance progress
297
			$this->output->progressAdvance();
298
		}
299
300
		// Store compiled indexes
301
		$indexModuleCompiler->compileGroup($compiledIndexes);
302
		
303
		// store compiled api
304
		$fileApi = $indexApiCompiler->compile();
305
306
		// Advance progress
307
		$this->output->progressAdvance();
308
	
309
		// Finish progress
310
		$this->output->progressFinish();
311
312
		// Summary
313
		$this->comment('- Files created');
314
315
		$this->comment('- - Index Api');
316
317
		$this->info('- - - ' . $fileApi);
318
319
		$this->comment('- - Resources');
320
		foreach ($resourceCompilerOutput as $file)
321
		{
322
			$this->info('- - - ' . $file);
323
		}
324
325
		$this->comment('- - Modules');
326
		foreach ($moduleCompilerOutput as $file)
327
		{
328
			$this->info('- - - ' . $file);
329
		}
330
331
		$this->comment('- - Translate');
332
		foreach ($translateCompilerOutput as $file)
333
		{
334
			$this->info('- - - ' . $file);
335
		}
336
337
		$this->comment('- - Register');
338
339
		$this->comment('- - - Modules');
340
		foreach ($registerModuleCompilerOutput as $file)
341
		{
342
			$this->info('- - - - ' . $file);
343
		}
344
345
		$this->comment('- - - Controllers');
346
		foreach ($registerControllerCompilerOutput as $file)
347
		{
348
			$this->info('- - - - ' . $file);
349
		}
350
351
		$this->comment('- - - Templates');
352
		foreach ($registerTemplateCompilerOutput as $file)
353
		{
354
			$this->info('- - - - ' . $file);
355
		}
356
357
		$this->comment('- - List');
358
359
		$this->comment('- - - Modules');
360
		foreach ($listModuleCompilerOutput as $file)
361
		{
362
			$this->info('- - - - ' . $file);
363
		}
364
365
366
		$this->comment('- - - Controllers');
367
		foreach ($listControllerCompilerOutput as $file)
368
		{
369
			$this->info('- - - - ' . $file);
370
		}
371
372
		$this->comment('- - - Templates');
373
		foreach ($listTemplateCompilerOutput as $file)
374
		{
375
			$this->info('- - - - ' . $file);
376
		}
377
		/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
378
		$this->comment('- - - Detail Dialog');
379
		foreach ($listDetailCompilerOutput as $file)
380
		{
381
			$this->info('- - - - ' . $file);
382
		}
383
384
		$this->comment('- - - Choose Columns Dialog');
385
		foreach ($listChooseColumnsCompilerOutput as $file)
386
		{
387
			$this->info('- - - - ' . $file);
388
		}
389
		*/
390
		
391
	}
392
393
	/**
394
	 * Generation command for Blade. 
395
	 * Generates Blade templates
396
	 * @return void
397
	 */
398
	private function handleBlade(){
399
		// Get all the models
400
		$modelsData = $this->getAllModelsData();
401
402
		// Start progress bar
403
		$this->output->progressStart(count($modelsData));
404
405
		// Get app config
406
		$scaffolderConfig = $this->getScaffolderConfig();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $scaffolderConfig is correct as $this->getScaffolderConfig() (which targets Scaffolder\Commands\Gene...::getScaffolderConfig()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
407
408
		// Compilers
409
		$indexViewCompiler = new IndexViewCompiler();
0 ignored issues
show
Bug introduced by
The call to IndexViewCompiler::__construct() misses a required argument $scaffolderConfig.

This check looks for function calls that miss required arguments.

Loading history...
410
		$createViewCompiler = new CreateViewCompiler();
0 ignored issues
show
Bug introduced by
The call to CreateViewCompiler::__construct() misses a required argument $scaffolderConfig.

This check looks for function calls that miss required arguments.

Loading history...
411
		$editViewCompiler = new EditViewCompiler();
0 ignored issues
show
Bug introduced by
The call to EditViewCompiler::__construct() misses a required argument $scaffolderConfig.

This check looks for function calls that miss required arguments.

Loading history...
412
		$pageLayoutViewCompiler = new PageLayoutCompiler();
0 ignored issues
show
Bug introduced by
The call to PageLayoutCompiler::__construct() misses a required argument $scaffolderConfig.

This check looks for function calls that miss required arguments.

Loading history...
413
414
		// Compiler output
415
		$viewCompilerOutput = [];
416
		
417
		// Sidenav links
418
		$sidenavLinks = [];
419
420
		// Compiled routes
421
		$compiledRoutes = '';
0 ignored issues
show
Unused Code introduced by
$compiledRoutes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
422
423
		// Get stubs
424
		$indexViewStub = File::get($this->themeViews->getIndexPath());
0 ignored issues
show
Bug introduced by
The property themeViews does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
425
		$createViewStub = File::get($this->themeViews->getCreatePath());
426
		$editViewStub = File::get($this->themeViews->getEditPath());
427
	
428
		// views
429
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->views), 0755, true);
430
		// layouts
431
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->layouts), 0755, true);
432
		// assets
433
		Directory::createIfNotExists(PathParser::parse($scaffolderConfig->generator->paths->assets), 0755, true);
434
	
435
		
436
		// Iterate over model files
437
		foreach ($modelsData as $modelData)
438
		{
439
			// Get model name
440
			$modelName = $modelData->modelName;
441
442
			// Create views directory
443
			Directory::createIfNotExists(base_path('resources/views/' . strtolower($modelName)));
444
445
			//set hash
446
			$modelHash = $modelData->modelHash;
447
448
449
			// Compile stubs
450
			array_push($viewCompilerOutput, $indexViewCompiler->compile($indexViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash));
451
			array_push($viewCompilerOutput, $createViewCompiler->compile($createViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash));
452
			array_push($viewCompilerOutput, $editViewCompiler->compile($editViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash));
453
			// Add entity link
454
			array_push($sidenavLinks, $modelName);
455
456
			// Advance progress
457
			$this->output->progressAdvance();
458
		}
459
460
		// Create layouts directory
461
		Directory::createIfNotExists(base_path('resources/views/layouts'), 0755, true);
462
463
		// Store compiled page layout
464
		array_push($viewCompilerOutput, $pageLayoutViewCompiler->compile(File::get($this->themeLayouts->getPagePath()), null, null, $scaffolderConfig, null, ['links' => $sidenavLinks]));
0 ignored issues
show
Bug introduced by
The property themeLayouts does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
465
466
		// Store create layout
467
		$createLayout = PathParser::parse($scaffolderConfig->generator->paths->views). 'layouts/create.blade.php' ;
468
		File::copy($this->themeLayouts->getCreatePath(), $createLayout);
469
		array_push($viewCompilerOutput, $createLayout);
470
471
		// Store edit layout
472
		$editLayout = PathParser::parse($scaffolderConfig->generator->paths->views). 'layouts/edit.blade.php' ;
473
		File::copy($this->themeLayouts->getCreatePath(), $editLayout);
474
		array_push($viewCompilerOutput, $editLayout);
475
476
		// Store dashboard
477
		$dashboardLayout = PathParser::parse($scaffolderConfig->generator->paths->views). 'layouts/dashboard.blade.php' ;
478
		File::copy($this->themeLayouts->getCreatePath(), $dashboardLayout);
479
		array_push($viewCompilerOutput, $dashboardLayout);
480
481
		// Finish progress
482
		$this->output->progressFinish();
483
484
		// Summary
485
		$this->comment('- Files created');
486
487
		$this->comment('- - Views');
488
		foreach ($viewCompilerOutput as $viewFile)
489
		{
490
			$this->info('- - - ' . $viewFile);
491
		}
492
493
	}
494
495
	/**
496
	 * Generation command for laravel. 
497
	 * Generates API Code 
498
	 * Generates Blade templates
499
	 * @return void
500
	 */
501
	private function handleLaravel(){
502
		// generate API
503
		$this->handleApi();
504
505
		// generate BLADE
506
		$this->handleApi();
507
508
	}
509
510
	/**
511
	 * Get the app.json configuration and parse to an object
512
	 *
513
	 * @return void
514
	 */
515
	private function getScaffolderConfig(){
516
		// Get app config
517
		$scaffolderConfig = Json::decodeFile(base_path('scaffolder-config/app.json'));
518
519
		$scaffolderConfig->generator = $scaffolderConfig->generators->{$this->argument('app')};
520
521
		return $scaffolderConfig ;
522
	}
523
524
	/**
525
	 * Get all model files and ordenates by
526
	 *
527
	 * @return object
528
	 */
529
	private function getAllModelsData(){
530
531
		$modelFiles = File::allFiles(base_path('scaffolder-config/models/'));
532
533
		$modelsData = [];
534
		$orderedModelsData = [];
0 ignored issues
show
Unused Code introduced by
$orderedModelsData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
535
536
		// set indexes
537
		//$firstKey = 
538
		foreach ($modelFiles as $modelFile)
539
		{
540
541
			// Get model data
542
			$modelData = Json::decodeFile($modelFile->getRealPath());
543
544
545
			// Get model name
546
			$modelName = CamelCase::convertToCamelCase(($modelFile->getBasename('.' . $modelFile->getExtension())));
547
548
			// Get model hash
549
			$modelHash = md5_file($modelFile->getRealPath());
550
551
			// Set model name
552
			$modelData->modelName = $modelName ;
553
554
			// Set model name
555
			$modelData->modelHash = $modelHash ;
556
557
			// get primary field or create 
558
			$primaryField = $this->getPrimaryKeyField($modelData);
559
560
			// put primary key at first position
561
			if(isset($primaryField->declared) && !$primaryField->declared){
562
				$modelData->fields = array_pad($modelData->fields, -(count($modelData->fields)+1), $primaryField);
563
			}
564
565
			// set timestamps
566
			if($modelData->timeStamps){
567
				$createdAtField = new stdClass;
568
				$createdAtField->name = "created_at" ;
569
				$createdAtField->index = "none" ;
570
				$createdAtField->declared =  false ;
571
				$createdAtField->type = new stdClass ;
572
				$createdAtField->type->ui = 'timestamp' ;
573
				$createdAtField->type->db = 'datetime' ;
574
				$createdAtField->foreignKey = [];
575
				$createdAtField->validations = "required" ;
576
				$updatedAtField = clone($createdAtField);
577
				$updatedAtField->name = "updated_at" ;
578
579
				array_push($modelData->fields, $createdAtField);
580
				array_push($modelData->fields, $updatedAtField);
581
			}
582
583
			$modelsData[$modelData->tableName] = $modelData ;
584
585
			// put all migrations pre-requisites in top of generation hieraquical
586
			if(count($modelData->migrationPreRequisites) == 0){
587
				$modelsData = Arrays::moveElement($modelsData, $modelData->tableName, 0);
588
			}
589
590
		}
591
592
		// let put all pre-requisites in order
593
		$actualTablePosition = 0 ;
594
		foreach ($modelsData as $key => $modelData)
595
		{
596
			// set migration order
597
			$modelData->migrationOrder = isset($modelData->migrationOrder) ? $modelData->migrationOrder : $actualTablePosition ;
598
599
			$positions = array_keys($modelsData);
600
601
			foreach($modelData->migrationPreRequisites as $preRequiste){
602
				$preRequisitePosition = array_search($preRequiste, $positions);
603
				// change positions
604
				if( $preRequisitePosition >  $actualTablePosition){
605
					$modelData->migrationOrder = $preRequisitePosition ;
606
					$modelsData[$preRequiste]->migrationOrder = $actualTablePosition ;
607
					$modelsData = Arrays::moveElement($modelsData, $preRequiste, $actualTablePosition);
608
					
609
				}
610
			}
611
612
613
			// search for other fields relationships
614
			foreach($modelData->fields as $field){
615
				
616
617
				if(isset($field->foreignKey->relationship)){
618
					switch ($field->foreignKey->relationship) {
619
						case 'belongsTo':
620
							// hasOne or hasMay are the inverse relationship for belongsTo
621
							$relationship = new stdClass ;
622
							$relationship->type  = $field->foreignKey->reverse ;
623
							$relationship->foreignKey = $field->name ;
624
							$relationship->localKey = $field->foreignKey->field ;
625
							$relationship->modelName = $modelData->modelName ;
626
							array_push($modelsData[$field->foreignKey->table]->reverseRelationships, $relationship);
627
							break;
628
						case 'belongsToMany':
629
							// belongsToMany is the inverse relationship for belongsTo
630
							$relationship = new stdClass ;
631
							$relationship->type  = $field->foreignKey->reverse ;
632
							$relationship->foreignKey = $field->name ;
633
							$relationship->relatedTable = isset($field->relatedTable) ? $field->relatedTable : '';
634
							$relationship->relatedField = isset($field->relatedField) ? $field->relatedField : '';
635
							$relationship->ui = $field->type->ui;
636
							$relationship->localKey = $field->foreignKey->field ;
637
							$relationship->modelName = $modelData->modelName ;
638
							$relationship->tableName = $modelData->tableName ;
639
							array_push($modelsData[$field->foreignKey->table]->reverseRelationships, $relationship);
640
							break;
641
						default:
642
							# code...
643
							break;
644
					}
645
				
646
				}
647
				
648
			}
649
			
650
651
			$actualTablePosition++ ;
652
		}
653
	
654
		// $this->info(print_r($modelsData, 1));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
655
	
656
		return $modelsData ;
657
	
658
	}
659
660 View Code Duplication
	protected function getPrimaryKeyField($modelData){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
661
		$primaryKey = new stdClass;
662
		$primaryKey->name = "id" ;
663
		$primaryKey->index = "primary" ;
664
		$primaryKey->declared =  false ;
665
		$primaryKey->type = new stdClass ;
666
		$primaryKey->type->ui = 'label' ;
667
		$primaryKey->type->db = 'integer' ;
668
		$primaryKey->foreignKey = [];
669
		$primaryKey->validations = "required" ;
670
671
		foreach ($modelData->fields as $field)
672
		{
673
			if ($field->index == 'primary')
674
			{
675
				$primaryKey = $field ;
676
				break;
677
			}
678
		}
679
680
		return $primaryKey ;
681
	}
682
683
}