Skip to content

Commit

Permalink
feat: #754 events support added
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalinfyom committed Jun 5, 2021
1 parent b93e06c commit 6398902
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Commands/API/APIGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InfyOm\Generator\Commands\BaseCommand;
use InfyOm\Generator\Common\CommandData;
use InfyOm\Generator\Utils\FileUtil;

class APIGeneratorCommand extends BaseCommand
{
Expand Down Expand Up @@ -39,12 +40,14 @@ public function __construct()
public function handle()
{
parent::handle();
$this->commandData->fireEvent('api', FileUtil::FILE_CREATING);

$this->generateCommonItems();

$this->generateAPIItems();

$this->performPostActionsWithMigration();
$this->commandData->fireEvent('api', FileUtil::FILE_CREATED);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/APIScaffoldGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace InfyOm\Generator\Commands;

use InfyOm\Generator\Common\CommandData;
use InfyOm\Generator\Utils\FileUtil;

class APIScaffoldGeneratorCommand extends BaseCommand
{
Expand Down Expand Up @@ -38,6 +39,7 @@ public function __construct()
public function handle()
{
parent::handle();
$this->commandData->fireEvent('api_scaffold', FileUtil::FILE_CREATING);

$this->generateCommonItems();

Expand All @@ -46,6 +48,7 @@ public function handle()
$this->generateScaffoldItems();

$this->performPostActionsWithMigration();
$this->commandData->fireEvent('api_scaffold', FileUtil::FILE_CREATED);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/Commands/RollbackGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use InfyOm\Generator\Generators\Scaffold\RequestGenerator;
use InfyOm\Generator\Generators\Scaffold\RoutesGenerator;
use InfyOm\Generator\Generators\Scaffold\ViewGenerator;
use InfyOm\Generator\Utils\FileUtil;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

Expand Down Expand Up @@ -64,7 +65,8 @@ public function __construct()
*/
public function handle()
{
if (!in_array($this->argument('type'), [
$type = $this->argument('type');
if (!in_array($type, [
CommandData::$COMMAND_TYPE_API,
CommandData::$COMMAND_TYPE_SCAFFOLD,
CommandData::$COMMAND_TYPE_API_SCAFFOLD,
Expand All @@ -73,6 +75,7 @@ public function handle()
}

$this->commandData = new CommandData($this, $this->argument('type'));
$this->commandData->fireEvent($type, FileUtil::FILE_DELETING);
$this->commandData->config->mName = $this->commandData->modelName = $this->argument('model');

$this->commandData->config->init($this->commandData, ['tableName', 'prefix', 'plural', 'views']);
Expand All @@ -85,6 +88,7 @@ public function handle()

$this->info('Generating autoload files');
$this->composer->dumpOptimized();
$this->commandData->fireEvent($type, FileUtil::FILE_DELETED);

return;
}
Expand Down Expand Up @@ -137,6 +141,8 @@ public function handle()

$this->info('Generating autoload files');
$this->composer->dumpOptimized();

$this->commandData->fireEvent($type, FileUtil::FILE_DELETED);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/Scaffold/ScaffoldGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InfyOm\Generator\Commands\BaseCommand;
use InfyOm\Generator\Common\CommandData;
use InfyOm\Generator\Utils\FileUtil;

class ScaffoldGeneratorCommand extends BaseCommand
{
Expand Down Expand Up @@ -41,11 +42,13 @@ public function handle()
parent::handle();

if ($this->checkIsThereAnyDataToGenerate()) {
$this->commandData->fireEvent('scaffold', FileUtil::FILE_CREATING);
$this->generateCommonItems();

$this->generateScaffoldItems();

$this->performPostActionsWithMigration();
$this->commandData->fireEvent('scaffold', FileUtil::FILE_CREATED);
} else {
$this->commandData->commandInfo('There are not enough input fields for scaffold generation.');
}
Expand Down
32 changes: 32 additions & 0 deletions src/Common/CommandData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use InfyOm\Generator\Events\GeneratorFileCreated;
use InfyOm\Generator\Events\GeneratorFileCreating;
use InfyOm\Generator\Events\GeneratorFileDeleted;
use InfyOm\Generator\Events\GeneratorFileDeleting;
use InfyOm\Generator\Utils\FileUtil;
use InfyOm\Generator\Utils\GeneratorFieldsInputUtil;
use InfyOm\Generator\Utils\TableFieldsGenerator;

Expand Down Expand Up @@ -307,4 +312,31 @@ private function getInputFromTable()
$this->fields = $tableFieldsGenerator->fields;
$this->relations = $tableFieldsGenerator->relations;
}

public function prepareEventsData()
{
$data['modelName'] = $this->modelName;
$data['tableName'] = $this->config->tableName;
$data['nsModel'] = $this->config->nsModel;

return $data;
}

public function fireEvent($commandType, $eventType)
{
switch ($eventType) {
case FileUtil::FILE_CREATING:
event(new GeneratorFileCreating($commandType, $this->prepareEventsData()));
break;
case FileUtil::FILE_CREATED:
event(new GeneratorFileCreated($commandType, $this->prepareEventsData()));
break;
case FileUtil::FILE_DELETING:
event(new GeneratorFileDeleting($commandType, $this->prepareEventsData()));
break;
case FileUtil::FILE_DELETED:
event(new GeneratorFileDeleted($commandType, $this->prepareEventsData()));
break;
}
}
}
28 changes: 28 additions & 0 deletions src/Events/GeneratorFileCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace InfyOm\Generator\Events;

use Illuminate\Queue\SerializesModels;

/**
* Class GeneratorFileCreated.
*/
class GeneratorFileCreated
{
use SerializesModels;

public $type;
public $data;

/**
* Create a new event instance.
*
* @param string $type
* @param array $data
*/
public function __construct($type, $data)
{
$this->type = $type;
$this->data = $data;
}
}
28 changes: 28 additions & 0 deletions src/Events/GeneratorFileCreating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace InfyOm\Generator\Events;

use Illuminate\Queue\SerializesModels;

/**
* Class GeneratorFileCreating.
*/
class GeneratorFileCreating
{
use SerializesModels;

public $type;
public $data;

/**
* Create a new event instance.
*
* @param string $type
* @param array $data
*/
public function __construct($type, $data)
{
$this->type = $type;
$this->data = $data;
}
}
28 changes: 28 additions & 0 deletions src/Events/GeneratorFileDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace InfyOm\Generator\Events;

use Illuminate\Queue\SerializesModels;

/**
* Class GeneratorFileDeleted.
*/
class GeneratorFileDeleted
{
use SerializesModels;

public $type;
public $data;

/**
* Create a new event instance.
*
* @param string $type
* @param array $data
*/
public function __construct($type, $data)
{
$this->type = $type;
$this->data = $data;
}
}
28 changes: 28 additions & 0 deletions src/Events/GeneratorFileDeleting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace InfyOm\Generator\Events;

use Illuminate\Queue\SerializesModels;

/**
* Class GeneratorFileDeleting.
*/
class GeneratorFileDeleting
{
use SerializesModels;

public $type;
public $data;

/**
* Create a new event instance.
*
* @param string $type
* @param array $data
*/
public function __construct($type, $data)
{
$this->type = $type;
$this->data = $data;
}
}
5 changes: 5 additions & 0 deletions src/Utils/FileUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class FileUtil
{
const FILE_CREATING = 1;
const FILE_CREATED = 2;
const FILE_DELETING = 3;
const FILE_DELETED = 4;

public static function createFile($path, $fileName, $contents)
{
if (!file_exists($path)) {
Expand Down

0 comments on commit 6398902

Please sign in to comment.