|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | namespace Nanvaie\DatabaseRepository\Commands;
|
| 4 | + |
4 | 5 | use Illuminate\Support\Collection;
|
5 | 6 | use Illuminate\Console\Command;
|
| 7 | +use Nanvaie\DatabaseRepository\CustomMySqlQueries; |
| 8 | +use Illuminate\Support\Str; |
| 9 | + |
6 | 10 | class BaseCommand extends Command
|
7 | 11 | {
|
| 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'); |
8 | 66 |
|
9 |
| - public function checkDelete(string $filenameWithPath,string $entityName){ |
| 67 | + } |
| 68 | + |
| 69 | + public function checkDelete(string $filenameWithPath, string $entityName, string $objectName): void |
| 70 | + { |
10 | 71 | if (file_exists($filenameWithPath) && $this->option('delete')) {
|
11 | 72 | unlink($filenameWithPath);
|
12 |
| - $this->info("Entity \"$entityName\" has been deleted."); |
13 |
| - return 0; |
| 73 | + $this->info("$objectName '$entityName' has been deleted."); |
14 | 74 | }
|
15 | 75 | }
|
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)) { |
18 | 80 | $this->alert("Directory \"$relativeEntitiesPath\" was not created");
|
19 |
| - return 0; |
| 81 | + exit; |
20 | 82 | }
|
21 | 83 | }
|
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; |
26 | 90 | }
|
27 | 91 | }
|
28 | 92 |
|
29 |
| - public function finalized(string $filenameWithPath,string $entityName, string $baseContent){ |
| 93 | + public function finalized(string $filenameWithPath, string $entityName, string $baseContent): void |
| 94 | + { |
30 | 95 | file_put_contents($filenameWithPath, $baseContent);
|
31 | 96 | if ($this->option('add-to-git')) {
|
32 |
| - shell_exec('git add '.$filenameWithPath); |
| 97 | + shell_exec('git add ' . $filenameWithPath); |
33 | 98 | }
|
34 | 99 |
|
35 |
| - $this->info("Entity \"$entityName\" has been created."); |
| 100 | + $this->info("\"$entityName\" has been created."); |
36 | 101 | }
|
37 | 102 |
|
38 |
| - public function checkEmpty(Collection $columns,string $tableName){ |
| 103 | + public function checkEmpty(Collection $columns, string $tableName): void |
| 104 | + { |
39 | 105 | if ($columns->isEmpty()) {
|
40 | 106 | $this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled.");
|
41 |
| - die; |
| 107 | + exit; |
42 | 108 | }
|
43 | 109 | }
|
| 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 | + } |
44 | 120 | }
|
0 commit comments