|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AwesIO\Repository\Commands; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use Illuminate\Console\Command; |
| 7 | + |
| 8 | +class RepositoryMakeMainCommand extends Command |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The name and signature of the console command. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $signature = 'repository:generate {modelName} {--scope=}'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Create a new Awes.IO repository'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The type of class being generated. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $type = 'Model'; |
| 30 | + |
| 31 | + protected $baseNamespace; |
| 32 | + |
| 33 | + /** |
| 34 | + * Execute the console command. |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function handle() |
| 39 | + { |
| 40 | + $this->baseNamespace = 'Repositories\\'.Str::plural($this->getNameInput()); |
| 41 | + |
| 42 | + $this->createRepository(); |
| 43 | + |
| 44 | + $this->createScopes(); |
| 45 | + |
| 46 | + if ($scope = $this->option('scope')) { |
| 47 | + $this->createScope($scope); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Create a repository. |
| 53 | + * |
| 54 | + * @return void |
| 55 | + */ |
| 56 | + protected function createRepository() |
| 57 | + { |
| 58 | + $this->call('make:repository', [ |
| 59 | + 'name' => $this->getNamespacedRepository(), |
| 60 | + '--model' => $this->getNamespacedModel(), |
| 61 | + '--scopes' => $this->getNamespacedScopes(), |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a repository. |
| 67 | + * |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + protected function createScopes() |
| 71 | + { |
| 72 | + $this->call('make:repository:scopes', [ |
| 73 | + 'name' => $this->getNamespacedScopes(), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Create a repository. |
| 79 | + * |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + protected function createScope($scope) |
| 83 | + { |
| 84 | + $this->call('make:repository:scope', [ |
| 85 | + 'name' => $this->getNamespacedScope($scope), |
| 86 | + ]); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get the desired class name from the input. |
| 91 | + * |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + protected function getNameInput() |
| 95 | + { |
| 96 | + return last(explode('/', trim($this->argument('modelName')))); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get the root namespace for the class. |
| 101 | + * |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + protected function rootNamespace() |
| 105 | + { |
| 106 | + return $this->laravel->getNamespace(); |
| 107 | + } |
| 108 | + |
| 109 | + private function getNamespacedModel() |
| 110 | + { |
| 111 | + return $this->baseNamespace .'\Models\\' |
| 112 | + . Str::singular($this->getNameInput()); |
| 113 | + } |
| 114 | + |
| 115 | + private function getNamespacedScopes() |
| 116 | + { |
| 117 | + return $this->baseNamespace .'\Scopes\\' |
| 118 | + . Str::plural($this->getNameInput()) . 'Scopes'; |
| 119 | + } |
| 120 | + |
| 121 | + private function getNamespacedScope($scope) |
| 122 | + { |
| 123 | + return $this->baseNamespace .'\Scopes\\' |
| 124 | + . Str::singular($scope) . Str::plural($this->getNameInput()) . 'Scope'; |
| 125 | + } |
| 126 | + |
| 127 | + private function getNamespacedRepository() |
| 128 | + { |
| 129 | + $repository = Str::studly(class_basename($this->getNameInput())); |
| 130 | + |
| 131 | + return $this->baseNamespace .'\\' |
| 132 | + . Str::plural($repository) . 'Repository'; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments