From f43d69367ed774feea5e00367f69cb80a2b15f20 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 24 May 2019 11:11:28 +0200 Subject: [PATCH] #2783 --- .../Console/Commands/MagicCubistCommand.php | 18 +++++++++++++ .../Magic/Models/CubistMagicModelAbstract.php | 27 ++++++------------- src/app/Magic/Util.php | 12 +++++++++ src/resources/cubistmagic/Controller.stub | 2 +- src/resources/cubistmagic/routes.stub | 8 ++++++ 5 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 src/app/Magic/Util.php create mode 100644 src/resources/cubistmagic/routes.stub diff --git a/src/app/Console/Commands/MagicCubistCommand.php b/src/app/Console/Commands/MagicCubistCommand.php index a17b733..8aa0c75 100644 --- a/src/app/Console/Commands/MagicCubistCommand.php +++ b/src/app/Console/Commands/MagicCubistCommand.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Console\Commands; use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; +use Cubist\Backpack\app\Magic\Util; use Cubist\Util\Files\Files; use Cubist\Util\PHP; use Illuminate\Console\Command; @@ -26,6 +27,8 @@ class MagicCubistCommand extends Command */ protected $description = 'Generate Controllers, Requests, Routes, migrations according to magic'; + protected $_routes = []; + /** * Execute the console command. * @@ -41,10 +44,23 @@ class MagicCubistCommand extends Command } else { $this->line('No magic folder at ' . $magic); } + $this->_writeRoutes(); $this->executeProcess('composer dump-autoload'); } + protected function _writeRoutes() + { + $routesDir = base_path() . '/routes/backpack'; + if (!file_exists($routesDir)) { + mkdir($routesDir, 0777, true); + } + $stub = file_get_contents(Util::getStubPath() . '/routes.stub'); + $stub = str_replace('_CUSTOM_', implode("\n", $this->_routes), $stub); + + file_put_contents($routesDir . '/custom.php', $stub); + } + protected function _handleMagicFolder($folder) { $this->line('Handling folder ' . $folder); @@ -58,6 +74,7 @@ class MagicCubistCommand extends Command if ($class instanceof CubistMagicModelAbstract) { $this->line('Generate code of ' . get_class($class)); $class->generateCode(); + $this->_routes[] = 'CRUD::resource(\'' . $class->getOption('name') . '\', \'' . $class->getControllerClass() . '\');'; } } catch (\Exception $e) { @@ -65,6 +82,7 @@ class MagicCubistCommand extends Command } } + } /** diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php index 81d6d16..5dd1cde 100644 --- a/src/app/Magic/Models/CubistMagicModelAbstract.php +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Magic\Models; use Cubist\Backpack\app\Magic\Fields\CubistMagicField; +use Cubist\Backpack\app\Magic\Util; use Illuminate\Database\Eloquent\Model; use Cubist\Backpack\app\Magic\CubistMagicAttribute; use Illuminate\Support\Facades\Route; @@ -53,18 +54,6 @@ class CubistMagicModelAbstract extends Model $this->_fields[$field->getAttribute('name')] = $field; } - public function loadRoute() - { - Route::group([ - 'prefix' => config('backpack.base.route_prefix', 'admin'), - 'middleware' => ['web', config('backpack.base.middleware_key', 'admin')], - 'namespace' => 'Cubist\Backpack\app\Http\Controllers', - ], function () { // custom admin routes - CRUD::resource('model', 'CubistModelCrudController'); - CRUD::resource('model', 'CubistModelCrudController'); - }); // this should be the absolute last line of this file - } - public function generateCode() { $this->_generateControllerCode(); @@ -72,14 +61,18 @@ class CubistMagicModelAbstract extends Model protected function _generateControllerCode() { - $this->_replaceInCode($this->_getStubPath() . 'Controller.stub', - app_path() . '/Http/Controllers/Admin/' . $this->getStudlyName() . 'Controller.php'); + $this->_replaceInCode(Util::getStubPath() . 'Controller.stub', + app_path() . '/Http/Controllers/Admin/' . $this->getControllerClass() . '.php'); } + public function getControllerClass() + { + return $this->getStudlyName() . 'CrudController'; + } protected function _replaceInCode($stub, $dest) { - $vars = ['STUDLYNAME' => $this->getStudlyName(), + $vars = ['CONTROLLERCLASS' => $this->getControllerClass(), 'ROUTEURL' => $this->getOption('name'), 'SINGULAR' => $this->getOption('singular', $this->getOption('name')), 'PLURAL' => $this->getOption('plural', ''), @@ -108,10 +101,6 @@ class CubistMagicModelAbstract extends Model return $res; } - protected function _getStubPath() - { - return __DIR__ . '/../../../resources/cubistmagic/'; - } public function getStudlyName() { diff --git a/src/app/Magic/Util.php b/src/app/Magic/Util.php new file mode 100644 index 0000000..7e6e8a9 --- /dev/null +++ b/src/app/Magic/Util.php @@ -0,0 +1,12 @@ + config('backpack.base.route_prefix', 'admin'), + 'middleware' => ['web', config('backpack.base.middleware_key', 'admin')], + 'namespace' => 'App\Http\Controllers\Admin', +], function () { // custom admin routes + _CUSTOM_ +}); // this should be the absolute last line of this file -- 2.39.5