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;
// $loader = \Illuminate\Foundation\AliasLoader::getInstance();
// $loader->alias('Template', \Backpack\Settings\app\Models\Template::class);
- $this->commands([MagicCubistCommand::class]);
+ $this->commands([GenerateCommand::class, MigrateCommand::class]);
}
}
--- /dev/null
+<?php
+
+
+namespace app\Console\Commands;
+
+
+use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract;
+use Cubist\Util\Files\Files;
+use Cubist\Util\PHP;
+use Illuminate\Console\Command;
+use Symfony\Component\Process\Exception\ProcessFailedException;
+use Symfony\Component\Process\Process;
+
+class CubistCommand extends Command
+{
+
+ /**
+ * 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);
+ }
+ }
+
+ 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) {
+ }
+ }
+ }
+ }
+}
--- /dev/null
+<?php
+
+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;
+
+class GenerateCommand extends CubistCommand
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'cubist:magic:generate';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Generate Controllers, Requests, Routes, migrations according to magic';
+
+ protected $_routes = [];
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $this->_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() . '\');';
+ }
+
+}
+
+
+++ /dev/null
-<?php
-
-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;
-use Symfony\Component\Process\Exception\ProcessFailedException;
-use Symfony\Component\Process\Process;
-
-class MagicCubistCommand extends Command
-{
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'cubist:magic:generate';
-
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Generate Controllers, Requests, Routes, migrations according to magic';
-
- protected $_routes = [];
-
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $magic = app_path() . '/Models';
- // Find cubistmagic directories
-
- if (file_exists($magic)) {
- $this->_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);
- }
- }
-}
--- /dev/null
+<?php
+
+
+namespace app\Console\Command;
+
+use app\Console\Commands\CubistCommand;
+use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract;
+
+class MigrateCommand extends CubistCommand
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'cubist:magic:migrate';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Make database migrations for Magic Models';
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $this->_handleMagicFolder([$this, '_migrate']);
+ }
+
+ /**
+ * @param $model CubistMagicModelAbstract
+ */
+ public function _migrate($model)
+ {
+
+ }
+}
class CubistMagicModelAbstract extends Model
{
-
-
use CubistMagicAttribute;
use CrudTrait;
use Sluggable, SluggableScopeHelpers;