Salah3id /
address-domains
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Salah3id\Domains\Commands; |
||||
| 4 | |||||
| 5 | use Illuminate\Support\Str; |
||||
| 6 | use Salah3id\Domains\Support\Config\GenerateConfigReader; |
||||
| 7 | use Salah3id\Domains\Support\Stub; |
||||
| 8 | use Salah3id\Domains\Traits\DomainCommandTrait; |
||||
| 9 | use Symfony\Component\Console\Input\InputArgument; |
||||
| 10 | |||||
| 11 | class FactoryMakeCommand extends GeneratorCommand |
||||
| 12 | { |
||||
| 13 | use DomainCommandTrait; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * The name of argument name. |
||||
| 17 | * |
||||
| 18 | * @var string |
||||
| 19 | */ |
||||
| 20 | protected $argumentName = 'name'; |
||||
| 21 | |||||
| 22 | /** |
||||
| 23 | * The console command name. |
||||
| 24 | * |
||||
| 25 | * @var string |
||||
| 26 | */ |
||||
| 27 | protected $name = 'domain:make-factory'; |
||||
| 28 | |||||
| 29 | /** |
||||
| 30 | * The console command description. |
||||
| 31 | * |
||||
| 32 | * @var string |
||||
| 33 | */ |
||||
| 34 | protected $description = 'Create a new model factory for the specified domain.'; |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * Get the console command arguments. |
||||
| 38 | * |
||||
| 39 | * @return array |
||||
| 40 | */ |
||||
| 41 | protected function getArguments() |
||||
| 42 | { |
||||
| 43 | return [ |
||||
| 44 | ['name', InputArgument::REQUIRED, 'The name of the model.'], |
||||
| 45 | ['domain', InputArgument::OPTIONAL, 'The name of domain will be used.'], |
||||
| 46 | ]; |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * @return mixed |
||||
| 51 | */ |
||||
| 52 | protected function getTemplateContents() |
||||
| 53 | { |
||||
| 54 | $domain = $this->laravel['domains']->findOrFail($this->getDomainName()); |
||||
| 55 | |||||
| 56 | return (new Stub('/factory.stub', [ |
||||
| 57 | 'NAMESPACE' => $this->getClassNamespace($domain), |
||||
| 58 | 'NAME' => $this->getModelName(), |
||||
| 59 | 'MODEL_NAMESPACE' => $this->getModelNamespace(), |
||||
| 60 | ]))->render(); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | /** |
||||
| 64 | * @return mixed |
||||
| 65 | */ |
||||
| 66 | protected function getDestinationFilePath() |
||||
| 67 | { |
||||
| 68 | $path = $this->laravel['domains']->getDomainPath($this->getDomainName()); |
||||
| 69 | |||||
| 70 | $factoryPath = GenerateConfigReader::read('factory'); |
||||
| 71 | |||||
| 72 | return $path . $factoryPath->getPath() . '/' . $this->getFileName(); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * @return string |
||||
| 77 | */ |
||||
| 78 | private function getFileName() |
||||
| 79 | { |
||||
| 80 | return Str::studly($this->argument('name')) . 'Factory.php'; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 81 | } |
||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * @return mixed|string |
||||
| 85 | */ |
||||
| 86 | private function getModelName() |
||||
| 87 | { |
||||
| 88 | return Str::studly($this->argument('name')); |
||||
|
0 ignored issues
–
show
It seems like
$this->argument('name') can also be of type array; however, parameter $value of Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * Get default namespace. |
||||
| 93 | * |
||||
| 94 | * @return string |
||||
| 95 | */ |
||||
| 96 | public function getDefaultNamespace(): string |
||||
| 97 | { |
||||
| 98 | $domain = $this->laravel['domains']; |
||||
| 99 | |||||
| 100 | return $domain->config('paths.generator.factory.namespace') ?: $domain->config('paths.generator.factory.path'); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * Get model namespace. |
||||
| 105 | * |
||||
| 106 | * @return string |
||||
| 107 | */ |
||||
| 108 | public function getModelNamespace(): string |
||||
| 109 | { |
||||
| 110 | $path = $this->laravel['domains']->config('paths.generator.model.path', 'Entities'); |
||||
| 111 | |||||
| 112 | $path = str_replace('/', '\\', $path); |
||||
| 113 | |||||
| 114 | return $this->laravel['domains']->config('namespace') . '\\' . $this->laravel['domains']->findOrFail($this->getDomainName()) . '\\' . $path; |
||||
| 115 | } |
||||
| 116 | } |
||||
| 117 |