|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TM\Commands\Makes; |
| 4 | + |
| 5 | +use Illuminate\Console\GeneratorCommand; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use Symfony\Component\Console\Input\InputArgument; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | + |
| 10 | +class MakeCollection extends GeneratorCommand |
| 11 | +{ |
| 12 | + protected $name = 'make:collection'; |
| 13 | + |
| 14 | + protected $type = 'Collection'; |
| 15 | + |
| 16 | + protected $description = 'Creates a new collection'; |
| 17 | + |
| 18 | + /** |
| 19 | + * Get the stub file for the generator. |
| 20 | + * |
| 21 | + * @return string |
| 22 | + */ |
| 23 | + protected function getStub() : string |
| 24 | + { |
| 25 | + return __DIR__ . '/stubs/make-collection.stub'; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get the default namespace for the class. |
| 30 | + * |
| 31 | + * @param string $rootNamespace |
| 32 | + * @return string |
| 33 | + */ |
| 34 | + protected function getDefaultNamespace($rootNamespace) |
| 35 | + { |
| 36 | + return $rootNamespace . '\Collections'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Get the console command arguments. |
| 41 | + * |
| 42 | + * @return array |
| 43 | + */ |
| 44 | + protected function getArguments() |
| 45 | + { |
| 46 | + return [ |
| 47 | + ['name', InputArgument::REQUIRED, 'The name of the collection.'], |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get the console command options. |
| 53 | + * |
| 54 | + * @return array |
| 55 | + */ |
| 56 | + protected function getOptions() |
| 57 | + { |
| 58 | + return [ |
| 59 | + new InputOption('model', 'm', InputOption::VALUE_REQUIRED, 'The name of the model to bind to'), |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Execute the console command. |
| 65 | + */ |
| 66 | + public function handle() |
| 67 | + { |
| 68 | + $models_namespace = 'App\\Models'; |
| 69 | + |
| 70 | + $model = $this->option('model'); |
| 71 | + $name = $this->argument('name'); |
| 72 | + $class = "{$models_namespace}\\{$model}"; |
| 73 | + $exists = class_exists($class); |
| 74 | + |
| 75 | + if ($exists) { |
| 76 | + $ds = DIRECTORY_SEPARATOR; |
| 77 | + $file_path = app_path("Models{$ds}{$model}.php"); |
| 78 | + |
| 79 | + $new_collection_exists = strpos(file_get_contents($file_path), 'function newCollection(') !== false; |
| 80 | + |
| 81 | + if ($new_collection_exists) { |
| 82 | + $this->warn(' The newCollection method already exists.'); |
| 83 | + } |
| 84 | + else { |
| 85 | + $search = '}'; |
| 86 | + $stub = file_get_contents(__DIR__ . "{$ds}stubs{$ds}collection-model-bind.stub"); |
| 87 | + $insert = str_replace(['{{ class }}', '{{class}}'], $name, $stub); |
| 88 | + $replace = "\n{$insert}\n{$search}"; |
| 89 | + file_put_contents($file_path, Str::replaceLast($search, $replace, file_get_contents($file_path))); |
| 90 | + |
| 91 | + $search = "namespace {$models_namespace};"; |
| 92 | + $namespace = $this->getDefaultNamespace(trim($this->rootNamespace(), '\\')); |
| 93 | + $insert = "use $namespace\\$name;"; |
| 94 | + $replace = "{$search}\n\n{$insert}"; |
| 95 | + file_put_contents($file_path, Str::replaceLast($search, $replace, file_get_contents($file_path))); |
| 96 | + } |
| 97 | + } |
| 98 | + else { |
| 99 | + $this->error(" model $model not found!"); |
| 100 | + } |
| 101 | + return parent::handle(); |
| 102 | + } |
| 103 | +} |
0 commit comments