From d0266fb507abf61acbfed9d088193d22c302559d Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 24 May 2019 18:37:28 +0200 Subject: [PATCH] #2783 --- src/CubistBackpackServiceProvider.php | 5 +- ...gicCubistCommand.php => CubistCommand.php} | 102 +++++------------- src/app/Console/Commands/GenerateCommand.php | 67 ++++++++++++ src/app/Console/Commands/MigrateCommand.php | 42 ++++++++ .../Magic/Models/CubistMagicModelAbstract.php | 2 - 5 files changed, 138 insertions(+), 80 deletions(-) rename src/app/Console/Commands/{MagicCubistCommand.php => CubistCommand.php} (54%) create mode 100644 src/app/Console/Commands/GenerateCommand.php create mode 100644 src/app/Console/Commands/MigrateCommand.php diff --git a/src/CubistBackpackServiceProvider.php b/src/CubistBackpackServiceProvider.php index 4f24759..2d6f547 100644 --- a/src/CubistBackpackServiceProvider.php +++ b/src/CubistBackpackServiceProvider.php @@ -2,7 +2,8 @@ namespace Cubist\Backpack; -use Cubist\Backpack\app\Console\Commands\MagicCubistCommand; +use app\Console\Command\MigrateCommand; +use Cubist\Backpack\app\Console\Commands\GenerateCommand; use Illuminate\Support\ServiceProvider; use Route; @@ -47,6 +48,6 @@ class CubistBackpackServiceProvider extends ServiceProvider // $loader = \Illuminate\Foundation\AliasLoader::getInstance(); // $loader->alias('Template', \Backpack\Settings\app\Models\Template::class); - $this->commands([MagicCubistCommand::class]); + $this->commands([GenerateCommand::class, MigrateCommand::class]); } } diff --git a/src/app/Console/Commands/MagicCubistCommand.php b/src/app/Console/Commands/CubistCommand.php similarity index 54% rename from src/app/Console/Commands/MagicCubistCommand.php rename to src/app/Console/Commands/CubistCommand.php index 344c012..fe525ba 100644 --- a/src/app/Console/Commands/MagicCubistCommand.php +++ b/src/app/Console/Commands/CubistCommand.php @@ -1,90 +1,18 @@ _handleMagicFolder($magic); - } else { - $this->line('No magic folder at ' . $magic); - } - $this->_writeRoutes(); - - //Artisan::call('cache:clear'); - //Artisan::call('optimize'); - $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); - $iterator = Files::getDirectoryIterator($folder, true); - foreach ($iterator as $item) { - $this->line('Testing ' . $item); - /** @var $item \SplFileInfo */ - if ($item->isFile() && $item->getExtension() == 'php') { - try { - $class = PHP::instanciateClassInFile($item); - 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) { - - } - - } - } - - } /** * Run a SSH command. @@ -131,4 +59,26 @@ class MagicCubistCommand extends Command $this->{$type}($content); } } + + protected function _handleMagicFolder($callback) + { + $magic = app_path() . '/Models'; + + $iterator = Files::getDirectoryIterator($magic, true); + foreach ($iterator as $item) { + $this->line('Testing ' . $item); + /** @var $item \SplFileInfo */ + if ($item->isFile() && $item->getExtension() == 'php') { + try { + $class = PHP::instanciateClassInFile($item); + if ($class instanceof CubistMagicModelAbstract) { + if (is_callable($callback)) { + call_user_func($callback, $class); + } + } + } catch (\Exception $e) { + } + } + } + } } diff --git a/src/app/Console/Commands/GenerateCommand.php b/src/app/Console/Commands/GenerateCommand.php new file mode 100644 index 0000000..54ba587 --- /dev/null +++ b/src/app/Console/Commands/GenerateCommand.php @@ -0,0 +1,67 @@ +_handleMagicFolder([$this, '_generate']); + $this->_writeRoutes(); + + //Artisan::call('cache:clear'); + //Artisan::call('optimize'); + $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); + } + + /** + * @param $model CubistMagicModelAbstract + */ + public function _generate($model) + { + $this->line('Generate code of ' . get_class($model)); + $model->generateCode(); + $this->_routes[] = 'CRUD::resource(\'' . $model->getOption('name') . '\', \'' . $model->getControllerClass() . '\');'; + } + +} + + diff --git a/src/app/Console/Commands/MigrateCommand.php b/src/app/Console/Commands/MigrateCommand.php new file mode 100644 index 0000000..19b9ae8 --- /dev/null +++ b/src/app/Console/Commands/MigrateCommand.php @@ -0,0 +1,42 @@ +_handleMagicFolder([$this, '_migrate']); + } + + /** + * @param $model CubistMagicModelAbstract + */ + public function _migrate($model) + { + + } +} diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php index 8a68f4c..a472d52 100644 --- a/src/app/Magic/Models/CubistMagicModelAbstract.php +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -14,8 +14,6 @@ use Illuminate\Support\Str; class CubistMagicModelAbstract extends Model { - - use CubistMagicAttribute; use CrudTrait; use Sluggable, SluggableScopeHelpers; -- 2.39.5