Skip to content

Commit 6398902

Browse files
committed
feat: #754 events support added
1 parent b93e06c commit 6398902

10 files changed

+165
-1
lines changed

src/Commands/API/APIGeneratorCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InfyOm\Generator\Commands\BaseCommand;
66
use InfyOm\Generator\Common\CommandData;
7+
use InfyOm\Generator\Utils\FileUtil;
78

89
class APIGeneratorCommand extends BaseCommand
910
{
@@ -39,12 +40,14 @@ public function __construct()
3940
public function handle()
4041
{
4142
parent::handle();
43+
$this->commandData->fireEvent('api', FileUtil::FILE_CREATING);
4244

4345
$this->generateCommonItems();
4446

4547
$this->generateAPIItems();
4648

4749
$this->performPostActionsWithMigration();
50+
$this->commandData->fireEvent('api', FileUtil::FILE_CREATED);
4851
}
4952

5053
/**

src/Commands/APIScaffoldGeneratorCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace InfyOm\Generator\Commands;
44

55
use InfyOm\Generator\Common\CommandData;
6+
use InfyOm\Generator\Utils\FileUtil;
67

78
class APIScaffoldGeneratorCommand extends BaseCommand
89
{
@@ -38,6 +39,7 @@ public function __construct()
3839
public function handle()
3940
{
4041
parent::handle();
42+
$this->commandData->fireEvent('api_scaffold', FileUtil::FILE_CREATING);
4143

4244
$this->generateCommonItems();
4345

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

4850
$this->performPostActionsWithMigration();
51+
$this->commandData->fireEvent('api_scaffold', FileUtil::FILE_CREATED);
4952
}
5053

5154
/**

src/Commands/RollbackGeneratorCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use InfyOm\Generator\Generators\Scaffold\RequestGenerator;
1919
use InfyOm\Generator\Generators\Scaffold\RoutesGenerator;
2020
use InfyOm\Generator\Generators\Scaffold\ViewGenerator;
21+
use InfyOm\Generator\Utils\FileUtil;
2122
use Symfony\Component\Console\Input\InputArgument;
2223
use Symfony\Component\Console\Input\InputOption;
2324

@@ -64,7 +65,8 @@ public function __construct()
6465
*/
6566
public function handle()
6667
{
67-
if (!in_array($this->argument('type'), [
68+
$type = $this->argument('type');
69+
if (!in_array($type, [
6870
CommandData::$COMMAND_TYPE_API,
6971
CommandData::$COMMAND_TYPE_SCAFFOLD,
7072
CommandData::$COMMAND_TYPE_API_SCAFFOLD,
@@ -73,6 +75,7 @@ public function handle()
7375
}
7476

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

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

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

8993
return;
9094
}
@@ -137,6 +141,8 @@ public function handle()
137141

138142
$this->info('Generating autoload files');
139143
$this->composer->dumpOptimized();
144+
145+
$this->commandData->fireEvent($type, FileUtil::FILE_DELETED);
140146
}
141147

142148
/**

src/Commands/Scaffold/ScaffoldGeneratorCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use InfyOm\Generator\Commands\BaseCommand;
66
use InfyOm\Generator\Common\CommandData;
7+
use InfyOm\Generator\Utils\FileUtil;
78

89
class ScaffoldGeneratorCommand extends BaseCommand
910
{
@@ -41,11 +42,13 @@ public function handle()
4142
parent::handle();
4243

4344
if ($this->checkIsThereAnyDataToGenerate()) {
45+
$this->commandData->fireEvent('scaffold', FileUtil::FILE_CREATING);
4446
$this->generateCommonItems();
4547

4648
$this->generateScaffoldItems();
4749

4850
$this->performPostActionsWithMigration();
51+
$this->commandData->fireEvent('scaffold', FileUtil::FILE_CREATED);
4952
} else {
5053
$this->commandData->commandInfo('There are not enough input fields for scaffold generation.');
5154
}

src/Common/CommandData.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
use Exception;
66
use Illuminate\Console\Command;
77
use Illuminate\Support\Str;
8+
use InfyOm\Generator\Events\GeneratorFileCreated;
9+
use InfyOm\Generator\Events\GeneratorFileCreating;
10+
use InfyOm\Generator\Events\GeneratorFileDeleted;
11+
use InfyOm\Generator\Events\GeneratorFileDeleting;
12+
use InfyOm\Generator\Utils\FileUtil;
813
use InfyOm\Generator\Utils\GeneratorFieldsInputUtil;
914
use InfyOm\Generator\Utils\TableFieldsGenerator;
1015

@@ -307,4 +312,31 @@ private function getInputFromTable()
307312
$this->fields = $tableFieldsGenerator->fields;
308313
$this->relations = $tableFieldsGenerator->relations;
309314
}
315+
316+
public function prepareEventsData()
317+
{
318+
$data['modelName'] = $this->modelName;
319+
$data['tableName'] = $this->config->tableName;
320+
$data['nsModel'] = $this->config->nsModel;
321+
322+
return $data;
323+
}
324+
325+
public function fireEvent($commandType, $eventType)
326+
{
327+
switch ($eventType) {
328+
case FileUtil::FILE_CREATING:
329+
event(new GeneratorFileCreating($commandType, $this->prepareEventsData()));
330+
break;
331+
case FileUtil::FILE_CREATED:
332+
event(new GeneratorFileCreated($commandType, $this->prepareEventsData()));
333+
break;
334+
case FileUtil::FILE_DELETING:
335+
event(new GeneratorFileDeleting($commandType, $this->prepareEventsData()));
336+
break;
337+
case FileUtil::FILE_DELETED:
338+
event(new GeneratorFileDeleted($commandType, $this->prepareEventsData()));
339+
break;
340+
}
341+
}
310342
}

src/Events/GeneratorFileCreated.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace InfyOm\Generator\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
/**
8+
* Class GeneratorFileCreated.
9+
*/
10+
class GeneratorFileCreated
11+
{
12+
use SerializesModels;
13+
14+
public $type;
15+
public $data;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @param string $type
21+
* @param array $data
22+
*/
23+
public function __construct($type, $data)
24+
{
25+
$this->type = $type;
26+
$this->data = $data;
27+
}
28+
}

src/Events/GeneratorFileCreating.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace InfyOm\Generator\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
/**
8+
* Class GeneratorFileCreating.
9+
*/
10+
class GeneratorFileCreating
11+
{
12+
use SerializesModels;
13+
14+
public $type;
15+
public $data;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @param string $type
21+
* @param array $data
22+
*/
23+
public function __construct($type, $data)
24+
{
25+
$this->type = $type;
26+
$this->data = $data;
27+
}
28+
}

src/Events/GeneratorFileDeleted.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace InfyOm\Generator\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
/**
8+
* Class GeneratorFileDeleted.
9+
*/
10+
class GeneratorFileDeleted
11+
{
12+
use SerializesModels;
13+
14+
public $type;
15+
public $data;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @param string $type
21+
* @param array $data
22+
*/
23+
public function __construct($type, $data)
24+
{
25+
$this->type = $type;
26+
$this->data = $data;
27+
}
28+
}

src/Events/GeneratorFileDeleting.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace InfyOm\Generator\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
/**
8+
* Class GeneratorFileDeleting.
9+
*/
10+
class GeneratorFileDeleting
11+
{
12+
use SerializesModels;
13+
14+
public $type;
15+
public $data;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @param string $type
21+
* @param array $data
22+
*/
23+
public function __construct($type, $data)
24+
{
25+
$this->type = $type;
26+
$this->data = $data;
27+
}
28+
}

src/Utils/FileUtil.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
class FileUtil
66
{
7+
const FILE_CREATING = 1;
8+
const FILE_CREATED = 2;
9+
const FILE_DELETING = 3;
10+
const FILE_DELETED = 4;
11+
712
public static function createFile($path, $fileName, $contents)
813
{
914
if (!file_exists($path)) {

0 commit comments

Comments
 (0)