Initial commit

This commit is contained in:
omair saleh
2020-08-12 04:16:41 +08:00
commit 57bd2be885
476 changed files with 41783 additions and 0 deletions
@@ -0,0 +1,140 @@
<?php
namespace App\Classes\CodeGenerator\Generators\Micros;
use App\Classes\CodeGenerator\Common\CommandData;
use App\Classes\CodeGenerator\Common\GenerateGetters;
use App\Classes\CodeGenerator\Generators\BaseGenerator;
use App\Classes\CodeGenerator\Utils\FileUtil;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class DataTransferObjectGenerator extends BaseGenerator
{
/** @var CommandData */
private $commandData;
/** @var string */
private $path;
/** @var string */
private $fileName;
/**
* FactoryGenerator constructor.
*
* @param CommandData $commandData
*/
public function __construct(CommandData $commandData)
{
parent::__construct();
$this->commandData = $commandData;
$this->path = app_path('Classes/Modules/'. str::pluralStudly($this->commandData->modelName).'/DataTransferObjects/');
$this->fileName = Str::studly(Str::singular($this->commandData->modelName)).'Object.php';
}
public function generate()
{
$templateData = $this->generatorHelpers->get_template('Micros.data_transfer_object');
$templateData = $this->fillTemplate($templateData);
FileUtil::createFile($this->path, $this->fileName, $templateData);
$this->commandData->commandComment("\nDataTransferObject created: ");
$this->commandData->commandObj->info($this->fileName);
}
/**
* @param string $templateData
*
* @return mixed|string
*/
private function fillTemplate($templateData)
{
$properties = [];
$docs = [];
$injection = [];
$body = [];
$getters = [];
foreach ($this->commandData->fields as $field) {
if(!in_array($field->name, $this->commandData->config->excludeFields())){
$docType = $this->getPHPDocType($field->fieldType);
$fieldName = $docType === 'bool' ? 'is'.str::studly($field->name) : str::camel($field->name);
$properties[] = '/** @var '.$docType.' */'.PHP_EOL.$this->generatorHelpers->generator_nl_tab(0, 1).'private $'.str::camel($fieldName).';';
$docs[] = '* @param '.$docType.' $'.str::camel($fieldName);
$injection[] = $docType.' $'.str::camel($fieldName);
$body[] = '$this->'.str::camel($fieldName).' = $'.str::camel($fieldName).';';
$getters[] = (new GenerateGetters())->generate($fieldName, $docType);
}
}
$templateData = $this->generatorHelpers->fill_template($this->commandData->dynamicVars, $templateData);
$templateData = str_replace(
'$PROPERTIES$',
implode(PHP_EOL.$this->generatorHelpers->generator_nl_tab(1, 1), $properties),
$templateData
);
$templateData = str_replace(
'$CONSTRUCTOR_DOCS$',
implode($this->generatorHelpers->generator_nl_tab(1, 2), $docs),
$templateData
);
$templateData = str_replace(
'$CONSTRUCTOR_PROPERTIES$',
implode(', ', $injection),
$templateData
);
$templateData = str_replace(
'$CONSTRUCTOR_BODY$',
implode($this->generatorHelpers->generator_nl_tab(1, 2), $body),
$templateData
);
$templateData = str_replace(
'$GETTER_FUNCTIONS$',
implode($this->generatorHelpers->generator_nl_tab(1, 0), $getters),
$templateData
);
return $templateData;
}
private function getPHPDocType($db_type){
switch ($db_type) {
case 'text':
return 'string';
case 'datetime':
return '\Carbon\Carbon';
case 'boolean':
return 'bool';
default:
return $db_type;
}
}
public function rollback()
{
if ($this->rollbackFile($this->path, $this->fileName)) {
File::deleteDirectory($this->path);
$this->commandData->commandComment('DataTransferObject file deleted: '.$this->fileName);
}
}
}
@@ -0,0 +1,75 @@
<?php
namespace App\Classes\CodeGenerator\Generators\Micros;
use App\Classes\CodeGenerator\Common\CommandData;
use App\Classes\CodeGenerator\Generators\BaseGenerator;
use App\Classes\CodeGenerator\Utils\FileUtil;
use Illuminate\Support\Str;
class ResourceGenerator extends BaseGenerator
{
/** @var CommandData */
private $commandData;
/** @var string */
private $path;
/** @var string */
private $fileName;
/**
* FactoryGenerator constructor.
*
* @param CommandData $commandData
*/
public function __construct(CommandData $commandData)
{
parent::__construct();
$this->commandData = $commandData;
$this->path = app_path('Http/Resources/');
$this->fileName = Str::studly(Str::singular($this->commandData->modelName)).'Resource.php';
}
public function generate()
{
$templateData = $this->generatorHelpers->get_template('Resource.model_resource');
$templateData = $this->generatorHelpers->fill_template($this->commandData->dynamicVars, $templateData);
$templateData = str_replace('$FIELDS$', implode(','.$this->generatorHelpers->generator_nl_tab(1, 3), $this->generateFields()), $templateData);
FileUtil::createFile($this->path, $this->fileName, $templateData);
$this->commandData->commandComment("\n Resource Object created: ");
$this->commandData->commandObj->info($this->fileName);
}
private function generateFields()
{
$fields = [];
foreach ($this->commandData->fields as $field) {
$field = "'" . $field->name . "' => " . "$" . "this->" . str::snake($field->name);
$fields[] = $field;
}
return $fields;
}
public function rollback()
{
if ($this->rollbackFile($this->path, $this->fileName)) {
$this->commandData->commandComment('Resource Object file deleted: '.$this->fileName);
}
}
}
@@ -0,0 +1,68 @@
<?php
namespace App\Classes\CodeGenerator\Generators\Micros;
use App\Classes\CodeGenerator\Common\CommandData;
use App\Classes\CodeGenerator\Generators\BaseGenerator;
use App\Classes\CodeGenerator\Utils\FileUtil;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class RulesGenerator extends BaseGenerator
{
/** @var CommandData */
private $commandData;
/** @var string */
private $path;
/**
* FactoryGenerator constructor.
*
* @param CommandData $commandData
*/
public function __construct(CommandData $commandData)
{
parent::__construct();
$this->commandData = $commandData;
$this->path = app_path('Classes/Modules/'. str::pluralStudly($this->commandData->modelName).'/Standards/Rules/');
}
public function generate()
{
foreach(['create', 'list', 'fetch', 'update', 'delete'] as $type){
$name = $type === 'list' ? str::pluralStudly($this->commandData->modelName) : str::singular($this->commandData->modelName);
$filename = 'Can'.str::studly($type.$name).'.php';
$templateData = $this->generatorHelpers->get_template('Rules.can_'.$type);
$templateData = $this->generatorHelpers->fill_template($this->commandData->dynamicVars, $templateData);
FileUtil::createFile($this->path, $filename, $templateData);
$this->commandData->commandComment("\n" . $type . " Rules created: ");
$this->commandData->commandObj->info($filename);
}
}
public function rollback()
{
foreach(['create', 'list', 'fetch', 'update', 'delete'] as $type){
$name = $type === 'list' ? str::pluralStudly($this->commandData->modelName) : str::singular($this->commandData->modelName);
$filename ='Can'.str::studly($type.$name).'.php';
if ($this->rollbackFile($this->path, $filename)) {
$this->commandData->commandComment($type . ' Rules file deleted: '.$filename);
}
}
File::deleteDirectory($this->path);
}
}
@@ -0,0 +1,96 @@
<?php
namespace App\Classes\CodeGenerator\Generators\Micros;
use App\Classes\CodeGenerator\Common\CommandData;
use App\Classes\CodeGenerator\Generators\BaseGenerator;
use App\Classes\CodeGenerator\Utils\FileUtil;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ServicesGenerator extends BaseGenerator
{
/** @var CommandData */
private $commandData;
/** @var string */
private $path;
/**
* FactoryGenerator constructor.
*
* @param CommandData $commandData
*/
public function __construct(CommandData $commandData)
{
parent::__construct();
$this->commandData = $commandData;
$this->path = app_path('Classes/Modules/'. str::pluralStudly($this->commandData->modelName).'/Services/');
}
public function generate()
{
foreach(['create', 'list', 'fetch', 'update', 'delete'] as $type){
$name = $type === 'list' ? str::pluralStudly($this->commandData->modelName) : str::singular($this->commandData->modelName);
$filename = str::studly($type.($type === 'fetch'?'es':'s').$name).'.php';
$templateData = $this->generatorHelpers->get_template("Services.".str::snake($type.'_service'));
$templateData = $this->generatorHelpers->fill_template($this->commandData->dynamicVars, $templateData);
$templateData = str_replace(
'$FIELDS$',
implode($this->generatorHelpers->generator_nl_tab(1, 2), $this->generateFields()),
$templateData
);
FileUtil::createFile($this->path, $filename, $templateData);
$this->commandData->commandComment("\n" . $type . " service class created: ");
$this->commandData->commandObj->info($filename);
}
}
private function generateFields()
{
$fields = [];
foreach ($this->commandData->fields as $field) {
if(!in_array($field->name, $this->commandData->config->excludeFields())) {
$getterName = $field->dbInput === 'boolean' ? 'is' . str::studly($field->name) : 'get' . str::studly($field->name);
$field = "$" . "model->" . $field->name . " = $" . "object->" . $getterName . "();";
$fields[] = $field;
}
}
return $fields;
}
public function rollback()
{
foreach(['create', 'list', 'fetch', 'update', 'delete'] as $type){
$name = $type === 'list' ? str::pluralStudly($this->commandData->modelName) : str::singular($this->commandData->modelName);
$filename = str::studly($type.($type === 'fetch'?'es':'s').$name).'.php';
if ($this->rollbackFile($this->path, $filename)) {
$this->commandData->commandComment( $type . ' service class file deleted: '.$filename);
}
}
File::deleteDirectory(($this->path));
}
}
@@ -0,0 +1,112 @@
<?php
namespace App\Classes\CodeGenerator\Generators\Micros;
use App\Classes\CodeGenerator\Common\CommandData;
use App\Classes\CodeGenerator\Generators\BaseGenerator;
use App\Classes\CodeGenerator\Utils\FileUtil;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class ValidatorsGenerator extends BaseGenerator
{
/** @var CommandData */
private $commandData;
/** @var string */
private $path;
/** @var string */
private $fileName;
/**
* FactoryGenerator constructor.
*
* @param CommandData $commandData
*/
public function __construct(CommandData $commandData)
{
parent::__construct();
$this->commandData = $commandData;
$this->path = app_path('Classes/Modules/'. str::pluralStudly($this->commandData->modelName).'/Standards/Validators/');
$this->fileName = Str::studly(Str::singular($this->commandData->modelName)).'Validation.php';
}
public function generate()
{
$templateData = $this->generatorHelpers->get_template('Validator.request_validation');
$templateData = $this->generatorHelpers->fill_template($this->commandData->dynamicVars, $templateData);
$templateData = str_replace('$FIELDS$', implode(','.$this->generatorHelpers->generator_nl_tab(1, 3), $this->generateFields()), $templateData);
$templateData = str_replace('$RULES$', implode(','.$this->generatorHelpers->generator_nl_tab(1, 3), $this->generateRules()), $templateData);
FileUtil::createFile($this->path, $this->fileName, $templateData);
$this->commandData->commandComment("\n Request Validator created: ");
$this->commandData->commandObj->info($this->fileName);
}
private function generateRules()
{
$dont_require_fields = [];
$rules = [];
foreach ($this->commandData->fields as $field) {
if (!$field->isPrimary && $field->isNotNull && empty($field->validations) &&
!in_array($field->name, $dont_require_fields)) {
$field->validations = 'required';
}
if (!empty($field->validations)) {
if (Str::contains($field->validations, 'unique:')) {
$rule = explode('|', $field->validations);
// move unique rule to last
usort($rule, function ($record) {
return (Str::contains($record, 'unique:')) ? 1 : 0;
});
$field->validations = implode('|', $rule);
}
$rule = "'".$field->name."' => '".$field->validations."'";
$rules[] = $rule;
}
}
return $rules;
}
private function generateFields()
{
$fields = [];
foreach ($this->commandData->fields as $field) {
if(!in_array($field->name, $this->commandData->config->excludeFields())) {
$getterName = $field->dbInput === 'boolean' ? 'is' . str::studly($field->name) : 'get' . str::studly($field->name);
$field = "'" . $field->name . "' => " . "$" . "object->" . $getterName . "()";
$fields[] = $field;
}
}
return $fields;
}
public function rollback()
{
if ($this->rollbackFile($this->path, $this->fileName)) {
File::deleteDirectory($this->path);
$this->commandData->commandComment('Request Validation file deleted: '.$this->fileName);
}
}
}