Initial Commit

This commit is contained in:
Mark Gregory
2026-04-21 09:12:51 +01:00
commit 6bc3c69c3e
151 changed files with 11917 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
<?php
namespace mpba\Modules\Support\Config;
class GenerateConfigReader
{
public static function read(string $value): GeneratorPath
{
return new GeneratorPath(config("modules.paths.generator.$value"));
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace mpba\Modules\Support\Config;
class GeneratorPath
{
private $path;
private $generate;
private $namespace;
public function __construct($config)
{
if (is_array($config)) {
$this->path = $config['path'];
$this->generate = $config['generate'];
$this->namespace = $config['namespace'] ?? $this->convertPathToNamespace($config['path']);
return;
}
$this->path = $config;
$this->generate = (bool) $config;
$this->namespace = $config;
}
public function getPath()
{
return $this->path;
}
public function generate(): bool
{
return $this->generate;
}
public function getNamespace()
{
return $this->namespace;
}
private function convertPathToNamespace($path)
{
return str_replace('/', '\\', $path);
}
}
+199
View File
@@ -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']);
}
}
+276
View File
@@ -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];
}
}
+165
View File
@@ -0,0 +1,165 @@
<?php
namespace mpba\Modules\Support;
class Stub
{
/**
* The stub path.
*
* @var string
*/
protected $path;
/**
* The base path of stub file.
*
* @var null|string
*/
protected static $basePath = null;
/**
* The replacements array.
*
* @var array
*/
protected $replaces = [];
/**
* The contructor.
*
* @param string $path
*/
public function __construct($path, array $replaces = [])
{
$this->path = $path;
$this->replaces = $replaces;
}
/**
* Create new self instance.
*
* @param string $path
* @return self
*/
public static function create($path, array $replaces = [])
{
return new static($path, $replaces);
}
/**
* Set stub path.
*
* @param string $path
* @return self
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get stub path.
*
* @return string
*/
public function getPath()
{
$path = static::getBasePath().$this->path;
return file_exists($path) ? $path : __DIR__.'/../Commands/stubs'.$this->path;
}
/**
* Set base path.
*
* @param string $path
*/
public static function setBasePath($path)
{
static::$basePath = $path;
}
/**
* Get base path.
*
* @return string|null
*/
public static function getBasePath()
{
return static::$basePath;
}
/**
* Get stub contents.
*
* @return mixed|string
*/
public function getContents()
{
$contents = file_get_contents($this->getPath());
foreach ($this->replaces as $search => $replace) {
$contents = str_replace('$'.strtoupper($search).'$', $replace, $contents);
}
return $contents;
}
/**
* Get stub contents.
*
* @return string
*/
public function render()
{
return $this->getContents();
}
/**
* Save stub to specific path.
*
* @param string $path
* @param string $filename
* @return bool
*/
public function saveTo($path, $filename)
{
return file_put_contents($path.'/'.$filename, $this->getContents());
}
/**
* Set replacements array.
*
*
* @return $this
*/
public function replace(array $replaces = [])
{
$this->replaces = $replaces;
return $this;
}
/**
* Get replacements.
*
* @return array
*/
public function getReplaces()
{
return $this->replaces;
}
/**
* Handle magic method __toString.
*
* @return string
*/
public function __toString()
{
return $this->render();
}
}