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:facade')] |
||||
| 11 | class MakeFacadeCommand extends GeneratorCommand |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * The console command name. |
||||
| 15 | * |
||||
| 16 | * @var string |
||||
| 17 | */ |
||||
| 18 | protected $name = 'make:facade'; |
||||
| 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:facade'; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The console command description. |
||||
| 33 | * |
||||
| 34 | * @var string |
||||
| 35 | */ |
||||
| 36 | protected $description = 'Create a new facade class'; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * The type of class being generated. |
||||
| 40 | * |
||||
| 41 | * @var string |
||||
| 42 | */ |
||||
| 43 | protected $type = 'Facade'; |
||||
| 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 | $contract = $this->option('contract'); |
||||
| 54 | $accessor = explode('\\', strtolower($name)); |
||||
| 55 | $accessor = array_pop($accessor); |
||||
| 56 | |||||
| 57 | if (! Str::startsWith($contract, [ |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 58 | $this->laravel->getNamespace(), |
||||
| 59 | 'Illuminate', |
||||
| 60 | '\\', |
||||
| 61 | ])) { |
||||
| 62 | $contract = $this->laravel->getNamespace().'Contracts\\'.str_replace('/', '\\', $contract); |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | $stub = str_replace( |
||||
| 66 | ['DummyContract', '{{ contract }}'], class_basename($contract), parent::buildClass($name) |
||||
|
0 ignored issues
–
show
It seems like
$contract 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...
|
|||||
| 67 | ); |
||||
| 68 | |||||
| 69 | $stub = str_replace( |
||||
| 70 | ['DummyFullAccessor', '{{ accessor }}'], $accessor, $stub |
||||
| 71 | ); |
||||
| 72 | |||||
| 73 | return str_replace( |
||||
| 74 | ['DummyFullContract', '{{ contractNamespace }}'], trim($contract, '\\'), $stub |
||||
| 75 | ); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Determine if the class already exists. |
||||
| 80 | * |
||||
| 81 | * @param string $rawName |
||||
| 82 | * @return bool |
||||
| 83 | */ |
||||
| 84 | protected function alreadyExists($rawName) |
||||
| 85 | { |
||||
| 86 | return class_exists($rawName) || |
||||
| 87 | $this->files->exists($this->getPath($this->qualifyClass($rawName))); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | /** |
||||
| 91 | * Get the stub file for the generator. |
||||
| 92 | * |
||||
| 93 | * @return string |
||||
| 94 | */ |
||||
| 95 | protected function getStub() |
||||
| 96 | { |
||||
| 97 | if ($this->option('contract')) { |
||||
| 98 | return __DIR__.'/stubs/facade.stub'; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | return __DIR__.'/stubs/facade-duck.stub'; |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Resolve the fully-qualified path to the stub. |
||||
| 106 | * |
||||
| 107 | * @param string $stub |
||||
| 108 | * @return string |
||||
| 109 | */ |
||||
| 110 | protected function resolveStubPath($stub) |
||||
| 111 | { |
||||
| 112 | return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
||||
| 113 | ? $customPath |
||||
| 114 | : __DIR__.$stub; |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * Get the default namespace for the class. |
||||
| 119 | * |
||||
| 120 | * @param string $rootNamespace |
||||
| 121 | * @return string |
||||
| 122 | */ |
||||
| 123 | protected function getDefaultNamespace($rootNamespace) |
||||
| 124 | { |
||||
| 125 | return $rootNamespace.'\Facades'; |
||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Get the console command options. |
||||
| 130 | * |
||||
| 131 | * @return array |
||||
| 132 | */ |
||||
| 133 | protected function getOptions() |
||||
| 134 | { |
||||
| 135 | return [ |
||||
| 136 | ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the facade already exists'], |
||||
| 137 | ['contract', 'c', InputOption::VALUE_OPTIONAL, 'Create a contract namespace for this facade'], |
||||
| 138 | ]; |
||||
| 139 | } |
||||
| 140 | } |
||||
| 141 |