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;
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);
}
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
}
/**
- * @param $model CubistMagicModelAbstract
+ * @param $model CubistMagicAbstractModel
*/
public function _generate($model)
{
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;
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;
}
/**
- * @return CubistMagicModelAbstract
+ * @return CubistMagicAbstractModel
*/
public function getModelInstance()
{
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
{
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]);
}
--- /dev/null
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+use Backpack\CRUD\CrudTrait;
+use Cubist\Backpack\app\Magic\Fields\Field;
+use Cubist\Backpack\app\Magic\Util;
+use Cviebrock\EloquentSluggable\Sluggable;
+use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
+use Doctrine\DBAL\Schema\Schema;
+use Illuminate\Database\Eloquent\Model;
+use Cubist\Backpack\app\Magic\CubistMagicAttribute;
+use Illuminate\Support\Str;
+
+class CubistMagicAbstractModel extends Model
+{
+ use CubistMagicAttribute;
+ use SluggableScopeHelpers;
+ use CrudTrait;
+ use Sluggable {
+ replicate as protected replicateSluggable;
+ }
+
+ protected static $_doctrineTypesMapping = ['int' => '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;
+ }
+
+
+}
--- /dev/null
+<?php
+
+namespace Cubist\Backpack\app\Magic\Models;
+
+class CubistMagicModel extends CubistMagicAbstractModel
+{
+
+}
+++ /dev/null
-<?php
-
-
-namespace Cubist\Backpack\app\Magic\Models;
-
-use Backpack\CRUD\CrudTrait;
-use Cubist\Backpack\app\Magic\Fields\Field;
-use Cubist\Backpack\app\Magic\Util;
-use Cviebrock\EloquentSluggable\Sluggable;
-use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
-use Doctrine\DBAL\Schema\Schema;
-use Illuminate\Database\Eloquent\Model;
-use Cubist\Backpack\app\Magic\CubistMagicAttribute;
-use Illuminate\Support\Str;
-
-class CubistMagicModelAbstract extends Model
-{
- use CubistMagicAttribute;
- use SluggableScopeHelpers;
- use CrudTrait;
- use Sluggable {
- replicate as protected replicateSluggable;
- }
-
- protected static $_doctrineTypesMapping = ['int' => '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;
- }
-
-
-}
use Webfactor\Laravel\Backpack\NestedModels\Traits\NestedModelTrait;
-class CubistMagicNestedModel extends CubistMagicModelAbstract
+class CubistMagicNestedModel extends CubistMagicModel
{
use NestedModelTrait {
replicate as private replicateNodeTrait;