ikechukwukalu /
makeservice
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Ikechukwukalu\Makeservice\Console\Commands; |
||||
| 4 | |||||
| 5 | use Illuminate\Console\GeneratorCommand; |
||||
| 6 | use Illuminate\Support\Str; |
||||
| 7 | use Symfony\Component\Console\Attribute\AsCommand; |
||||
| 8 | use Symfony\Component\Console\Input\InputOption; |
||||
| 9 | |||||
| 10 | #[AsCommand(name: 'make:interfaceclass')] |
||||
| 11 | class MakeInterfaceCommand extends GeneratorCommand |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * The console command name. |
||||
| 15 | * |
||||
| 16 | * @var string |
||||
| 17 | */ |
||||
| 18 | protected $name = 'make:interfaceclass'; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * The name of the console command. |
||||
| 22 | * |
||||
| 23 | * This name is used to identify the command during lazy loading. |
||||
| 24 | * |
||||
| 25 | * @var string|null |
||||
| 26 | * |
||||
| 27 | * @deprecated |
||||
| 28 | */ |
||||
| 29 | protected static $defaultName = 'make:interfaceclass'; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The console command description. |
||||
| 33 | * |
||||
| 34 | * @var string |
||||
| 35 | */ |
||||
| 36 | protected $description = 'Create a new interface class'; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * The type of class being generated. |
||||
| 40 | * |
||||
| 41 | * @var string |
||||
| 42 | */ |
||||
| 43 | protected $type = 'Interface'; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * Build the class with the given name. |
||||
| 47 | * |
||||
| 48 | * @param string $name |
||||
| 49 | * @return string |
||||
| 50 | */ |
||||
| 51 | protected function buildClass($name) |
||||
| 52 | { |
||||
| 53 | $model = $this->option('model'); |
||||
| 54 | |||||
| 55 | if (! Str::startsWith($model, [ |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 56 | $this->laravel->getNamespace(), |
||||
| 57 | 'Illuminate', |
||||
| 58 | '\\', |
||||
| 59 | ])) { |
||||
| 60 | $model = $this->laravel->getNamespace().'Models\\'.str_replace('/', '\\', $model); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | $stub = str_replace( |
||||
| 64 | ['DummyModel', '{{ model }}'], class_basename($model), parent::buildClass($name) |
||||
|
0 ignored issues
–
show
It seems like
$model can also be of type array; however, parameter $class of class_basename() does only seem to accept object|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...
|
|||||
| 65 | ); |
||||
| 66 | |||||
| 67 | return str_replace( |
||||
| 68 | ['DummyFullModel', '{{ modelNamespace }}'], trim($model, '\\'), $stub |
||||
| 69 | ); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * Determine if the class already exists. |
||||
| 74 | * |
||||
| 75 | * @param string $rawName |
||||
| 76 | * @return bool |
||||
| 77 | */ |
||||
| 78 | protected function alreadyExists($rawName) |
||||
| 79 | { |
||||
| 80 | return class_exists($rawName) || |
||||
| 81 | $this->files->exists($this->getPath($this->qualifyClass($rawName))); |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | /** |
||||
| 85 | * Get the stub file for the generator. |
||||
| 86 | * |
||||
| 87 | * @return string |
||||
| 88 | */ |
||||
| 89 | protected function getStub() |
||||
| 90 | { |
||||
| 91 | if ($this->option('model') && $this->option('user')) { |
||||
| 92 | return __DIR__.'/stubs/interface-user.stub'; |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | if ($this->option('model')) { |
||||
| 96 | return __DIR__.'/stubs/interface.stub'; |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | return __DIR__.'/stubs/interface-duck.stub'; |
||||
| 100 | } |
||||
| 101 | |||||
| 102 | /** |
||||
| 103 | * Resolve the fully-qualified path to the stub. |
||||
| 104 | * |
||||
| 105 | * @param string $stub |
||||
| 106 | * @return string |
||||
| 107 | */ |
||||
| 108 | protected function resolveStubPath($stub) |
||||
| 109 | { |
||||
| 110 | return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
||||
| 111 | ? $customPath |
||||
| 112 | : __DIR__.$stub; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * Get the default namespace for the class. |
||||
| 117 | * |
||||
| 118 | * @param string $rootNamespace |
||||
| 119 | * @return string |
||||
| 120 | */ |
||||
| 121 | protected function getDefaultNamespace($rootNamespace) |
||||
| 122 | { |
||||
| 123 | return $rootNamespace.'\Contracts'; |
||||
| 124 | } |
||||
| 125 | |||||
| 126 | /** |
||||
| 127 | * Get the console command options. |
||||
| 128 | * |
||||
| 129 | * @return array |
||||
| 130 | */ |
||||
| 131 | protected function getOptions() |
||||
| 132 | { |
||||
| 133 | return [ |
||||
| 134 | ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the interface already exists'], |
||||
| 135 | ['model', 'm', InputOption::VALUE_REQUIRED, 'Create a model namespace for this interface'], |
||||
| 136 | ['user', 'u', InputOption::VALUE_NONE, 'Create extra fetch by user id functions for this repository'], |
||||
| 137 | ]; |
||||
| 138 | } |
||||
| 139 | } |
||||
| 140 |