From 39566b1da13d6ffcbb37629e8b9b502651f6706e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 27 Feb 2020 19:44:34 +0100 Subject: [PATCH] wip #3439 @0.5 --- src/app/Console/Commands/GenerateCommand.php | 8 ++++++ .../Http/Controllers/CubistCrudController.php | 27 ++++++++++++------- .../Controllers/CubistMagicController.php | 27 ++++++++++++------- src/resources/cubistmagic/routes.stub | 2 +- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/app/Console/Commands/GenerateCommand.php b/src/app/Console/Commands/GenerateCommand.php index 3c712f9..4191c85 100644 --- a/src/app/Console/Commands/GenerateCommand.php +++ b/src/app/Console/Commands/GenerateCommand.php @@ -21,6 +21,11 @@ class GenerateCommand extends CubistCommand */ protected $description = 'Generate Controllers, Requests, Routes, migrations according to magic'; + /** + * @var array + */ + protected $_routes = []; + /** * Execute the console command. @@ -44,6 +49,7 @@ class GenerateCommand extends CubistCommand 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); } @@ -58,6 +64,8 @@ class GenerateCommand extends CubistCommand $route = $model->getOption('route', $model->getOption('name')); $controller = $model->getControllerClass(); + + $this->_routes[] = ' Route::crud(\'' . $route . '\', \'' . $controller . '\');'; } } diff --git a/src/app/Http/Controllers/CubistCrudController.php b/src/app/Http/Controllers/CubistCrudController.php index 09b6b32..2cd3fd3 100644 --- a/src/app/Http/Controllers/CubistCrudController.php +++ b/src/app/Http/Controllers/CubistCrudController.php @@ -11,18 +11,25 @@ class CubistCrudController extends CrudController { public function __construct() { - if (!$this->crud) { + if ($this->crud) { + return; + } + + // call the setup function inside this closure to also have the request there + // this way, developers can use things stored in session (auth variables, etc) + $this->middleware(function ($request, $next) { + // make a new CrudPanel object, from the one stored in Laravel's service container $this->crud = app()->make(CubistCrudPanel::class); + // ensure crud has the latest request + $this->crud->setRequest($request); + $this->request = $request; + $this->setupDefaults(); + $this->setup(); + $this->setupConfigurationForCurrentOperation(); - // call the setup function inside this closure to also have the request there - // this way, developers can use things stored in session (auth variables, etc) - $this->middleware(function ($request, $next) { - $this->request = $request; - $this->crud->request = $request; - $this->setup(); + return $next($request); + }); - return $next($request); - }); - } + parent::__construct(); } } diff --git a/src/app/Magic/Controllers/CubistMagicController.php b/src/app/Magic/Controllers/CubistMagicController.php index 6852bf3..8419ef1 100644 --- a/src/app/Magic/Controllers/CubistMagicController.php +++ b/src/app/Magic/Controllers/CubistMagicController.php @@ -30,7 +30,6 @@ use Illuminate\Support\Facades\Route; class CubistMagicController extends CubistCrudController { - use CrudTrait; use HandleAjaxMedia; use ListOperation; @@ -50,11 +49,6 @@ class CubistMagicController extends CubistCrudController protected $_oneInstance; protected $_nested = false; - public function __construct() - { - parent::__construct(); - } - // Routes //Route::post($this->name . '/bulk-publish', [ ////'as' => 'crud.' . $this->name . '.bulkPublish', @@ -150,7 +144,7 @@ class CubistMagicController extends CubistCrudController } - $this->crud->setRoute(config('backpack.base.route_prefix') . '/' . $this->_routeURL); + $this->crud->setRoute(backpack_url($this->_routeURL)); $this->crud->setEntityNameStrings($this->_singular, $this->_plural); @@ -164,9 +158,6 @@ class CubistMagicController extends CubistCrudController $this->crud->addColumn(['name' => $model->getPrimaryKey(), 'type' => 'number', 'label' => "#", 'searchLogic' => 'text']); $this->updateFieldsFromModel($model); - - $this->setupRoutes($this->_routeURL, $this->_routeURL, $this->_routeURL); - } public function updateFieldsFromModel($model = null) @@ -368,4 +359,20 @@ class CubistMagicController extends CubistCrudController $this->_forgetCache(); parent::saveReorder(); } + + public function setupListOperation() + { + echo ":)"; + } + + public function setupCreateOperation() + { + echo ":)"; + } + + public function setupUpdateOperation() + { + echo ":)"; + } + } diff --git a/src/resources/cubistmagic/routes.stub b/src/resources/cubistmagic/routes.stub index c2f7f55..b6008f6 100644 --- a/src/resources/cubistmagic/routes.stub +++ b/src/resources/cubistmagic/routes.stub @@ -4,5 +4,5 @@ Route::group([ 'middleware' => ['web', config('backpack.base.middleware_key', 'admin')], 'namespace' => 'App\Http\Controllers\Admin', ], function () { // custom admin routes - + _CUSTOM_ }); -- 2.39.5