|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MrPunyapal\LaravelExtendedCommands\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\GeneratorCommand; |
| 6 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 7 | +use Symfony\Component\Console\Input\InputOption; |
| 8 | + |
| 9 | +#[AsCommand(name: 'make:builder')] |
| 10 | +class BuilderMakeCommand extends GeneratorCommand |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The console command name. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $name = 'make:builder'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Create a new builder class'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The type of class being generated. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $type = 'Builder'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Determine if the class already exists. |
| 35 | + * |
| 36 | + * @param string $rawName |
| 37 | + * @return bool |
| 38 | + */ |
| 39 | + protected function alreadyExists($rawName) |
| 40 | + { |
| 41 | + return class_exists($rawName) || |
| 42 | + $this->files->exists($this->getPath($this->qualifyClass($rawName))); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get the stub file for the generator. |
| 47 | + * |
| 48 | + * @return string |
| 49 | + */ |
| 50 | + protected function getStub() |
| 51 | + { |
| 52 | + return $this->resolveStubPath('/../../stubs/builder.stub'); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Resolve the fully-qualified path to the stub. |
| 57 | + * |
| 58 | + * @param string $stub |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + protected function resolveStubPath($stub) |
| 62 | + { |
| 63 | + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) |
| 64 | + ? $customPath |
| 65 | + : __DIR__.$stub; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the default namespace for the class. |
| 70 | + * |
| 71 | + * @param string $rootNamespace |
| 72 | + * @return string |
| 73 | + */ |
| 74 | + protected function getDefaultNamespace($rootNamespace) |
| 75 | + { |
| 76 | + return $rootNamespace.'\Models\Builders'; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Build the class with the given name. |
| 81 | + * |
| 82 | + * @param string $name |
| 83 | + * @return string |
| 84 | + * |
| 85 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 86 | + */ |
| 87 | + protected function buildClass($name) |
| 88 | + { |
| 89 | + $replace = $this->buildModelReplacements(); |
| 90 | + |
| 91 | + return str_replace( |
| 92 | + array_keys($replace), array_values($replace), parent::buildClass($name) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Build the replacements for a model. |
| 98 | + * |
| 99 | + * @return array<string, string> |
| 100 | + */ |
| 101 | + protected function buildModelReplacements() |
| 102 | + { |
| 103 | + $replacements = []; |
| 104 | + |
| 105 | + if ($this->option('model')) { |
| 106 | + $modelNamespace = $this->option('model'); |
| 107 | + |
| 108 | + $replacements['{{ modelPhpdoc }}'] = <<<EOT |
| 109 | + /** |
| 110 | + * @template TModel of $modelNamespace |
| 111 | + * |
| 112 | + * @extends Builder<$modelNamespace> |
| 113 | + */ |
| 114 | + EOT; |
| 115 | + } else { |
| 116 | + $replacements["{{ modelPhpdoc }}\n"] = ''; |
| 117 | + $replacements["{{ modelPhpdoc }}\r\n"] = ''; |
| 118 | + } |
| 119 | + |
| 120 | + return $replacements; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Get the console command options. |
| 125 | + * |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + protected function getOptions() |
| 129 | + { |
| 130 | + return [ |
| 131 | + ['model', 'm', InputOption::VALUE_REQUIRED, 'The model that the builder applies to'], |
| 132 | + ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the event already exists'], |
| 133 | + ]; |
| 134 | + } |
| 135 | +} |
0 commit comments