|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PPSpaces\Console; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use InvalidArgumentException; |
| 7 | +use Illuminate\Console\GeneratorCommand; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | + |
| 10 | +class RepositoryMakeCommand extends GeneratorCommand |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The console command name. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $name = 'make:repository'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Create a new repository class'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The type of class being generated. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $type = 'Repository'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Get the stub file for the generator. |
| 35 | + * |
| 36 | + * @return string |
| 37 | + */ |
| 38 | + protected function getStub() |
| 39 | + { |
| 40 | + $stub = null; |
| 41 | + |
| 42 | + if ($this->option('model')) { |
| 43 | + $stub = '/stubs/repository.model.stub'; |
| 44 | + } |
| 45 | + |
| 46 | + $stub = $stub ?? '/stubs/repository.plain.stub'; |
| 47 | + |
| 48 | + return __DIR__.$stub; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the default namespace for the class. |
| 53 | + * |
| 54 | + * @param string $rootNamespace |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + protected function getDefaultNamespace($rootNamespace) |
| 58 | + { |
| 59 | + return $rootNamespace.'\Repositories'; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Build the class with the given name. |
| 64 | + * |
| 65 | + * Remove the base controller import if we are already in base namespace. |
| 66 | + * |
| 67 | + * @param string $name |
| 68 | + * @return string |
| 69 | + */ |
| 70 | + protected function buildClass($name) |
| 71 | + { |
| 72 | + $controllerNamespace = $this->getNamespace($name); |
| 73 | + |
| 74 | + $replace = []; |
| 75 | + |
| 76 | + if ($this->option('model')) { |
| 77 | + $replace = $this->buildModelReplacements($replace); |
| 78 | + } |
| 79 | + |
| 80 | + $replace["use {$controllerNamespace}\Controller;\n"] = ''; |
| 81 | + |
| 82 | + return str_replace( |
| 83 | + array_keys($replace), array_values($replace), parent::buildClass($name) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Build the replacements for a parent controller. |
| 89 | + * |
| 90 | + * @return array |
| 91 | + */ |
| 92 | + protected function buildParentReplacements() |
| 93 | + { |
| 94 | + $parentModelClass = $this->parseModel($this->option('parent')); |
| 95 | + |
| 96 | + if (! class_exists($parentModelClass)) { |
| 97 | + if ($this->confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", true)) { |
| 98 | + $this->call('make:model', ['name' => $parentModelClass]); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return [ |
| 103 | + 'ParentDummyFullModelClass' => $parentModelClass, |
| 104 | + 'ParentDummyModelClass' => class_basename($parentModelClass), |
| 105 | + 'ParentDummyModelVariable' => lcfirst(class_basename($parentModelClass)), |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Build the model replacement values. |
| 111 | + * |
| 112 | + * @param array $replace |
| 113 | + * @return array |
| 114 | + */ |
| 115 | + protected function buildModelReplacements(array $replace) |
| 116 | + { |
| 117 | + $modelClass = $this->parseModel($this->option('model')); |
| 118 | + |
| 119 | + if (! class_exists($modelClass)) { |
| 120 | + if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) { |
| 121 | + $this->call('make:model', ['name' => $modelClass]); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return array_merge($replace, [ |
| 126 | + 'DummyFullModelClass' => $modelClass, |
| 127 | + 'DummyModelClass' => class_basename($modelClass), |
| 128 | + 'DummyModelVariable' => lcfirst(class_basename($modelClass)), |
| 129 | + ]); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Get the fully-qualified model class name. |
| 134 | + * |
| 135 | + * @param string $model |
| 136 | + * @return string |
| 137 | + * |
| 138 | + * @throws \InvalidArgumentException |
| 139 | + */ |
| 140 | + protected function parseModel($model) |
| 141 | + { |
| 142 | + if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) { |
| 143 | + throw new InvalidArgumentException('Model name contains invalid characters.'); |
| 144 | + } |
| 145 | + |
| 146 | + $model = trim(str_replace('/', '\\', $model), '\\'); |
| 147 | + |
| 148 | + if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) { |
| 149 | + $model = $rootNamespace.$model; |
| 150 | + } |
| 151 | + |
| 152 | + return $model; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get the console command options. |
| 157 | + * |
| 158 | + * @return array |
| 159 | + */ |
| 160 | + protected function getOptions() |
| 161 | + { |
| 162 | + return [ |
| 163 | + ['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a repository for the given model.'], |
| 164 | + ]; |
| 165 | + } |
| 166 | +} |
0 commit comments