|
12 | 12 | namespace Barryvdh\LaravelIdeHelper\Console;
|
13 | 13 |
|
14 | 14 | use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
|
| 15 | +use Barryvdh\LaravelIdeHelper\Generator; |
15 | 16 | use Barryvdh\LaravelIdeHelper\Parsers\PhpDocReturnTypeParser;
|
16 | 17 | use Barryvdh\Reflection\DocBlock;
|
17 | 18 | use Barryvdh\Reflection\DocBlock\Context;
|
18 | 19 | use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
|
19 | 20 | use Barryvdh\Reflection\DocBlock\Tag;
|
20 | 21 | use Composer\ClassMapGenerator\ClassMapGenerator;
|
21 | 22 | use Illuminate\Console\Command;
|
| 23 | +use Illuminate\Contracts\Config\Repository; |
22 | 24 | use Illuminate\Contracts\Database\Eloquent\Castable;
|
23 | 25 | use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
24 | 26 | use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
|
|
46 | 48 | use Illuminate\Support\Arr;
|
47 | 49 | use Illuminate\Support\Collection;
|
48 | 50 | use Illuminate\Support\Str;
|
| 51 | +use Illuminate\View\Factory as ViewFactory; |
49 | 52 | use phpDocumentor\Reflection\Types\ContextFactory;
|
50 | 53 | use ReflectionClass;
|
51 | 54 | use ReflectionNamedType;
|
@@ -82,6 +85,14 @@ class ModelsCommand extends Command
|
82 | 85 | */
|
83 | 86 | protected $files;
|
84 | 87 |
|
| 88 | + /** |
| 89 | + * @var Repository |
| 90 | + */ |
| 91 | + protected $config; |
| 92 | + |
| 93 | + /** @var ViewFactory */ |
| 94 | + protected $view; |
| 95 | + |
85 | 96 | /**
|
86 | 97 | * The console command name.
|
87 | 98 | *
|
@@ -131,10 +142,12 @@ class ModelsCommand extends Command
|
131 | 142 | /**
|
132 | 143 | * @param Filesystem $files
|
133 | 144 | */
|
134 |
| - public function __construct(Filesystem $files) |
| 145 | + public function __construct(Filesystem $files, Repository $config, ViewFactory $view) |
135 | 146 | {
|
136 |
| - parent::__construct(); |
| 147 | + $this->config = $config; |
137 | 148 | $this->files = $files;
|
| 149 | + $this->view = $view; |
| 150 | + parent::__construct(); |
138 | 151 | }
|
139 | 152 |
|
140 | 153 | /**
|
@@ -187,6 +200,27 @@ public function handle()
|
187 | 200 | $this->error("Failed to write model information to $filename");
|
188 | 201 | }
|
189 | 202 | }
|
| 203 | + |
| 204 | + $helperFilename = $this->config->get('ide-helper.filename'); |
| 205 | + $writeHelper = $this->option('write-eloquent-helper'); |
| 206 | + |
| 207 | + if (!$writeHelper && !$this->files->exists($helperFilename) && ($this->write || $this->write_mixin)) { |
| 208 | + if ($this->confirm("{$helperFilename} does not exist. |
| 209 | + Do you want to generate a minimal helper to generate the Eloquent methods?")) { |
| 210 | + $writeHelper = true; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + if ($writeHelper) { |
| 215 | + $generator = new Generator($this->config, $this->view, $this->getOutput()); |
| 216 | + $content = $generator->generateEloquent(); |
| 217 | + $written = $this->files->put($helperFilename, $content); |
| 218 | + if ($written !== false) { |
| 219 | + $this->info("Eloquent helper was written to $helperFilename"); |
| 220 | + } else { |
| 221 | + $this->error("Failed to write eloquent helper to $helperFilename"); |
| 222 | + } |
| 223 | + } |
190 | 224 | }
|
191 | 225 |
|
192 | 226 |
|
@@ -217,6 +251,9 @@ protected function getOptions()
|
217 | 251 | ['write-mixin', 'M', InputOption::VALUE_NONE,
|
218 | 252 | "Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings",
|
219 | 253 | ],
|
| 254 | + ['write-eloquent-helper', 'E', InputOption::VALUE_NONE, |
| 255 | + 'Write Eloquent helper file to _ide_helper.php', |
| 256 | + ], |
220 | 257 | ['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'],
|
221 | 258 | ['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'],
|
222 | 259 | ['smart-reset', 'r', InputOption::VALUE_NONE, 'Retained for compatibility, while it no longer has any effect'],
|
|
0 commit comments