Last active
January 30, 2018 19:40
-
-
Save kikoseijo/b3ceef0d25801248bacc7615b62bf3ea to your computer and use it in GitHub Desktop.
Laravel CRUD Generator + Bootstrap4 upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\File; | |
use Illuminate\Support\Facades\Artisan; | |
class CrudGenerator extends Controller | |
{ | |
public function generate() | |
{ | |
$models = [ | |
'operations' => [ | |
'fields' => 'tipo#select#options=relationship; almacen#string; mercancia#string; transfer_from#string; transfer_to#string; notes#text; complete#boolean', | |
'validations' => 'tipo#required; almacen#required; mercancia#required; transfer_from#required; transfer_to#required', | |
], | |
'types' => [ | |
'fields' => 'client_id#select#options=relationship; name#string; facturable#boolean', | |
'validations' => 'client_id#required|numeric|min:1; name#required|min:2', | |
], | |
'vacios' => [ | |
'fields' => 'tipo#radio#options={"Puerto": "Puerto", "Oficina": "Oficina", "ZAL": "ZAL"}; matricula#string; parking#string; notes#json; complete#boolean', | |
'validations' => 'tipo#required; matricula#required; parking#required', | |
], | |
// 'gps' => [ | |
// 'fields' => 'name#string; client_id#select#options=relationship; estado#select#options=relationship; notes#json', | |
// 'validations' => 'name#required; client_id#required|numeric|min:1', | |
// ], | |
'gpsjobs' => [ | |
'fields' => 'client_id#select#options=relationship; matricula#string; chofer#string; gps_id#select#options=relationship; delivery_at#date; delivered_by#select#options=relationship; return_at#date; return_by#select#options=relationship; complete#boolean; notes#json', | |
'validations' => 'chofer#required; maticula#required; gps_id#required; delivery_at#required; client_id#required', | |
], | |
'users' => [ | |
'fields' => 'name#string; email#email; password#password; admin#boolean', | |
'validations' => 'name#required|min:2; email#required|email', | |
], | |
'clients' => [ | |
'fields' => 'name#string; cif#string; address#string; phone#string; email#email; calidad_name#string; calidad_email#email; notes#json; logo#file', | |
'validations' => 'name#required', | |
] | |
]; | |
foreach ($models as $model => $data) { | |
$this->generateViewsCrud($model, $data); | |
// $this->generateControllerCrud($model); | |
} | |
} | |
protected function generateViewsCrud($model, $data){ | |
$directory = resource_path('views/altraser/'.kebab_case($model)); | |
$success = File::deleteDirectory($directory); | |
$options = [ | |
'name' => $model, | |
'--view-path' => 'altraser', | |
'--form-helper' => 'boot4', | |
'--fields' => $data['fields'], | |
'--validations' => $data['validations'], | |
// '--force' => true, | |
]; | |
$this->runCommand('crud:view', $options); | |
// $command = "sed -i -e 's/abc/XYZ/g' /tmp/file.txt" | |
$this->upgradeBootstrap($directory); | |
} | |
protected function generateControllerCrud($model){ | |
$options = [ | |
'model' => $model, | |
'--swagger' => false, | |
]; | |
$this->runCommand('ksoft:krud', $options); | |
} | |
protected function runCommand($command, $options){ | |
$exitCode = Artisan::call($command, $options); | |
$output = Artisan::output(); | |
echo $output; | |
} | |
protected function upgradeBootstrap($directory){ | |
$replacements = [ | |
// 'table table-borderless' => 'table table-inverse', | |
// 'help-block' => 'form-text', | |
// 'btn-default' => 'btn-secondary', | |
// 'btn-xs' => 'btn-sm', | |
// 'control-label' => 'form-control-label', | |
// 'form-group ' => 'form-group row ', | |
// 'panel-default' => '', | |
// 'panel-heading' => 'card-header', | |
// 'panel-body' => 'card-block', | |
// '<div class="panel">' => '<div class="card">', | |
"json_decode('relationship', true)" => "['uno', 'dos']", | |
]; | |
$files = File::allFiles($directory); | |
foreach ($files as $file) | |
{ | |
$content = app('files')->get($file); | |
$content = str_replace(array_keys($replacements), array_values($replacements), $content); | |
app('files')->put($file, $content); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment