Skip to content

Commit 347483b

Browse files
barryvdhlaravel-ide-helper
andauthored
Add option for only eloquent (#1636)
* Add option for only eloquent * composer fix-style * Add snapshot * Add option to models command * composer fix-style * Fix namespace --------- Co-authored-by: laravel-ide-helper <[email protected]>
1 parent 14fdb57 commit 347483b

File tree

13 files changed

+305
-45343
lines changed

13 files changed

+305
-45343
lines changed

src/Console/GeneratorCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ public function handle()
113113
}
114114

115115
$generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers);
116-
$content = $generator->generate();
116+
if ($this->option('eloquent')) {
117+
$content = $generator->generateEloquent();
118+
} else {
119+
$content = $generator->generate();
120+
}
121+
117122
$written = $this->files->put($filename, $content);
118123

119124
if ($written !== false) {
@@ -169,6 +174,7 @@ protected function getOptions()
169174
['write_mixins', 'W', InputOption::VALUE_OPTIONAL, 'Write mixins to Laravel Model?', $writeMixins],
170175
['helpers', 'H', InputOption::VALUE_NONE, 'Include the helper files'],
171176
['memory', 'M', InputOption::VALUE_NONE, 'Use sqlite memory driver'],
177+
['eloquent', 'E', InputOption::VALUE_NONE, 'Only write Eloquent methods'],
172178
];
173179
}
174180
}

src/Console/ModelsCommand.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
namespace Barryvdh\LaravelIdeHelper\Console;
1313

1414
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
15+
use Barryvdh\LaravelIdeHelper\Generator;
1516
use Barryvdh\LaravelIdeHelper\Parsers\PhpDocReturnTypeParser;
1617
use Barryvdh\Reflection\DocBlock;
1718
use Barryvdh\Reflection\DocBlock\Context;
1819
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
1920
use Barryvdh\Reflection\DocBlock\Tag;
2021
use Composer\ClassMapGenerator\ClassMapGenerator;
2122
use Illuminate\Console\Command;
23+
use Illuminate\Contracts\Config\Repository;
2224
use Illuminate\Contracts\Database\Eloquent\Castable;
2325
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2426
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
@@ -46,6 +48,7 @@
4648
use Illuminate\Support\Arr;
4749
use Illuminate\Support\Collection;
4850
use Illuminate\Support\Str;
51+
use Illuminate\View\Factory as ViewFactory;
4952
use phpDocumentor\Reflection\Types\ContextFactory;
5053
use ReflectionClass;
5154
use ReflectionNamedType;
@@ -82,6 +85,14 @@ class ModelsCommand extends Command
8285
*/
8386
protected $files;
8487

88+
/**
89+
* @var Repository
90+
*/
91+
protected $config;
92+
93+
/** @var ViewFactory */
94+
protected $view;
95+
8596
/**
8697
* The console command name.
8798
*
@@ -131,10 +142,12 @@ class ModelsCommand extends Command
131142
/**
132143
* @param Filesystem $files
133144
*/
134-
public function __construct(Filesystem $files)
145+
public function __construct(Filesystem $files, Repository $config, ViewFactory $view)
135146
{
136-
parent::__construct();
147+
$this->config = $config;
137148
$this->files = $files;
149+
$this->view = $view;
150+
parent::__construct();
138151
}
139152

140153
/**
@@ -187,6 +200,27 @@ public function handle()
187200
$this->error("Failed to write model information to $filename");
188201
}
189202
}
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+
}
190224
}
191225

192226

@@ -217,6 +251,9 @@ protected function getOptions()
217251
['write-mixin', 'M', InputOption::VALUE_NONE,
218252
"Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings",
219253
],
254+
['write-eloquent-helper', 'E', InputOption::VALUE_NONE,
255+
'Write Eloquent helper file to _ide_helper.php',
256+
],
220257
['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'],
221258
['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'],
222259
['smart-reset', 'r', InputOption::VALUE_NONE, 'Retained for compatibility, while it no longer has any effect'],

src/Generator.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Barryvdh\LaravelIdeHelper;
1313

14+
use Illuminate\Database\Eloquent\Model;
1415
use Illuminate\Foundation\AliasLoader;
1516
use Illuminate\Support\Collection;
1617
use Illuminate\Support\Facades\Facade;
@@ -89,6 +90,33 @@ public function generate()
8990
->render();
9091
}
9192

93+
public function generateEloquent()
94+
{
95+
$name = 'Eloquent';
96+
$facade = Model::class;
97+
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : [];
98+
$alias = new Alias($this->config, $name, $facade, $magicMethods, $this->interfaces);
99+
if ($alias->isValid()) {
100+
//Add extra methods, from other classes (magic static calls)
101+
if (array_key_exists($name, $this->extra)) {
102+
$alias->addClass($this->extra[$name]);
103+
}
104+
105+
$eloquentAliases['__root'] = [$alias];
106+
}
107+
108+
$app = app();
109+
return $this->view->make('helper')
110+
->with('namespaces_by_extends_ns', [])
111+
->with('namespaces_by_alias_ns', $eloquentAliases)
112+
->with('real_time_facades', [])
113+
->with('helpers', '')
114+
->with('version', $app->version())
115+
->with('include_fluent', false)
116+
->with('factories', [])
117+
->render();
118+
}
119+
92120
protected function detectDrivers()
93121
{
94122
$defaultUserModel = config('auth.providers.users.model', config('auth.model', 'App\User'));

src/IdeHelperServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function register()
6565
$configPath = __DIR__ . '/../config/ide-helper.php';
6666
$this->mergeConfigFrom($configPath, 'ide-helper');
6767

68-
$this->app->when([GeneratorCommand::class, MetaCommand::class])
68+
$this->app->when([GeneratorCommand::class, MetaCommand::class, ModelsCommand::class])
6969
->needs(\Illuminate\Contracts\View\Factory::class)
7070
->give(function () {
7171
return $this->createLocalViewFactory();

tests/Console/EloquentCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Barryvdh\LaravelIdeHelper\Tests\Console;
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\EloquentCommand;
66

77
use Barryvdh\LaravelIdeHelper\Console\EloquentCommand;
88
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
@@ -23,7 +23,7 @@ public function testCommand()
2323
}
2424

2525
$actualContent = null;
26-
$mockFilesystem = Mockery::mock(Filesystem::class);
26+
$mockFilesystem = Mockery::mock(Filesystem::class)->makePartial();
2727
$mockFilesystem
2828
->shouldReceive('get')
2929
// We don't care about actual args (filename)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\GenerateEloquentOnly;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\AbstractGeneratorCommand;
9+
10+
class Test extends AbstractGeneratorCommand
11+
{
12+
public function testGenerator(): void
13+
{
14+
$command = $this->app->make(GeneratorCommand::class);
15+
16+
$tester = $this->runCommand($command, [
17+
'--eloquent' => true,
18+
]);
19+
20+
$this->assertSame(0, $tester->getStatusCode());
21+
$this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay());
22+
$this->assertStringNotContainsString('public static function configure($basePath = null)', $this->mockFilesystemOutput);
23+
$this->assertStringContainsString('class Eloquent extends \Illuminate\Database\Eloquent\Model', $this->mockFilesystemOutput);
24+
}
25+
}

0 commit comments

Comments
 (0)