Skip to content

Commit d0c6be3

Browse files
authored
Merge pull request #9 from sama20/master
Changes based on issues
2 parents 435252a + 03c776d commit d0c6be3

17 files changed

+250
-155
lines changed

src/Commands/BaseCommand.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Commands;
4+
use Illuminate\Support\Collection;
5+
use Illuminate\Console\Command;
6+
class BaseCommand extends Command
7+
{
8+
9+
public function checkDelete(string $filenameWithPath,string $entityName){
10+
if (file_exists($filenameWithPath) && $this->option('delete')) {
11+
unlink($filenameWithPath);
12+
$this->info("Entity \"$entityName\" has been deleted.");
13+
return 0;
14+
}
15+
}
16+
public function checkDirectory(string $relativeEntitiesPath,string $entityName){
17+
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
18+
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
19+
return 0;
20+
}
21+
}
22+
public function checkClassExist(string $relativeEntitiesPath, string $entityName){
23+
if (class_exists($relativeEntitiesPath.'\\'.$entityName) && ! $this->option('force')) {
24+
$this->alert("Entity \"$entityName\" is already exist!");
25+
return 0;
26+
}
27+
}
28+
29+
public function finalized(string $filenameWithPath,string $entityName, string $baseContent){
30+
file_put_contents($filenameWithPath, $baseContent);
31+
if ($this->option('add-to-git')) {
32+
shell_exec('git add '.$filenameWithPath);
33+
}
34+
35+
$this->info("Entity \"$entityName\" has been created.");
36+
}
37+
38+
public function checkEmpty(Collection $columns,string $tableName){
39+
if ($columns->isEmpty()) {
40+
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
41+
die;
42+
}
43+
}
44+
}

src/Commands/MakeEntity.php

Lines changed: 16 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace Nanvaie\DatabaseRepository\Commands;
44

55
use Illuminate\Support\Str;
6+
use Nanvaie\DatabaseRepository\CreateEntity;
67
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
7-
use Illuminate\Console\Command;
8+
use Nanvaie\DatabaseRepository\Creators\CreatorEntity;
9+
use Nanvaie\DatabaseRepository\Creators\BaseCreator;
10+
use Illuminate\Support\Collection;
811

9-
class MakeEntity extends Command
12+
class MakeEntity extends BaseCommand
1013
{
14+
use CustomMySqlQueries;
1115
/**
1216
* The name and signature of the console command.
1317
*
@@ -26,39 +30,10 @@ class MakeEntity extends Command
2630
*/
2731
protected $description = 'Create a new entity.';
2832

29-
use CustomMySqlQueries;
3033

31-
/**
32-
* @param string $attributeStub
33-
* @param string $attributeName
34-
* @param string $attributeType
35-
* @return string
36-
*/
37-
private function writeAttribute(string $attributeStub, string $attributeName, string $attributeType): string
38-
{
39-
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}'],
40-
[$attributeType, $attributeName],
41-
$attributeStub);
42-
}
43-
44-
/**
45-
* Generate getter and setter for given attribute.
46-
* @param string $accessorStub
47-
* @param string $attributeName
48-
* @param string $attributeType
49-
* @return string
50-
*/
51-
private function writeAccessors(string $accessorStub, string $attributeName, string $attributeType): string
52-
{
53-
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}', '{{ GetterName }}', '{{ SetterName }}'],
54-
[$attributeType, $attributeName, ucfirst($attributeName), ucfirst($attributeName)],
55-
$accessorStub);
56-
}
5734

5835
/**
5936
* Execute the console command.
60-
*
61-
* @return int
6237
*/
6338
public function handle(): int
6439
{
@@ -70,28 +45,12 @@ public function handle(): int
7045
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
7146
$filenameWithPath = $relativeEntitiesPath . $entityName.'.php';
7247

73-
if (file_exists($filenameWithPath) && $this->option('delete')) {
74-
unlink($filenameWithPath);
75-
$this->info("Entity \"$entityName\" has been deleted.");
76-
return 0;
77-
}
78-
79-
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
80-
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
81-
return 0;
82-
}
83-
84-
if (class_exists($relativeEntitiesPath.'\\'.$entityName) && ! $this->option('force')) {
85-
$this->alert("Entity \"$entityName\" is already exist!");
86-
return 0;
87-
}
48+
$this->checkDelete($filenameWithPath,$entityName);
49+
$this->checkDirectory($relativeEntitiesPath,$entityName);
50+
$this->checkClassExist($relativeEntitiesPath,$entityName);
8851

8952
$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-
}
53+
$this->checkEmpty($columns,$tableName);
9554

9655
foreach ($columns as $_column) {
9756
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
@@ -101,60 +60,13 @@ public function handle(): int
10160
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
10261
$accessorsStub = file_get_contents($entityStubsPath.'accessors.stub');
10362

104-
// Create Attributes
105-
$attributes = '';
106-
foreach ($columns as $_column) {
107-
$attributes .= $this->writeAttribute(
108-
$attributeStub,
109-
$_column->COLUMN_NAME,
110-
($_column->IS_NULLABLE === 'YES' ? '?' : '') . $this->dataTypes[$_column->DATA_TYPE]
111-
);
112-
}
113-
114-
// Create Setters and Getters
115-
$settersAndGetters = '';
116-
foreach ($columns as $_column) {
117-
$settersAndGetters .= $this->writeAccessors(
118-
$accessorsStub,
119-
$_column->COLUMN_NAME,
120-
($_column->IS_NULLABLE === 'YES' ? '?' : '') . $this->dataTypes[$_column->DATA_TYPE]
121-
);
122-
}
123-
124-
if ($detectForeignKeys) {
125-
$foreignKeys = $this->extractForeignKeys($tableName);
126-
127-
// Create Additional Attributes from Foreign Keys
128-
foreach ($foreignKeys as $_foreignKey) {
129-
$attributes .= $this->writeAttribute(
130-
$attributeStub,
131-
$_foreignKey->VARIABLE_NAME,
132-
$_foreignKey->ENTITY_DATA_TYPE
133-
);
134-
}
135-
136-
// Create Additional Setters and Getters from Foreign keys
137-
foreach ($foreignKeys as $_foreignKey) {
138-
$settersAndGetters .= $this->writeAccessors(
139-
$accessorsStub,
140-
$_foreignKey->VARIABLE_NAME,
141-
$_foreignKey->ENTITY_DATA_TYPE
142-
);
143-
}
144-
}
145-
146-
$baseContent = str_replace(['{{ EntityNamespace }}', '{{ EntityName }}', '{{ Attributes }}', '{{ SettersAndGetters }}'],
147-
[$entityNamespace, $entityName, $attributes, $settersAndGetters],
148-
$baseContent);
149-
150-
file_put_contents($filenameWithPath, $baseContent);
151-
152-
if ($this->option('add-to-git')) {
153-
shell_exec('git add '.$filenameWithPath);
154-
}
155-
156-
$this->info("Entity \"$entityName\" has been created.");
63+
$entityCreator = new CreatorEntity($columns,$attributeStub, $detectForeignKeys,$tableName,$entityName,$entityNamespace,$accessorsStub,$baseContent);
64+
$creator = new BaseCreator($entityCreator);
65+
$baseContent = $creator->createClass();
15766

67+
$this->finalized($filenameWithPath, $entityName, $baseContent);
15868
return 0;
15969
}
70+
71+
16072
}

src/Commands/MakeRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MakeRepository extends Command
3131
private function writeFunction(string $functionStub, string $functionName, string $columnName, string $attributeType): string
3232
{
3333
if ($functionName === 'getOneBy') {
34-
$functionReturnType = '?{{ EntityName }}';
34+
$functionReturnType = 'null|{{ EntityName }}';
3535
$functionName .= ucfirst(Str::camel($columnName));
3636
$columnName = Str::camel($columnName);
3737
} elseif ($functionName === 'getAllBy') {

src/Creators/BaseCreator.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Creators;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Console\Command;
7+
use function Nanvaie\DatabaseRepository\Commands\config;
8+
9+
class BaseCreator
10+
{
11+
private $creator;
12+
13+
public function __construct(IClassCreator $creator)
14+
{
15+
$this->creator = $creator;
16+
}
17+
18+
public function createClass():string
19+
{
20+
// Create Attributes
21+
$attributesArray = $this->creator->createAttributs(
22+
$this->creator->columns,
23+
$this->creator->attributeStub,
24+
$this->creator->detectForeignKeys,
25+
$this->creator->tableName);
26+
$attributes = implode('',array_column($attributesArray, '1'));
27+
28+
29+
// Create Setters and Getters
30+
$settersAndGettersArray = $this->creator->createFunctions(
31+
$this->creator->columns,
32+
$this->creator->accessorsStub,
33+
$this->creator->detectForeignKeys,
34+
$this->creator->tableName);
35+
$settersAndGetters = implode('',array_column($settersAndGettersArray, '1'));
36+
37+
$this->creator->baseContent = str_replace(['{{ EntityNamespace }}', '{{ EntityName }}', '{{ Attributes }}', '{{ SettersAndGetters }}'],
38+
[
39+
$this->creator->entityNamespace,
40+
$this->creator->entityName,
41+
$attributes,
42+
$settersAndGetters
43+
],
44+
$this->creator->baseContent);
45+
46+
return $this->creator->baseContent;
47+
}
48+
49+
50+
51+
52+
53+
}

src/Creators/CreatorEntity.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Creators;
4+
5+
use Illuminate\Support\Collection;
6+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
7+
8+
class CreatorEntity implements IClassCreator
9+
{
10+
use CustomMySqlQueries;
11+
// protected $columns=
12+
public function __construct(
13+
public Collection $columns,
14+
public string $attributeStub,
15+
public string $detectForeignKeys,
16+
public string $tableName,
17+
public string $entityName,
18+
public string $entityNamespace,
19+
public string $accessorsStub,
20+
public string $baseContent)
21+
{
22+
23+
}
24+
25+
public function createAttributs(Collection $columns, string $attributeStub,string $detectForeignKeys,string $tableName):array{
26+
$attributes = [];
27+
foreach ($columns as $_column) {
28+
$defaultValue = ($_column->COLUMN_DEFAULT ?? 'null') ? ($_column->COLUMN_DEFAULT ?? 'null') : "''";
29+
$attributes[] = [
30+
$_column->COLUMN_NAME,
31+
$this->writeAttribute(
32+
$attributeStub,
33+
$_column->COLUMN_NAME.($_column->IS_NULLABLE === 'YES' ? ' = '.$defaultValue : ''),
34+
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $this->dataTypes[$_column->DATA_TYPE]
35+
)
36+
];
37+
}
38+
39+
if ($detectForeignKeys) {
40+
$foreignKeys = $this->extractForeignKeys($tableName);
41+
42+
// Create Additional Attributes from Foreign Keys
43+
foreach ($foreignKeys as $_foreignKey) {
44+
$attributes[] = [
45+
$_column->COLUMN_NAME,
46+
$this->writeAttribute(
47+
$attributeStub,
48+
$_foreignKey->VARIABLE_NAME,
49+
$_foreignKey->ENTITY_DATA_TYPE
50+
)
51+
];
52+
}
53+
}
54+
55+
return $attributes;
56+
}
57+
58+
59+
public function createFunctions(Collection $columns, bool|string $accessorsStub,$detectForeignKeys,$tableName):array
60+
{
61+
$settersAndGetters = [];
62+
foreach ($columns as $_column) {
63+
$settersAndGetters[] =
64+
[
65+
$_column->COLUMN_NAME,
66+
$this->writeAccessors(
67+
$accessorsStub,
68+
$_column->COLUMN_NAME,
69+
($_column->IS_NULLABLE === 'YES' ? 'null|' : '') . $this->dataTypes[$_column->DATA_TYPE]
70+
)
71+
];
72+
}
73+
if ($detectForeignKeys) {
74+
$foreignKeys = $this->extractForeignKeys($tableName);
75+
76+
// Create Additional Setters and Getters from Foreign keys
77+
foreach ($foreignKeys as $_foreignKey) {
78+
$settersAndGetters[] =
79+
[
80+
$_column->COLUMN_NAME,
81+
$this->writeAccessors(
82+
$accessorsStub,
83+
$_foreignKey->VARIABLE_NAME,
84+
$_foreignKey->ENTITY_DATA_TYPE
85+
)
86+
];
87+
}
88+
}
89+
return $settersAndGetters;
90+
}
91+
92+
93+
private function writeAttribute(string $attributeStub, string $attributeName, string $attributeType): string
94+
{
95+
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}'],
96+
[$attributeType, $attributeName],
97+
$attributeStub);
98+
}
99+
100+
/**
101+
* Generate getter and setter for given attribute.
102+
*/
103+
private function writeAccessors(string $accessorStub, string $attributeName, string $attributeType): string
104+
{
105+
return str_replace(['{{ AttributeType }}', '{{ AttributeName }}', '{{ GetterName }}', '{{ SetterName }}'],
106+
[$attributeType, $attributeName, ucfirst($attributeName), ucfirst($attributeName)],
107+
$accessorStub);
108+
}
109+
110+
}

src/Creators/IClassCreator.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Creators;
4+
5+
use Illuminate\Support\Collection;
6+
7+
interface IClassCreator
8+
{
9+
// public function getOneById(int $id): null|User;
10+
//
11+
// public function getAllByIds(array $ids): Collection;
12+
//
13+
public function createAttributs(Collection $columns, string $attributeStub,string $detectForeignKeys,string $tableName): array;
14+
15+
public function createFunctions(Collection $columns, bool|string $accessorsStub,string $detectForeignKeys,string $tableName):array;
16+
17+
18+
}

0 commit comments

Comments
 (0)