Skip to content

Commit 292ac40

Browse files
authored
Merge pull request #11 from sama20/master
Complate all current issues
2 parents d0c6be3 + c3674e9 commit 292ac40

Some content is hidden

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

46 files changed

+1125
-842
lines changed

Readme.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ composer require nanvaie/database-repository --dev
77
```
88

99
### Setup Laravel Repository
10-
Before publishing assets, add `REPOSITORY_PHP_VERSION` variable to `.env` and set it to your version of choosing. Supported values are: 8.0, 7.4 (Default is 8.0).
11-
1210
Then run following command in console to publish necessary assets in your project directory.
1311
```bash
1412
php artisan vendor:publish vendor:publish --tag=database-repository-config
@@ -30,17 +28,18 @@ Note: Make sure to run `composer dump-autoload` after these changes.
3028
## Usage
3129
List of artisan commands:
3230

33-
| Command | Inputs | Options | Description |
34-
|----------------------------------------|-------------------------------------|--------------------|-----------------------------------|
35-
| `repository:make-entity` | table_name | -f, -d, -k, -g | Create new Entity |
36-
| `repository:make-enum` | table_name | -f, -d, -g | Create new Enum |
37-
| `repository:make-factory` | table_name | -f, -d, -g | Create new Factory |
38-
| `repository:make-resource` | table_name | -f, -d, -k, -g | Create new Resource |
39-
| `repository:make-interface-repository` | table_name | -f, -d, -k, -g | Create new Repository Interface |
40-
| `repository:make-repository` | table_name | -f, -d, -k, -g | Create new Base Repository |
41-
| `repository:make-mysql-repository` | table_name | -f, -d, -k, -g | Create new MySql Repository class |
42-
| `repository:make-redis-repository` | table_name | -f, -d, -k, -g | Create new Redis Repository class |
43-
| `repository:make-all` | --table_names=table_names(optional) | -a, -f, -d, -k, -g | Run all of the above commands |
31+
| Command | Inputs | Options | Description |
32+
|----------------------------------------|---------------------------------------------------------------------------|--------------------|-----------------------------------|
33+
| `repository:make-entity` | table_name | -f, -d, -k, -g | Create new Entity |
34+
| `repository:make-enum` | table_name | -f, -d, -g | Create new Enum |
35+
| `repository:make-factory` | table_name | -f, -d, -g | Create new Factory |
36+
| `repository:make-resource` | table_name | -f, -d, -k, -g | Create new Resource |
37+
| `repository:make-interface-repository` | table_name | -f, -d, -k, -g | Create new Repository Interface |
38+
| `repository:make-repository` | table_name, selected_db(optional) | -f, -d, -k, -g | Create new Base Repository |
39+
| `repository:make-mysql-repository` | table_name | -f, -d, -k, -g | Create new MySql Repository class |
40+
| `repository:make-redis-repository` | table_name | -f, -d, -k, -g | Create new Redis Repository class |
41+
| `repository:make-all` | --table_names=table_names(optional) <br/>--selected_db=database(optional) | -a, -f, -d, -k, -g | Run all of the above commands |
42+
4443

4544
### Options Explanation
4645
- `-f|--force`: Force commands to override existing files.
@@ -49,6 +48,7 @@ List of artisan commands:
4948
- `-g|--add-to-git`: Add created files to git repository.
5049
- `-a|--all-tables`: Use all existing tables.
5150
- `--table_names=`: Add table names, separate names with comma.
51+
- `--selected_db=` : Use between `Mysql`,`Redis`, If it does not send, the value will return from `config/repository.php`
5252

5353
Example 1. Create new Entity for a table named 'users'.
5454
```bash

config/repository.php

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

55
'php_version' => env('REPOSITORY_PHP_VERSION', '8.0'),
66

7+
'default_db' => 'MySql',# Options: ( Redis, MySql )
8+
79
'path' => [
810
'namespace' => [
911
'entities' => 'App\Models\Entities',

src/Commands/BaseCommand.php

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,120 @@
11
<?php
22

33
namespace Nanvaie\DatabaseRepository\Commands;
4+
45
use Illuminate\Support\Collection;
56
use Illuminate\Console\Command;
7+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
8+
use Illuminate\Support\Str;
9+
610
class BaseCommand extends Command
711
{
12+
// use CustomMySqlQueries;
13+
public string $selectedDb;
14+
public string $tableName;
15+
public string $detectForeignKeys;
16+
public string $entityName;
17+
public string $entityNamespace;
18+
public string $relativeEntitiesPath;
19+
public string $entityStubsPath;
20+
public string $enumNamespace;
21+
public string $relativeEnumsPath;
22+
public string $enumStubPath;
23+
24+
public string $entityVariableName;
25+
public string $factoryName;
26+
public string $factoryNamespace;
27+
public string $relativeFactoriesPath;
28+
public string $factoryStubsPath;
29+
public string $interfaceName;
30+
public string $repositoryNamespace;
31+
public string $relativeInterfacePath;
32+
public string $interfaceRepositoryStubsPath;
33+
34+
public string $mysqlRepositoryName;
35+
public string $relativeMysqlRepositoryPath;
36+
public string $mysqlRepositoryStubsPath;
37+
38+
public function setArguments()
39+
{
40+
$this->selectedDb = $this->hasArgument('selected_db') && $this->argument('selected_db') ? $this->argument('selected_db') : config('repository.default_db');
41+
$this->tableName = $this->argument('table_name');
42+
if ($this->hasOption('foreign-keys')) $this->detectForeignKeys = $this->option('foreign-keys');
43+
$this->entityName = Str::singular(ucfirst(Str::camel($this->tableName)));
44+
$this->entityNamespace = config('repository.path.namespace.entities');
45+
$this->relativeEntitiesPath = config('repository.path.relative.entities');
46+
$this->entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
47+
48+
$this->enumNamespace = config('repository.path.namespace.enums');
49+
$this->relativeEnumsPath = config('repository.path.relative.enums');
50+
$this->enumStubPath = __DIR__ . '/../../' . config('repository.path.stub.enums');
51+
52+
$this->entityVariableName = Str::camel($this->entityName);
53+
$this->factoryName = $this->entityName . 'Factory';
54+
$this->factoryNamespace = config('repository.path.namespace.factories');
55+
$this->relativeFactoriesPath = config('repository.path.relative.factories');
56+
$this->factoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.factories');
57+
58+
$this->interfaceName = "I$this->entityName" . "Repository";
59+
$this->repositoryNamespace = config('repository.path.namespace.repositories');
60+
$this->relativeInterfacePath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
61+
$this->interfaceRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.interface');
62+
63+
$this->mysqlRepositoryName = 'MySql' . $this->entityName . 'Repository';
64+
$this->relativeMysqlRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
65+
$this->mysqlRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.mysql');
866

9-
public function checkDelete(string $filenameWithPath,string $entityName){
67+
}
68+
69+
public function checkDelete(string $filenameWithPath, string $entityName, string $objectName): void
70+
{
1071
if (file_exists($filenameWithPath) && $this->option('delete')) {
1172
unlink($filenameWithPath);
12-
$this->info("Entity \"$entityName\" has been deleted.");
13-
return 0;
73+
$this->info("$objectName '$entityName' has been deleted.");
1474
}
1575
}
16-
public function checkDirectory(string $relativeEntitiesPath,string $entityName){
17-
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
76+
77+
public function checkDirectory(string $relativeEntitiesPath): void
78+
{
79+
if (!file_exists($relativeEntitiesPath) && !mkdir($relativeEntitiesPath, 0775, true) && !is_dir($relativeEntitiesPath)) {
1880
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
19-
return 0;
81+
exit;
2082
}
2183
}
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;
84+
85+
public function checkClassExist(string $nameSpace, string $entityName, string $objectName): void
86+
{
87+
if (class_exists($nameSpace . '\\' . $entityName) && !$this->option('force')) {
88+
$this->alert("$objectName \"$entityName\" is already exist!");
89+
exit;
2690
}
2791
}
2892

29-
public function finalized(string $filenameWithPath,string $entityName, string $baseContent){
93+
public function finalized(string $filenameWithPath, string $entityName, string $baseContent): void
94+
{
3095
file_put_contents($filenameWithPath, $baseContent);
3196
if ($this->option('add-to-git')) {
32-
shell_exec('git add '.$filenameWithPath);
97+
shell_exec('git add ' . $filenameWithPath);
3398
}
3499

35-
$this->info("Entity \"$entityName\" has been created.");
100+
$this->info("\"$entityName\" has been created.");
36101
}
37102

38-
public function checkEmpty(Collection $columns,string $tableName){
103+
public function checkEmpty(Collection $columns, string $tableName): void
104+
{
39105
if ($columns->isEmpty()) {
40106
$this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
41-
die;
107+
exit;
42108
}
43109
}
110+
111+
public function setChoice($choice): void
112+
{
113+
\config(['replacement.choice' => $choice]);
114+
}
115+
116+
public function getChoice(): null|string
117+
{
118+
return \config('replacement.choice');
119+
}
44120
}

src/Commands/MakeAll.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MakeAll extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'repository:make-all
19+
{--selected_db= : Main database}
1920
{--table_names= : Table names, separate names with comma}
2021
{--k|foreign-keys : Detect foreign keys}
2122
{--d|delete : Delete resource}
@@ -35,6 +36,7 @@ class MakeAll extends Command
3536
*/
3637
public function handle()
3738
{
39+
$this->selectedDb = $this->hasOption('selected_db') && $this->option('selected_db') ? $this->option('selected_db') : config('repository.default_db');
3840
$force = $this->option('force');
3941
$delete = $this->option('delete');
4042
$detectForeignKeys = $this->option('foreign-keys');
@@ -65,7 +67,7 @@ public function handle()
6567
$this->call('repository:make-interface-repository', $arguments);
6668
$this->call('repository:make-mysql-repository', $arguments);
6769
$this->call('repository:make-redis-repository', $arguments);
68-
$this->call('repository:make-repository', $arguments);
70+
$this->call('repository:make-repository', [...$arguments,'selected_db'=>$this->selectedDb]);
6971
}
7072
}
7173
}

src/Commands/MakeEntity.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,36 @@ class MakeEntity extends BaseCommand
3030
*/
3131
protected $description = 'Create a new entity.';
3232

33-
34-
3533
/**
3634
* Execute the console command.
3735
*/
38-
public function handle(): int
36+
public function handle(): void
3937
{
40-
$tableName = $this->argument('table_name');
41-
$detectForeignKeys = $this->option('foreign-keys');
42-
$entityName = Str::singular(ucfirst(Str::camel($tableName)));
43-
$entityNamespace = config('repository.path.namespace.entities');
44-
$relativeEntitiesPath = config('repository.path.relative.entities');
45-
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
46-
$filenameWithPath = $relativeEntitiesPath . $entityName.'.php';
38+
$this->setArguments();
39+
$filenameWithPath = $this->relativeEntitiesPath . $this->entityName.'.php';
4740

48-
$this->checkDelete($filenameWithPath,$entityName);
49-
$this->checkDirectory($relativeEntitiesPath,$entityName);
50-
$this->checkClassExist($relativeEntitiesPath,$entityName);
41+
$this->checkDelete($filenameWithPath,$this->entityName,"Entity");
42+
$this->checkDirectory($this->relativeEntitiesPath);
43+
$this->checkClassExist($this->entityNamespace,$this->entityName,"Entity");
5144

52-
$columns = $this->getAllColumnsInTable($tableName);
53-
$this->checkEmpty($columns,$tableName);
45+
$columns = $this->getAllColumnsInTable($this->tableName);
46+
$this->checkEmpty($columns,$this->tableName);
5447

5548
foreach ($columns as $_column) {
5649
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
5750
}
5851

59-
$baseContent = file_get_contents($entityStubsPath.'class.stub');
60-
$attributeStub = file_get_contents($entityStubsPath.'attribute.stub');
61-
$accessorsStub = file_get_contents($entityStubsPath.'accessors.stub');
62-
63-
$entityCreator = new CreatorEntity($columns,$attributeStub, $detectForeignKeys,$tableName,$entityName,$entityNamespace,$accessorsStub,$baseContent);
52+
$entityCreator = new CreatorEntity($columns,
53+
$this->detectForeignKeys,
54+
$this->tableName,
55+
$this->entityName,
56+
$this->entityNamespace,
57+
$this->entityStubsPath
58+
);
6459
$creator = new BaseCreator($entityCreator);
65-
$baseContent = $creator->createClass();
66-
67-
$this->finalized($filenameWithPath, $entityName, $baseContent);
68-
return 0;
69-
}
60+
$baseContent = $creator->createClass($filenameWithPath,$this);
7061

62+
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
7163

64+
}
7265
}

0 commit comments

Comments
 (0)