Initial Commit
This commit is contained in:
Executable
+199
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace mpba\Modules\Support\Migrations;
|
||||
|
||||
class NameParser
|
||||
{
|
||||
/**
|
||||
* The migration name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* The array data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* The available schema actions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $actions = [
|
||||
'create' => [
|
||||
'create',
|
||||
'make',
|
||||
],
|
||||
'delete' => [
|
||||
'delete',
|
||||
'remove',
|
||||
],
|
||||
'add' => [
|
||||
'add',
|
||||
'update',
|
||||
'append',
|
||||
'insert',
|
||||
],
|
||||
'drop' => [
|
||||
'destroy',
|
||||
'drop',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->data = $this->fetchData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get original migration name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOriginalName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get schema type or action.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAction()
|
||||
{
|
||||
return head($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the table will be used.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTableName()
|
||||
{
|
||||
$matches = array_reverse($this->getMatches());
|
||||
|
||||
return array_shift($matches);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get matches data from regex.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMatches()
|
||||
{
|
||||
preg_match($this->getPattern(), $this->name, $matches);
|
||||
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name pattern.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPattern()
|
||||
{
|
||||
switch ($action = $this->getAction()) {
|
||||
case 'add':
|
||||
case 'append':
|
||||
case 'update':
|
||||
case 'insert':
|
||||
return "/{$action}_(.*)_to_(.*)_table/";
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
case 'remove':
|
||||
case 'alter':
|
||||
return "/{$action}_(.*)_from_(.*)_table/";
|
||||
break;
|
||||
|
||||
default:
|
||||
return "/{$action}_(.*)_table/";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the migration name to an array data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function fetchData()
|
||||
{
|
||||
return explode('_', $this->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the array data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given type is same with the current schema action or type.
|
||||
*
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is($type)
|
||||
{
|
||||
return $type === $this->getAction();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the current schema action is a adding action.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdd()
|
||||
{
|
||||
return in_array($this->getAction(), $this->actions['add']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the current schema action is a deleting action.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDelete()
|
||||
{
|
||||
return in_array($this->getAction(), $this->actions['delete']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the current schema action is a creating action.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isCreate()
|
||||
{
|
||||
return in_array($this->getAction(), $this->actions['create']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the current schema action is a dropping action.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDrop()
|
||||
{
|
||||
return in_array($this->getAction(), $this->actions['drop']);
|
||||
}
|
||||
}
|
||||
Executable
+276
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
|
||||
namespace mpba\Modules\Support\Migrations;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class SchemaParser implements Arrayable
|
||||
{
|
||||
/**
|
||||
* The array of custom attributes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $customAttributes = [
|
||||
'remember_token' => 'rememberToken()',
|
||||
'soft_delete' => 'softDeletes()',
|
||||
];
|
||||
|
||||
/**
|
||||
* The migration schema.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $schema;
|
||||
|
||||
/**
|
||||
* The relationship keys.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $relationshipKeys = [
|
||||
'belongsTo',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create new instance.
|
||||
*
|
||||
* @param string|null $schema
|
||||
*/
|
||||
public function __construct($schema = null)
|
||||
{
|
||||
$this->schema = $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a string to array of formatted schema.
|
||||
*
|
||||
* @param string $schema
|
||||
* @return array
|
||||
*/
|
||||
public function parse($schema)
|
||||
{
|
||||
$this->schema = $schema;
|
||||
|
||||
$parsed = [];
|
||||
|
||||
foreach ($this->getSchemas() as $schemaArray) {
|
||||
$column = $this->getColumn($schemaArray);
|
||||
|
||||
$attributes = $this->getAttributes($column, $schemaArray);
|
||||
|
||||
$parsed[$column] = $attributes;
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get array of schema.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSchemas()
|
||||
{
|
||||
if (is_null($this->schema)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return explode(',', str_replace(' ', '', $this->schema));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert string migration to array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->parse($this->schema);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the migration to formatted script.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$results = '';
|
||||
|
||||
foreach ($this->toArray() as $column => $attributes) {
|
||||
$results .= $this->createField($column, $attributes);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render up migration fields.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render down migration fields.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$results = '';
|
||||
|
||||
foreach ($this->toArray() as $column => $attributes) {
|
||||
$attributes = [head($attributes)];
|
||||
$results .= $this->createField($column, $attributes, 'remove');
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create field.
|
||||
*
|
||||
* @param string $column
|
||||
* @param array $attributes
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
public function createField($column, $attributes, $type = 'add')
|
||||
{
|
||||
$results = "\t\t\t".'$table';
|
||||
|
||||
foreach ($attributes as $key => $field) {
|
||||
if (in_array($column, $this->relationshipKeys)) {
|
||||
$results .= $this->addRelationColumn($key, $field, $column);
|
||||
} else {
|
||||
$results .= $this->{"{$type}Column"}($key, $field, $column);
|
||||
}
|
||||
}
|
||||
|
||||
return $results.';'.PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add relation column.
|
||||
*
|
||||
* @param int $key
|
||||
* @param string $field
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
protected function addRelationColumn($key, $field, $column)
|
||||
{
|
||||
if ($key === 0) {
|
||||
$relatedColumn = Str::snake(class_basename($field)).'_id';
|
||||
|
||||
return "->integer('{$relatedColumn}')->unsigned();".PHP_EOL."\t\t\t"."\$table->foreign('{$relatedColumn}')";
|
||||
}
|
||||
if ($key === 1) {
|
||||
return "->references('{$field}')";
|
||||
}
|
||||
if ($key === 2) {
|
||||
return "->on('{$field}')";
|
||||
}
|
||||
if (Str::contains($field, '(')) {
|
||||
return '->'.$field;
|
||||
}
|
||||
|
||||
return '->'.$field.'()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format field to script.
|
||||
*
|
||||
* @param int $key
|
||||
* @param string $field
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
protected function addColumn($key, $field, $column)
|
||||
{
|
||||
if ($this->hasCustomAttribute($column)) {
|
||||
return '->'.$field;
|
||||
}
|
||||
|
||||
if ($key == 0) {
|
||||
return '->'.$field."('".$column."')";
|
||||
}
|
||||
|
||||
if (Str::contains($field, '(')) {
|
||||
return '->'.$field;
|
||||
}
|
||||
|
||||
return '->'.$field.'()';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format field to script.
|
||||
*
|
||||
* @param int $key
|
||||
* @param string $field
|
||||
* @param string $column
|
||||
* @return string
|
||||
*/
|
||||
protected function removeColumn($key, $field, $column)
|
||||
{
|
||||
if ($this->hasCustomAttribute($column)) {
|
||||
return '->'.$field;
|
||||
}
|
||||
|
||||
return '->dropColumn('."'".$column."')";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get column name from schema.
|
||||
*
|
||||
* @param string $schema
|
||||
* @return string
|
||||
*/
|
||||
public function getColumn($schema)
|
||||
{
|
||||
return Arr::get(explode(':', $schema), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get column attributes.
|
||||
*
|
||||
* @param string $column
|
||||
* @param string $schema
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributes($column, $schema)
|
||||
{
|
||||
$fields = str_replace($column.':', '', $schema);
|
||||
|
||||
return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given column is exist in customAttributes array.
|
||||
*
|
||||
* @param string $column
|
||||
* @return bool
|
||||
*/
|
||||
public function hasCustomAttribute($column)
|
||||
{
|
||||
return array_key_exists($column, $this->customAttributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom attributes value.
|
||||
*
|
||||
* @param string $column
|
||||
* @return array
|
||||
*/
|
||||
public function getCustomAttribute($column)
|
||||
{
|
||||
return (array) $this->customAttributes[$column];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user