Skip to content

Commit f7f2354

Browse files
committed
Initial Commit
0 parents  commit f7f2354

19 files changed

+1528
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
/.idea
3+
/.vagrant
4+
.vscode

Commands/MakeAllRepository.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class MakeAllRepository extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'command:make-all-repository {table_names*} {--k|foreign-keys : Detect foreign keys} {--d|delete : Delete resource} {--f|force : Override/Delete existing classes}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Create all classes necessary for repository.';
22+
23+
/**
24+
* Create a new command instance.
25+
*
26+
* @return void
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Execute the console command.
35+
*/
36+
public function handle()
37+
{
38+
$tableNames = $this->argument('table_names');
39+
$force = $this->option('force');
40+
$delete = $this->option('delete');
41+
$detectForeignKeys = $this->option('foreign-keys');
42+
43+
foreach ($tableNames as $_tableName) {
44+
$arguments = [
45+
'table_name' => $_tableName,
46+
'--foreign-keys' => $detectForeignKeys,
47+
'--delete' => $delete,
48+
'--force' => $force
49+
];
50+
51+
$this->call('command:make-entity', $arguments);
52+
$this->call('command:make-factory', ['table_name' => $_tableName, '--delete' => $delete, '--force' => $force]);
53+
$this->call('command:make-resource', $arguments);
54+
$this->call('command:make-interface-repository', $arguments);
55+
$this->call('command:make-mysql-repository', $arguments);
56+
$this->call('command:make-redis-repository', $arguments);
57+
$this->call('command:make-repository', ['table_name' => $_tableName, '--delete' => $delete, '--force' => $force]);
58+
}
59+
}
60+
}

Commands/MakeEntity.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Traits\CustomMySqlQueries;
6+
use Illuminate\Console\Command;
7+
8+
class MakeEntity extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'command:make-entity {table_name} {--k|foreign-keys : Detect foreign keys} {--d|delete : Delete resource} {--f|force : Override/Delete existing mysql repository}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new entity.';
23+
24+
/**
25+
* Create a new command instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
}
33+
34+
use CustomMySqlQueries;
35+
36+
/**
37+
* Generate a getter for given attribute.
38+
* @param string $attributeName
39+
* @param string $attributeType
40+
* @return string
41+
*/
42+
private function writeGetter(string $attributeName, string $attributeType): string
43+
{
44+
return "\n\t/**\n\t * @return $attributeType\n\t */\n\t" .
45+
"public function get" . ucfirst($attributeName) . "(): $attributeType\n\t" .
46+
"{\n\t\treturn \$this->$attributeName;\n\t}\n";
47+
}
48+
49+
/**
50+
* Generate a setter for given attribute.
51+
* @param string $attributeName
52+
* @param string $attributeType
53+
* @return string
54+
*/
55+
private function writeSetter(string $attributeName, string $attributeType): string
56+
{
57+
return "\n\t/**\n\t * @param $attributeType \$$attributeName\n\t */\n\t" .
58+
"public function set" . ucfirst($attributeName) . "($attributeType \$$attributeName): void\n\t" .
59+
"{\n\t\t\$this->$attributeName = \$$attributeName;\n\t}\n";
60+
}
61+
62+
/**
63+
* Execute the console command.
64+
*
65+
* @return int
66+
*/
67+
public function handle(): int
68+
{
69+
$tableName = $this->argument('table_name');
70+
$detectForeignKeys = $this->option('foreign-keys');
71+
$entityName = str_singular(ucfirst(camel_case($tableName)));
72+
$relativeEntitiesPath = config('repository.path.relative.entities');
73+
74+
if ($this->option('delete')) {
75+
unlink("$relativeEntitiesPath/$entityName.php");
76+
$this->info("Entity \"$entityName\" has been deleted.");
77+
return 0;
78+
}
79+
80+
if (!file_exists($relativeEntitiesPath)) {
81+
mkdir($relativeEntitiesPath, 775, true);
82+
}
83+
84+
if (class_exists("$relativeEntitiesPath\\$entityName") && !$this->option('force')) {
85+
$this->alert("Entity $entityName is already exist!");
86+
die;
87+
}
88+
89+
$columns = $this->getAllColumnsInTable($tableName);
90+
91+
if ($columns->isEmpty()) {
92+
$this->alert("Couldn't retrieve columns from table " . $tableName . "! Perhaps table's name is misspelled.");
93+
die;
94+
}
95+
96+
if ($detectForeignKeys) {
97+
$foreignKeys = $this->extractForeignKeys($tableName);
98+
}
99+
100+
foreach ($columns as $_column) {
101+
$_column->COLUMN_NAME = camel_case($_column->COLUMN_NAME);
102+
}
103+
104+
// Initialize Class
105+
$entityContent = "<?php\n\nnamespace $relativeEntitiesPath;\n\n";
106+
$entityContent .= "class $entityName extends Entity\n{\n";
107+
108+
// Create Attributes
109+
foreach ($columns as $_column) {
110+
$entityContent .= "\tprotected \$$_column->COLUMN_NAME;\n";
111+
}
112+
// Create Additional Attributes from Foreign Keys
113+
if ($detectForeignKeys) {
114+
foreach ($foreignKeys as $_foreignKey) {
115+
$entityContent .= "\n\tprotected \$" . $_foreignKey->VARIABLE_NAME . ";";
116+
}
117+
$entityContent .= "\n";
118+
}
119+
120+
// Create Setters and Getters
121+
foreach ($columns as $_column) {
122+
$dataType = $this->dataTypes[$_column->DATA_TYPE];
123+
$entityContent .= $this->writeGetter($_column->COLUMN_NAME, $dataType);
124+
$entityContent .= $this->writeSetter($_column->COLUMN_NAME, $dataType);
125+
}
126+
// Create Additional Setters and Getters from Foreign keys
127+
if ($detectForeignKeys) {
128+
foreach ($foreignKeys as $_foreignKey) {
129+
$entityContent .= $this->writeGetter($_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE);
130+
$entityContent .= $this->writeSetter($_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE);
131+
}
132+
}
133+
$entityContent .= "}";
134+
135+
file_put_contents("$relativeEntitiesPath/$entityName.php", $entityContent);
136+
137+
shell_exec("git add $relativeEntitiesPath/$entityName.php");
138+
139+
$this->info("Entity \"$entityName\" has been created.");
140+
141+
return 0;
142+
}
143+
}

Commands/MakeFactory.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Traits\CustomMySqlQueries;
6+
use Illuminate\Console\Command;
7+
8+
class MakeFactory extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'command:make-factory {table_name} {--d|delete : Delete resource} {--f|force : Override/Delete existing factory class}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new factory.';
23+
24+
/**
25+
* Create a new command instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
}
33+
34+
use CustomMySqlQueries;
35+
36+
/**
37+
* Execute the console command.
38+
*
39+
* @return int
40+
*/
41+
public function handle(): int
42+
{
43+
$tableName = $this->argument('table_name');
44+
$entityName = str_singular(ucfirst(camel_case($tableName)));
45+
$entityVariableName = camel_case($entityName);
46+
$factoryName = $entityName . "Factory";
47+
$relativeFactoriesPath = config('repository.path.relative.factories');
48+
49+
if ($this->option('delete')) {
50+
unlink("$relativeFactoriesPath/$factoryName.php");
51+
$this->info("Factory \"$factoryName\" has been deleted.");
52+
return 0;
53+
}
54+
55+
if (!file_exists($relativeFactoriesPath)) {
56+
mkdir($relativeFactoriesPath, 775, true);
57+
}
58+
59+
if (class_exists("$relativeFactoriesPath\\$factoryName") && !$this->option('force')) {
60+
$this->alert("Factory $factoryName is already exist!");
61+
die;
62+
}
63+
64+
$columns = $this->getAllColumnsInTable($tableName);
65+
66+
if ($columns->isEmpty()) {
67+
$this->alert("Couldn't retrieve columns from table " . $tableName . "! Perhaps table's name is misspelled.");
68+
die;
69+
}
70+
71+
// Initialize Class
72+
$factoryContent = "<?php\n\nnamespace $relativeFactoriesPath;\n\n";
73+
$factoryContent .= "use App\Models\Entities\\$entityName;\nuse stdClass;\n\n";
74+
$factoryContent .= "class $factoryName extends Factory\n{\n";
75+
76+
// Create "makeEntityFromStdClass" Function
77+
$factoryContent .= "\t/**\n\t * @param stdClass \$entity\n\t * @return $entityName\n\t */\n";
78+
$factoryContent .= "\tpublic function makeEntityFromStdClass(stdClass \$entity): $entityName\n\t{\n";
79+
$factoryContent .= "\t\t\$$entityVariableName = new $entityName();\n";
80+
foreach ($columns as $_column) {
81+
$factoryContent .= "\n\t\t\$" . $entityVariableName . "->set" . ucfirst(camel_case($_column->COLUMN_NAME)) . "(\$entity->" . snake_case($_column->COLUMN_NAME) . " ?? null);";
82+
}
83+
$factoryContent .= "\n\n\t\treturn \$$entityVariableName;\n\t}\n}";
84+
85+
file_put_contents("$relativeFactoriesPath/$factoryName.php", $factoryContent);
86+
87+
shell_exec("git add $relativeFactoriesPath/$factoryName.php");
88+
89+
$this->info("Factory \"$factoryName\" has been created.");
90+
91+
return 0;
92+
}
93+
}

0 commit comments

Comments
 (0)