Skip to content

Commit c3674e9

Browse files
committed
Get Database From User
issue:#12
1 parent cc8cc9a commit c3674e9

File tree

12 files changed

+37
-34
lines changed

12 files changed

+37
-34
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class BaseCommand extends Command
1111
{
1212
// use CustomMySqlQueries;
13+
public string $selectedDb;
1314
public string $tableName;
1415
public string $detectForeignKeys;
1516
public string $entityName;
@@ -36,6 +37,7 @@ class BaseCommand extends Command
3637

3738
public function setArguments()
3839
{
40+
$this->selectedDb = $this->hasArgument('selected_db') && $this->argument('selected_db') ? $this->argument('selected_db') : config('repository.default_db');
3941
$this->tableName = $this->argument('table_name');
4042
if ($this->hasOption('foreign-keys')) $this->detectForeignKeys = $this->option('foreign-keys');
4143
$this->entityName = Str::singular(ucfirst(Str::camel($this->tableName)));

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/MakeRepository.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MakeRepository extends BaseCommand
1515
*
1616
* @var string
1717
*/
18-
protected $signature = 'repository:make-repository {table_name}
18+
protected $signature = 'repository:make-repository {table_name} {selected_db?}
1919
{--k|foreign-keys : Detect foreign keys}
2020
{--d|delete : Delete resource}
2121
{--f|force : Override/Delete existing repository class}
@@ -38,9 +38,11 @@ class MakeRepository extends BaseCommand
3838
public function handle(): void
3939
{
4040
$this->setArguments();
41+
// dd($this->selectedDb);
4142
$repositoryName = $this->entityName.'Repository';
42-
$sqlRepositoryName = 'MySql'.$this->entityName.'Repository';
43-
$sqlRepositoryVariable = 'mysqlRepository';
43+
// $sqlRepositoryName = 'MySql'.$this->entityName.'Repository';
44+
$sqlRepositoryName = ucwords($this->selectedDb).$this->entityName.'Repository';
45+
$sqlRepositoryVariable = 'repository';
4446
$relativeRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
4547
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
4648
$filenameWithPath = $relativeRepositoryPath . $repositoryName . '.php';
@@ -64,7 +66,8 @@ public function handle(): void
6466
$this->entityNamespace,
6567
$repositoryName,
6668
$this->interfaceName,
67-
$this->repositoryNamespace
69+
$this->repositoryNamespace,
70+
$this->selectedDb
6871
);
6972
$creator = new BaseCreator($RepoCreator);
7073
$baseContent = $creator->createClass($filenameWithPath,$this);

src/Creators/CreatorRedisRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getClassName(): string
2626
}
2727
public function getExtendSection(): string
2828
{
29-
return "extends RedisRepository";
29+
return "extends RedisRepository implements IUserRepository";
3030
}
3131
public function createAttributs(): array
3232
{

src/Creators/CreatorRepository.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Nanvaie\DatabaseRepository\Creators;
44

5+
use App\Models\Repositories\User\IUserRepository;
56
use Illuminate\Support\Collection;
67
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
78
use Illuminate\Support\Str;
@@ -20,7 +21,8 @@ public function __construct(
2021
public string $entityNamespace,
2122
public string $repositoryName,
2223
public string $interfaceName,
23-
public string $repositoryNamespace
24+
public string $repositoryNamespace,
25+
public string $selectedDb
2426
)
2527
{
2628
}
@@ -82,7 +84,7 @@ public function createAttributs(): array
8284
{
8385
$attributeSqlStub = file_get_contents($this->repositoryStubsPath . 'attribute.sql.stub');
8486
$attributes = [];
85-
$attributes[$this->sqlRepositoryVariable] = $this->writeSqlAttribute($attributeSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName);
87+
$attributes['repository'] = 'private IUserRepository $repository;';
8688
return $attributes;
8789
}
8890

@@ -133,8 +135,6 @@ public function createFunctions(): array
133135

134136
public function getConstruct(string $setterSqlStub, string $constructStub)
135137
{
136-
$setters = '';
137-
$setters = substr_replace($setters,$this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName),-1, 0);
138-
return str_replace("{{ Setters }}", $setters, $constructStub);
138+
return str_replace("{{ Setters }}", $this->writeSqlAttribute($setterSqlStub, $this->sqlRepositoryVariable, $this->sqlRepositoryName), $constructStub);
139139
}
140140
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

22
public function __construct()
33
{
4-
{{ Setters }}
5-
}
4+
{{ Setters }} }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
$this->{{ SqlRepositoryVariable }} = new {{ SqlRepositoryName }}();
1+
$this->{{ SqlRepositoryVariable }} = new {{ SqlRepositoryName }}();

stubs/Repositories/Mysql/mysql.create.stub

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
public function create({{ EntityName }} ${{ EntityVariableName }}): {{ EntityName }}
1+
2+
public function create({{ EntityName }} ${{ EntityVariableName }}): {{ EntityName }}
23
{
34
{{ SetterFunctions }}
45

0 commit comments

Comments
 (0)