]> _ Git - cubist_cms-back.git/commitdiff
#2783
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 24 May 2019 09:11:28 +0000 (11:11 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 24 May 2019 09:11:28 +0000 (11:11 +0200)
src/app/Console/Commands/MagicCubistCommand.php
src/app/Magic/Models/CubistMagicModelAbstract.php
src/app/Magic/Util.php [new file with mode: 0644]
src/resources/cubistmagic/Controller.stub
src/resources/cubistmagic/routes.stub [new file with mode: 0644]

index a17b7330d97c9f4a2dbbb25344b8cbe98fe12604..8aa0c757e3bd5f98cddc2ced0510ab7faa83512e 100644 (file)
@@ -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
 
             }
         }
+
     }
 
     /**
index 81d6d16499c858b9ec28591cdf41989d5272e847..5dd1cde170389ae57b7434adcf93046baf056195 100644 (file)
@@ -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 (file)
index 0000000..7e6e8a9
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+
+namespace Cubist\Backpack\app\Magic;
+
+
+class Util
+{
+    public static function getStubPath()
+    {
+        return __DIR__ . '/../../../resources/cubistmagic/';
+    }
+}
index b6d5dcfed24da035e1a39794782f755b5f6d7277..806f19f7586a3e51371e74dba2bc1858b714d4e8 100644 (file)
@@ -3,7 +3,7 @@
 namespace App\Htpp\Controllers\Admin;
 use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
 
-class _STUDLYNAME_Controller extends CubistMagicController
+class _CONTROLLERCLASS_ extends CubistMagicController
 {
     protected $_modelNamespace = '_MODELNAMESPACE_';
     protected $_routeURL = '_ROUTEURL_';
diff --git a/src/resources/cubistmagic/routes.stub b/src/resources/cubistmagic/routes.stub
new file mode 100644 (file)
index 0000000..f9bac56
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+Route::group([
+    'prefix'     => 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