From: Vincent Vanwaelscappel Date: Wed, 29 May 2019 13:02:35 +0000 (+0200) Subject: #2783 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=ab99d25b446789e6ef7d59dd38072b1e96726a40;p=cubist_cms-back.git #2783 --- diff --git a/src/app/Console/Commands/CubistCommand.php b/src/app/Console/Commands/CubistCommand.php index af8c97f..455b40c 100644 --- a/src/app/Console/Commands/CubistCommand.php +++ b/src/app/Console/Commands/CubistCommand.php @@ -4,7 +4,7 @@ namespace Cubist\Backpack\app\Console\Commands; -use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; use Cubist\Util\Files\Files; use Cubist\Util\PHP; use Illuminate\Console\Command; @@ -71,7 +71,7 @@ class CubistCommand extends Command if ($item->isFile() && $item->getExtension() == 'php') { try { $class = PHP::instanciateClassInFile($item); - if ($class instanceof CubistMagicModelAbstract) { + if ($class instanceof CubistMagicAbstractModel) { if (is_callable($callback)) { call_user_func($callback, $class); } diff --git a/src/app/Console/Commands/GenerateCommand.php b/src/app/Console/Commands/GenerateCommand.php index 45a9cb8..b4d1449 100644 --- a/src/app/Console/Commands/GenerateCommand.php +++ b/src/app/Console/Commands/GenerateCommand.php @@ -2,7 +2,7 @@ namespace Cubist\Backpack\app\Console\Commands; -use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; use Cubist\Backpack\app\Magic\Util; class GenerateCommand extends CubistCommand @@ -51,7 +51,7 @@ class GenerateCommand extends CubistCommand } /** - * @param $model CubistMagicModelAbstract + * @param $model CubistMagicAbstractModel */ public function _generate($model) { diff --git a/src/app/Console/Commands/MigrateCommand.php b/src/app/Console/Commands/MigrateCommand.php index ffdcb20..735cab9 100644 --- a/src/app/Console/Commands/MigrateCommand.php +++ b/src/app/Console/Commands/MigrateCommand.php @@ -4,7 +4,6 @@ namespace Cubist\Backpack\app\Console\Command; use Cubist\Backpack\app\Console\Commands\CubistCommand; -use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Schema\Comparator; use Doctrine\DBAL\Schema\Schema; diff --git a/src/app/Magic/Controllers/CubistMagicControllerTrait.php b/src/app/Magic/Controllers/CubistMagicControllerTrait.php index d44f414..fb5f8a0 100644 --- a/src/app/Magic/Controllers/CubistMagicControllerTrait.php +++ b/src/app/Magic/Controllers/CubistMagicControllerTrait.php @@ -3,7 +3,7 @@ namespace Cubist\Backpack\app\Magic\Controllers; use Cubist\Backpack\app\Magic\Fields\Field; -use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; use Cubist\Backpack\app\Magic\Requests\CubistMagicStoreRequest; use Cubist\Backpack\app\Magic\Requests\CubistMagicUpdateRequest; @@ -53,7 +53,7 @@ trait CubistMagicControllerTrait } /** - * @return CubistMagicModelAbstract + * @return CubistMagicAbstractModel */ public function getModelInstance() { diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 6466d3b..25eb624 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -7,7 +7,7 @@ use Cubist\Backpack\app\Magic\CubistMagicAttribute; use Doctrine\DBAL\Schema\Table; use Exception; use Illuminate\Support\Str; -use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; class Field { @@ -106,7 +106,7 @@ class Field public function defineDbColumn($table) { $name = $this->getAttribute('name'); - $table->addColumn($name, CubistMagicModelAbstract::toDoctrineType($this->_databaseType), $this->_databaseAttributes); + $table->addColumn($name, CubistMagicAbstractModel::toDoctrineType($this->_databaseType), $this->_databaseAttributes); if ($this->_databaseUnique) { $table->addUniqueIndex([$name]); } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php new file mode 100644 index 0000000..ee9daca --- /dev/null +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -0,0 +1,212 @@ + 'integer']; + + protected $nested = false; + protected $primaryKey = 'id'; + public $timestamps = true; + + /** + * @var Field[] + */ + protected $_fields = []; + + /** + * @var array + */ + protected $_options = []; + + /** + * @var array + */ + protected $_slugFields = ['slug', 'title', 'name']; + + public function __construct(array $attributes = []) + { + $this->setFields(); + $this->bootIfNotBooted(); + $this->initializeTraits(); + $this->syncOriginal(); + $this->fill($attributes); + } + + public function setFields() + { + + } + + public function getFields() + { + return $this->_fields; + } + + /** + * Return the sluggable configuration array for this model. + * + * @return array + */ + public function sluggable() + { + return [ + 'slug' => [ + 'source' => 'slug_or_title', + ], + ]; + } + + // The slug is created automatically from the "title" field if no slug exists. + public function getSlugOrTitleAttribute() + { + + foreach ($this->_slugFields as $item) { + if (isset($this->$item) && $this->item != '') { + return $this->$item; + } + } + } + + public function getOption($key, $default = null) + { + if (isset($this->_options[$key])) { + return $this->_options[$key]; + } + return $default; + } + + /** + * @param $attributes + */ + public function addField($attributes) + { + /** @var Field $field */ + $field = Field::getInstance($attributes); + $name = $field->getAttribute('name'); + $this->_fields[$name] = $field; + + if ($field->getAttribute('fillable')) { + $this->fillable[] = $name; + } + if ($field->getAttribute('guarded')) { + $this->guarded[] = $name; + } + if ($field->getAttribute('hidden')) { + $this->hidden[] = $name; + } + } + + public function generateCode() + { + $this->_generateControllerCode(); + } + + protected function _generateControllerCode() + { + $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 = ['CONTROLLERCLASS' => $this->getControllerClass(), + 'ROUTEURL' => $this->getOption('name'), + 'SINGULAR' => $this->getOption('singular', $this->getOption('name')), + 'PLURAL' => $this->getOption('plural', ''), + 'MODELNAMESPACE' => get_class($this), + 'EXTENDS' => $this->nested ? 'CubistNestedMagicController' : 'CubistMagicController', + ]; + + $res = file_get_contents($stub); + foreach ($vars as $name => $value) { + $res = str_replace('_' . $name . '_', $value, $res); + } + + if (!file_exists(dirname($dest))) { + mkdir(dirname($dest), 0777, true); + } + + file_put_contents($dest, $res); + } + + public function getStudlyName() + { + return Str::studly($this->getOption('name')); + } + + /** + * @param $schema Schema + */ + public function setSchema($schema) + { + $table = $schema->createTable($this->table); + try { + $table->addColumn($this->primaryKey, self::toDoctrineType($this->keyType), ['autoincrement' => $this->incrementing, 'unsigned' => true]); + } catch (\Exception $e) { + return $e->getMessage(); + } + + $table->setPrimaryKey([$this->primaryKey], 'pk_' . $this->table); + + foreach ($this->_fields as $field) { + $field->defineDbColumn($table); + } + + if ($this->nested) { + $table->addColumn('parent_id', 'integer', ['unsigned' => true, 'notnull' => false]); + $table->addIndex(['parent_id']); + $table->addColumn('lft', 'integer', ['unsigned' => true, 'default' => 0]); + $table->addIndex(['lft']); + $table->addColumn('rgt', 'integer', ['unsigned' => true, 'default' => 0]); + $table->addIndex(['rgt']); + $table->addColumn('depth', 'integer', ['unsigned' => true, 'default' => 0]); + } + + if ($this->timestamps) { + $options = ['notnull' => false]; + $table->addColumn(static::CREATED_AT, 'date', $options); + $table->addColumn(static::UPDATED_AT, 'date', $options); + $table->addColumn('deleted_at', 'date', $options); + } + } + + public function replicate(array $except = null) + { + $this->replicateSluggable($except); + } + + public static function toDoctrineType($type) + { + if (isset(self::$_doctrineTypesMapping[$type])) { + return self::$_doctrineTypesMapping[$type]; + } + return $type; + } + + +} diff --git a/src/app/Magic/Models/CubistMagicModel.php b/src/app/Magic/Models/CubistMagicModel.php new file mode 100644 index 0000000..8fdb583 --- /dev/null +++ b/src/app/Magic/Models/CubistMagicModel.php @@ -0,0 +1,8 @@ + 'integer']; - - protected $nested = false; - protected $primaryKey = 'id'; - public $timestamps = true; - - /** - * @var Field[] - */ - protected $_fields = []; - - /** - * @var array - */ - protected $_options = []; - - /** - * @var array - */ - protected $_slugFields = ['slug', 'title', 'name']; - - public function __construct(array $attributes = []) - { - $this->setFields(); - $this->bootIfNotBooted(); - $this->initializeTraits(); - $this->syncOriginal(); - $this->fill($attributes); - } - - public function setFields() - { - - } - - public function getFields() - { - return $this->_fields; - } - - /** - * Return the sluggable configuration array for this model. - * - * @return array - */ - public function sluggable() - { - return [ - 'slug' => [ - 'source' => 'slug_or_title', - ], - ]; - } - - // The slug is created automatically from the "title" field if no slug exists. - public function getSlugOrTitleAttribute() - { - - foreach ($this->_slugFields as $item) { - if (isset($this->$item) && $this->item != '') { - return $this->$item; - } - } - } - - public function getOption($key, $default = null) - { - if (isset($this->_options[$key])) { - return $this->_options[$key]; - } - return $default; - } - - /** - * @param $attributes - */ - public function addField($attributes) - { - /** @var Field $field */ - $field = Field::getInstance($attributes); - $name = $field->getAttribute('name'); - $this->_fields[$name] = $field; - - if ($field->getAttribute('fillable')) { - $this->fillable[] = $name; - } - if ($field->getAttribute('guarded')) { - $this->guarded[] = $name; - } - if ($field->getAttribute('hidden')) { - $this->hidden[] = $name; - } - } - - public function generateCode() - { - $this->_generateControllerCode(); - } - - protected function _generateControllerCode() - { - $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 = ['CONTROLLERCLASS' => $this->getControllerClass(), - 'ROUTEURL' => $this->getOption('name'), - 'SINGULAR' => $this->getOption('singular', $this->getOption('name')), - 'PLURAL' => $this->getOption('plural', ''), - 'MODELNAMESPACE' => get_class($this), - 'EXTENDS' => $this->nested ? 'CubistNestedMagicController' : 'CubistMagicController', - ]; - - $res = file_get_contents($stub); - foreach ($vars as $name => $value) { - $res = str_replace('_' . $name . '_', $value, $res); - } - - if (!file_exists(dirname($dest))) { - mkdir(dirname($dest), 0777, true); - } - - file_put_contents($dest, $res); - } - - public function getStudlyName() - { - return Str::studly($this->getOption('name')); - } - - /** - * @param $schema Schema - */ - public function setSchema($schema) - { - $table = $schema->createTable($this->table); - try { - $table->addColumn($this->primaryKey, self::toDoctrineType($this->keyType), ['autoincrement' => $this->incrementing, 'unsigned' => true]); - } catch (\Exception $e) { - return $e->getMessage(); - } - - $table->setPrimaryKey([$this->primaryKey], 'pk_' . $this->table); - - foreach ($this->_fields as $field) { - $field->defineDbColumn($table); - } - - if ($this->nested) { - $table->addColumn('parent_id', 'integer', ['unsigned' => true, 'notnull' => false]); - $table->addIndex(['parent_id']); - $table->addColumn('lft', 'integer', ['unsigned' => true, 'default' => 0]); - $table->addIndex(['lft']); - $table->addColumn('rgt', 'integer', ['unsigned' => true, 'default' => 0]); - $table->addIndex(['rgt']); - $table->addColumn('depth', 'integer', ['unsigned' => true, 'default' => 0]); - } - - if ($this->timestamps) { - $options = ['notnull' => false]; - $table->addColumn(static::CREATED_AT, 'date', $options); - $table->addColumn(static::UPDATED_AT, 'date', $options); - $table->addColumn('deleted_at', 'date', $options); - } - } - - public function replicate(array $except = null) - { - $this->replicateSluggable($except); - } - - public static function toDoctrineType($type) - { - if (isset(self::$_doctrineTypesMapping[$type])) { - return self::$_doctrineTypesMapping[$type]; - } - return $type; - } - - -} diff --git a/src/app/Magic/Models/CubistMagicNestedModel.php b/src/app/Magic/Models/CubistMagicNestedModel.php index 8081be4..0f6902b 100644 --- a/src/app/Magic/Models/CubistMagicNestedModel.php +++ b/src/app/Magic/Models/CubistMagicNestedModel.php @@ -6,7 +6,7 @@ namespace Cubist\Backpack\app\Magic\Models; use Webfactor\Laravel\Backpack\NestedModels\Traits\NestedModelTrait; -class CubistMagicNestedModel extends CubistMagicModelAbstract +class CubistMagicNestedModel extends CubistMagicModel { use NestedModelTrait { replicate as private replicateNodeTrait;