Skip to content

Commit ef59839

Browse files
committed
feature: some modification to support only single version of php(8.0)
- Inheritance from parent classes in package - add enum generator
1 parent 8288d2d commit ef59839

File tree

92 files changed

+228
-1246
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+228
-1246
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Before publishing assets, add `REPOSITORY_PHP_VERSION` variable to `.env` and se
1111

1212
Then run following command in console to publish necessary assets in your project directory.
1313
```bash
14-
php artisan vendor:publish --provider=Nanvaie\DatabaseRepository\DatabaseRepositoryServiceProvider
14+
php artisan vendor:publish vendor:publish --tag=database-repository-config
1515
```
1616

1717
### Setup Lumen Repository

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.0|^8.0",
13+
"php": "^8.0",
1414
"ext-pdo": "*",
1515
"ext-json": "*",
1616
"laravel/helpers": "^1.5",
17-
"illuminate/console": "~5.8.0|^6.0|^7.0|^8.0|^9.0",
18-
"illuminate/support": "^5.5|^6.0|^7.0|^8.0|^9.0"
17+
"illuminate/console": "^9.0",
18+
"illuminate/support": "^9.0",
19+
"illuminate/cache": "^9.0",
20+
"illuminate/database": "^9.0"
1921
},
2022
"autoload": {
2123
"psr-4": {

config/repository.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
],
1515

1616
'stub' => [
17-
'entities' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.entity.',
18-
'factories' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.factory.',
19-
'resources' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.resource.',
17+
'entities' => 'stubs/Entities/entity.',
18+
'enums' => 'stubs/Enums/enum.',
19+
'factories' => 'stubs/Factories/factory.',
20+
'resources' => 'stubs/Resources/resource.',
2021
'repositories' => [
21-
'base' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.base.',
22-
'mysql' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.mysql.',
23-
'interface' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.interface.',
22+
'base' => 'stubs/Repositories/Base/base.',
23+
'mysql' => 'stubs/Repositories/Mysql/mysql.',
24+
'interface' => 'stubs/Repositories/Interface/interface.',
2425
]
2526
],
2627

src/Commands/MakeAll.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function handle()
4545
];
4646

4747
$this->call('repository:make-entity', $arguments);
48+
$this->call('repository:make-enum', ['table_name' => $_tableName, '--delete' => $delete, '--force' => $force, '--add-to-git' => $addToGit]);
4849
$this->call('repository:make-factory', ['table_name' => $_tableName, '--delete' => $delete, '--force' => $force, '--add-to-git' => $addToGit]);
4950
$this->call('repository:make-resource', $arguments);
5051
$this->call('repository:make-interface-repository', $arguments);

src/Commands/MakeEntity.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ public function handle(): int
6767
$entityNamespace = config('repository.path.namespace.entities');
6868
$relativeEntitiesPath = config('repository.path.relative.entities');
6969
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
70-
$phpVersion = config('repository.php_version');
71-
$filenameWithPath = $relativeEntitiesPath.$entityName.'.php';
70+
$filenameWithPath = $relativeEntitiesPath . $entityName.'.php';
7271

73-
if ($this->option('delete')) {
72+
if (file_exists($filenameWithPath) && $this->option('delete')) {
7473
unlink($filenameWithPath);
7574
$this->info("Entity \"$entityName\" has been deleted.");
7675
return 0;

src/Commands/MakeEnum.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace Nanvaie\DatabaseRepository\Commands;
4+
5+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
6+
use Illuminate\Console\Command;
7+
8+
class MakeEnum extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'repository:make-enum {table_name}
16+
{--d|delete : Delete resource}
17+
{--f|force : Override/Delete enum}
18+
{--g|add-to-git : Add created file to git repository}';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Create a new enum(s).';
26+
27+
use CustomMySqlQueries;
28+
29+
/**
30+
* @param string $attributeStub
31+
* @param string $attributeName
32+
* @param string $attributeType
33+
* @return string
34+
*/
35+
private function writeAttribute(string $attributeStub, string $attributeName, string $attributeString): string
36+
{
37+
return str_replace(['{{ AttributeName }}', '{{ AttributeString }}'],
38+
[$attributeName, $attributeString],
39+
$attributeStub);
40+
}
41+
42+
/**
43+
* Execute the console command.
44+
*
45+
* @return int
46+
*/
47+
public function handle(): int
48+
{
49+
$tableName = $this->argument('table_name');
50+
$enumNamespace = config('repository.path.namespace.enums');
51+
$relativeEntitiesPath = config('repository.path.relative.enums');
52+
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.enums');
53+
54+
55+
$columns = $this->getAllColumnsInTable($tableName);
56+
57+
if ($columns->isEmpty()) {
58+
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
59+
die;
60+
}
61+
62+
$enums = [];
63+
foreach ($columns as $_column) {
64+
if ($_column->DATA_TYPE == 'enum') {
65+
$enumClassName = studly_case(substr_replace($_column->TABLE_NAME, '', -1) . '_' . $_column->COLUMN_NAME);
66+
$enums[$enumClassName] = explode(',', str_replace(['enum(', '\'', ')'], ['', '', ''], $_column->COLUMN_TYPE));
67+
68+
$filenameWithPath = $relativeEntitiesPath . $enumClassName.'.php';
69+
70+
if (file_exists($filenameWithPath) && $this->option('delete')) {
71+
unlink($filenameWithPath);
72+
$this->info("Enum \"$enumClassName\" has been deleted.");
73+
return 0;
74+
}
75+
}
76+
}
77+
78+
// Create Attributes
79+
80+
$baseContentStub = file_get_contents($entityStubsPath.'class.stub');
81+
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
82+
83+
foreach ($enums as $enumName => $enum) {
84+
$filenameWithPath = $relativeEntitiesPath . $enumName.'.php';
85+
86+
87+
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
88+
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
89+
return 0;
90+
}
91+
92+
if (class_exists($relativeEntitiesPath.'\\'.$enumName) && ! $this->option('force')) {
93+
$this->alert("Enum \"$enumName\" is already exist!");
94+
return 0;
95+
}
96+
97+
$attributes = '';
98+
foreach ($enum as $_enum) {
99+
$attributes .= $this->writeAttribute(
100+
$attributeStub,
101+
strtoupper($_enum),
102+
$_enum
103+
);
104+
}
105+
106+
107+
$baseContent = str_replace(['{{ EnumNamespace }}', '{{ EnumName }}', '{{ Attributes }}',],
108+
[$enumNamespace, $enumName, $attributes,],
109+
$baseContentStub);
110+
111+
file_put_contents($filenameWithPath, $baseContent);
112+
113+
if ($this->option('add-to-git')) {
114+
shell_exec('git add '.$filenameWithPath);
115+
}
116+
117+
$this->info("Enum \"$enumName\" has been created.");
118+
}
119+
120+
121+
122+
return 0;
123+
}
124+
}

src/Commands/MakeFactory.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ public function handle(): int
4848
$factoryNamespace = config('repository.path.namespace.factories');
4949
$relativeFactoriesPath = config('repository.path.relative.factories');
5050
$factoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.factories');
51-
$phpVersion = config('repository.php_version');
52-
$filenameWithPath = $relativeFactoriesPath.$factoryName.'.php';
51+
$filenameWithPath = $relativeFactoriesPath . $factoryName.'.php';
5352

54-
if ($this->option('delete')) {
55-
unlink("$relativeFactoriesPath/$factoryName.php");
53+
if (file_exists($filenameWithPath) && $this->option('delete')) {
54+
unlink($filenameWithPath);
5655
$this->info("Factory \"$factoryName\" has been deleted.");
5756
return 0;
5857
}
@@ -101,4 +100,4 @@ public function handle(): int
101100

102101
return 0;
103102
}
104-
}
103+
}

src/Commands/MakeInterfaceRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ public function handle(): int
5555
$interfaceName = "I$entityName"."Repository";
5656
$entityNamespace = config('repository.path.namespace.entities');
5757
$repositoryNamespace = config('repository.path.namespace.repositories');
58-
$relativeInterfacePath = config('repository.path.relative.repositories') . "$entityName";
58+
$relativeInterfacePath = config('repository.path.relative.repositories') . "$entityName" . DIRECTORY_SEPARATOR;
5959
$interfaceRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.interface');
60-
$filenameWithPath = $relativeInterfacePath . '/' . $interfaceName.'.php';
60+
$filenameWithPath = $relativeInterfacePath . $interfaceName.'.php';
6161

62-
if ($this->option('delete')) {
63-
unlink("$relativeInterfacePath/$interfaceName.php");
62+
if (file_exists($filenameWithPath) && $this->option('delete')) {
63+
unlink($filenameWithPath);
6464
$this->info("Interface \"$interfaceName\" has been deleted.");
6565
return 0;
6666
}

src/Commands/MakeMySqlRepository.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ public function handle(): int
7272
$entityNamespace = config('repository.path.namespace.entities');
7373
$factoryNamespace = config('repository.path.namespace.factories');
7474
$repositoryNamespace = config('repository.path.namespace.repositories');
75-
$relativeMysqlRepositoryPath = config('repository.path.relative.repositories')."$entityName";
75+
$relativeMysqlRepositoryPath = config('repository.path.relative.repositories') . "$entityName" . DIRECTORY_SEPARATOR;
7676
$mysqlRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.mysql');
77-
$phpVersion = config('repository.php_version');
78-
$filenameWithPath = $relativeMysqlRepositoryPath . '/' . $mysqlRepositoryName.'.php';
77+
$filenameWithPath = $relativeMysqlRepositoryPath . $mysqlRepositoryName.'.php';
7978

80-
if ($this->option('delete')) {
81-
unlink("$relativeMysqlRepositoryPath/$mysqlRepositoryName.php");
79+
if (file_exists($filenameWithPath) && $this->option('delete')) {
80+
unlink($filenameWithPath);
8281
$this->info("MySql Repository \"$mysqlRepositoryName\" has been deleted.");
8382
return 0;
8483
}
@@ -171,8 +170,8 @@ public function handle(): int
171170
$baseContent = str_replace('{{ Functions }}',
172171
$functions, $baseContent);
173172

174-
$baseContent = str_replace(['{{ EntityName }}', '{{ EntityNamespace }}', '{{ FactoryName }}', '{{ FactoryNamespace }}', '{{ EntityVariableName }}', '{{ MySqlRepositoryName }}', '{{ RepositoryNamespace }}', '{{ RepositoryInterfaceName }}', '{{ TableName }}', '{{ HasSoftDelete }}'],
175-
[$entityName, $entityNamespace, $factoryName, $factoryNamespace, $entityVariableName, $mysqlRepositoryName, $repositoryNamespace, $interfaceName, $tableName, $hasSoftDelete ? 'true' : 'false'],
173+
$baseContent = str_replace(['{{ EntityName }}', '{{ EntityNamespace }}', '{{ FactoryName }}', '{{ FactoryNamespace }}', '{{ EntityVariableName }}', '{{ MySqlRepositoryName }}', '{{ RepositoryNamespace }}', '{{ RepositoryInterfaceName }}', '{{ TableName }}', '{{ HasSoftDelete }}',],
174+
[$entityName, $entityNamespace, $factoryName, $factoryNamespace, $entityVariableName, $mysqlRepositoryName, $repositoryNamespace, $interfaceName, $tableName, $hasSoftDelete ? 'true' : 'false',],
176175
$baseContent);
177176

178177
file_put_contents($filenameWithPath, $baseContent);

src/Commands/MakeRedisRepository.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ public function handle(): int
3737
$tableName = $this->argument('table_name');
3838
$detectForeignKeys = $this->option('foreign-keys');
3939
$entityName = str_singular(ucfirst(camel_case($tableName)));
40-
$entityVariableName = camel_case($entityName);
41-
$factoryName = $entityName."Factory";
42-
$interfaceName = "I$entityName"."Repository";
4340
$redisRepositoryName = "Redis$entityName"."Repository";
4441
$redisRepositoryNamespace = config('repository.path.namespace.repositories');
45-
$relativeRedisRepositoryPath = config('repository.path.relative.repositories')."$entityName";
42+
$relativeRedisRepositoryPath = config('repository.path.relative.repositories') . "$entityName" . DIRECTORY_SEPARATOR;
43+
$filenameWithPath = $relativeRedisRepositoryPath . $redisRepositoryName . '.php';
4644

47-
if ($this->option('delete')) {
48-
unlink("$relativeRedisRepositoryPath/$redisRepositoryName.php");
45+
if (file_exists($filenameWithPath) && $this->option('delete')) {
46+
unlink($filenameWithPath);
4947
$this->info("Redis Repository \"$redisRepositoryName\" has been deleted.");
5048
return 0;
5149
}
@@ -73,7 +71,7 @@ public function handle(): int
7371

7472
// Initialize Redis Repository
7573
$redisRepositoryContent = "<?php\n\nnamespace $redisRepositoryNamespace\\$entityName;\n\n";
76-
$redisRepositoryContent .= "use Nanvaie\DatabaseRepository\Models\Repository\RedisRepository;\n\n";
74+
$redisRepositoryContent .= "use Nanvaie\DatabaseRepository\Models\Repositories\RedisRepository;\n\n";
7775
$redisRepositoryContent .= "class $redisRepositoryName extends RedisRepository\n{";
7876
$redisRepositoryContent .= "}";
7977

0 commit comments

Comments
 (0)