Skip to content

Commit

Permalink
Migration Generator Done
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Jun 5, 2024
1 parent f49cce7 commit 19add13
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
14 changes: 7 additions & 7 deletions src/Commands/CrudMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function handle()
{
try {

$this->createRequests();
$this->createRequest();

$this->createResources();
$this->createResource();

$this->createModelFiles();
$this->createModel();

$this->createController();

Expand All @@ -66,7 +66,7 @@ public function handle()
return self::FAILURE;
}

private function createRequests()
private function createRequest()
{
if (!config('api-crud.templates.request.generate', true)) {
return;
Expand Down Expand Up @@ -103,7 +103,7 @@ private function getResourceName()
return Str::studly($this->argument('name'));
}

private function createResources()
private function createResource()
{
if (!config('api-crud.templates.resource.generate', true)) {
return;
Expand Down Expand Up @@ -137,7 +137,7 @@ private function createController()
/**
* @throws GeneratorException
*/
private function createModelFiles()
private function createModel()
{

if (!config('api-crud.templates.model.generate', true)) {
Expand All @@ -146,7 +146,7 @@ private function createModelFiles()
$this->call('laraflow:make-model', [
'name' => $this->getResourceName(),
'module' => $this->getModuleName(),
// '--migration' => config('api-crud.templates.migration.generate', true)
'--migration' => config('api-crud.templates.migration.generate', true)
]);
}

Expand Down
18 changes: 14 additions & 4 deletions src/Commands/MigrationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use Laraflow\ApiCrud\Abstracts\GeneratorCommand;
use Laraflow\ApiCrud\Support\Config\GenerateConfigReader;
use Laraflow\ApiCrud\Support\Migrations\NameParser;
use Laraflow\ApiCrud\Support\Migrations\SchemaParser;
use Laraflow\ApiCrud\Support\Stub;
Expand Down Expand Up @@ -49,9 +50,6 @@ class MigrationMakeCommand extends GeneratorCommand
*/
public function handle(): int
{

$this->components->info('Creating migration...');

if (parent::handle() === E_ERROR) {
return E_ERROR;
}
Expand Down Expand Up @@ -100,7 +98,7 @@ protected function getTemplateContents(): string

return new Stub('/migration.stub', [
'class' => $this->getClass(),
'table' => $parser->getTableName(),
'table' => $this->argument('name'),
'fields' => $this->getSchemaParser()->render(),
]);
}
Expand Down Expand Up @@ -135,4 +133,16 @@ private function getSchemaName()
{
return $this->argument('name');
}

protected function getDestinationFilePath(): string
{
$config = GenerateConfigReader::read($this->type);

return $config->getPath().'/' .$this->getFileName();
}

protected function getFileName()
{
return date('Y_m_d_His_\c\r\e\a\t\e_') . Str::snake($this->argument('name')) . "_table.php";
}
}
32 changes: 7 additions & 25 deletions src/Commands/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,11 @@ public function handle(): int
private function handleOptionalMigrationOption()
{
if ($this->option('migration') === true) {
$migrationName = 'create_'.$this->createMigrationName().'_table';
$migrationName = $this->getTableName();
$this->call('laraflow:make-migration', ['name' => $migrationName, 'module' => $this->argument('module')]);
}
}

/**
* Create a proper migration name:
* ProductDetail: product_details
* Product: products
*
* @return string
*/
private function createMigrationName()
{
$pieces = preg_split('/(?=[A-Z])/', $this->argument('model'), -1, PREG_SPLIT_NO_EMPTY);

$string = '';
foreach ($pieces as $i => $piece) {
if ($i + 1 < count($pieces)) {
$string .= strtolower($piece).'_';
} else {
$string .= Str::plural(strtolower($piece));
}
}

return $string;
}

/**
* @return mixed|string
*/
Expand Down Expand Up @@ -154,7 +131,7 @@ protected function getTemplateContents(): string
'NAME' => $this->getModelName(),
'ROUTE_NAME' => Str::plural(Str::lower(Str::kebab($this->getModelName()))),
'JSON_NAME' => Str::lower(Str::snake($this->getModelName())).'_data',
'TABLE' => Str::plural($this->getModelName()),
'TABLE' => $this->getTableName(),
'FILLABLE' => $this->getFillable(),
'NAMESPACE' => $this->getClassNamespace($this->getModuleName()),
'CLASS' => $this->getClass(),
Expand All @@ -166,6 +143,11 @@ protected function getTemplateContents(): string
]))->render();
}

private function GetTableName()
{
return Str::replace('/', '', Str::lower(Str::snake(Str::plural($this->getModelName()))));
}

/**
* @return string
*/
Expand Down

0 comments on commit 19add13

Please sign in to comment.