From: Vincent Vanwaelscappel Date: Fri, 24 May 2019 16:37:28 +0000 (+0200) Subject: #2783 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=d0266fb507abf61acbfed9d088193d22c302559d;p=cubist_cms-back.git #2783 --- 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/CubistCommand.php b/src/app/Console/Commands/CubistCommand.php new file mode 100644 index 0000000..fe525ba --- /dev/null +++ b/src/app/Console/Commands/CubistCommand.php @@ -0,0 +1,84 @@ +echo('info', $beforeNotice ? ' ' . $beforeNotice : $command); + + $process = new Process($command, null, null, null, 50, null); + $process->run(function ($type, $buffer) { + if (Process::ERR === $type) { + $this->echo('comment', $buffer); + } else { + $this->echo('line', $buffer); + } + }); + + // executes after the command finishes + if (!$process->isSuccessful()) { + throw new ProcessFailedException($process); + } + + if ($afterNotice) { + $this->echo('info', $afterNotice); + } + } + + /** + * Write text to the screen for the user to see. + * + * @param [string] $type line, info, comment, question, error + * @param [string] $content + */ + public function echo($type, $content) + { + // skip empty lines + if (trim($content)) { + $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/MagicCubistCommand.php b/src/app/Console/Commands/MagicCubistCommand.php deleted file mode 100644 index 344c012..0000000 --- a/src/app/Console/Commands/MagicCubistCommand.php +++ /dev/null @@ -1,134 +0,0 @@ -_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. - * - * @param string $command The SSH command that needs to be run - * @param bool $beforeNotice Information for the user before the command is run - * @param bool $afterNotice Information for the user after the command is run - * - * @return mixed Command-line output - */ - public function executeProcess($command, $beforeNotice = false, $afterNotice = false) - { - $this->echo('info', $beforeNotice ? ' ' . $beforeNotice : $command); - - $process = new Process($command, null, null, null, 50, null); - $process->run(function ($type, $buffer) { - if (Process::ERR === $type) { - $this->echo('comment', $buffer); - } else { - $this->echo('line', $buffer); - } - }); - - // executes after the command finishes - if (!$process->isSuccessful()) { - throw new ProcessFailedException($process); - } - - if ($afterNotice) { - $this->echo('info', $afterNotice); - } - } - - /** - * Write text to the screen for the user to see. - * - * @param [string] $type line, info, comment, question, error - * @param [string] $content - */ - public function echo($type, $content) - { - // skip empty lines - if (trim($content)) { - $this->{$type}($content); - } - } -} 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;