1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
5
|
|
|
|
6
|
|
|
Route::get('scaffolder/generator', function () |
7
|
|
|
{ |
8
|
|
|
return view('scaffolder::generator'); |
9
|
|
|
}); |
10
|
|
|
|
11
|
|
|
Route::post('scaffolder/add-model', function (Request $request) |
12
|
|
|
{ |
13
|
|
|
return view('scaffolder::partials.model', ['modelId' => $request->input('modelId')]); |
14
|
|
|
}); |
15
|
|
|
|
16
|
|
|
Route::post('scaffolder/add-field', function (Request $request) |
17
|
|
|
{ |
18
|
|
|
return view('scaffolder::partials.field', [ |
19
|
|
|
'modelId' => $request->input('modelId'), |
20
|
|
|
'fieldId' => $request->input('fieldId') |
21
|
|
|
]); |
22
|
|
|
}); |
23
|
|
|
|
24
|
|
|
Route::post('scaffolder/generate', function (Request $request) |
25
|
|
|
{ |
26
|
|
|
$models = $request->only('models'); |
27
|
|
|
|
28
|
|
View Code Duplication |
array_walk_recursive($models, function (&$item) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
if ($item == 'true') $item = true; |
31
|
|
|
elseif ($item == 'false') $item = false; |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
// Generate app.json |
35
|
|
|
File::put(base_path('scaffolder-config/app.json'), |
36
|
|
|
json_encode($request->except('models'), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
37
|
|
|
|
38
|
|
|
// Generate json for models |
39
|
|
View Code Duplication |
foreach ($models['models'] as $model) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$modelName = $model['modelName']; |
42
|
|
|
array_shift($model); |
43
|
|
|
File::put(base_path('scaffolder-config/models/' . $modelName . '.json'), |
44
|
|
|
json_encode($model, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
45
|
|
|
} |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
Route::post('scaffolder/generate-and-execute', function (Request $request) |
49
|
|
|
{ |
50
|
|
|
$models = $request->only('models'); |
51
|
|
|
|
52
|
|
View Code Duplication |
array_walk_recursive($models, function (&$item) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
if ($item == 'true') $item = true; |
55
|
|
|
elseif ($item == 'false') $item = false; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
// Generate app.json |
59
|
|
|
File::put(base_path('scaffolder-config/app.json'), |
60
|
|
|
json_encode($request->except('models'), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
61
|
|
|
|
62
|
|
|
// Generate json for models |
63
|
|
View Code Duplication |
foreach ($models['models'] as $model) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$modelName = $model['modelName']; |
66
|
|
|
array_shift($model); |
67
|
|
|
File::put(base_path('scaffolder-config/models/' . $modelName . '.json'), |
68
|
|
|
json_encode($model, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Execute artisan command |
72
|
|
|
$exitCode = Artisan::call('mpaleo.scaffolder:generate'); |
73
|
|
|
|
74
|
|
|
return response()->json(['exitCode' => $exitCode]); |
75
|
|
|
}); |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: