Skip to content

Commit

Permalink
Add option for only eloquent (#1636)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
barryvdh and laravel-ide-helper authored Dec 29, 2024
1 parent 14fdb57 commit 347483b
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 45,343 deletions.
8 changes: 7 additions & 1 deletion src/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public function handle()
}

$generator = new Generator($this->config, $this->view, $this->getOutput(), $helpers);
$content = $generator->generate();
if ($this->option('eloquent')) {
$content = $generator->generateEloquent();
} else {
$content = $generator->generate();
}

$written = $this->files->put($filename, $content);

if ($written !== false) {
Expand Down Expand Up @@ -169,6 +174,7 @@ protected function getOptions()
['write_mixins', 'W', InputOption::VALUE_OPTIONAL, 'Write mixins to Laravel Model?', $writeMixins],
['helpers', 'H', InputOption::VALUE_NONE, 'Include the helper files'],
['memory', 'M', InputOption::VALUE_NONE, 'Use sqlite memory driver'],
['eloquent', 'E', InputOption::VALUE_NONE, 'Only write Eloquent methods'],
];
}
}
41 changes: 39 additions & 2 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
namespace Barryvdh\LaravelIdeHelper\Console;

use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
use Barryvdh\LaravelIdeHelper\Generator;
use Barryvdh\LaravelIdeHelper\Parsers\PhpDocReturnTypeParser;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag;
use Composer\ClassMapGenerator\ClassMapGenerator;
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
Expand Down Expand Up @@ -46,6 +48,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\View\Factory as ViewFactory;
use phpDocumentor\Reflection\Types\ContextFactory;
use ReflectionClass;
use ReflectionNamedType;
Expand Down Expand Up @@ -82,6 +85,14 @@ class ModelsCommand extends Command
*/
protected $files;

/**
* @var Repository
*/
protected $config;

/** @var ViewFactory */
protected $view;

/**
* The console command name.
*
Expand Down Expand Up @@ -131,10 +142,12 @@ class ModelsCommand extends Command
/**
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
public function __construct(Filesystem $files, Repository $config, ViewFactory $view)
{
parent::__construct();
$this->config = $config;
$this->files = $files;
$this->view = $view;
parent::__construct();
}

/**
Expand Down Expand Up @@ -187,6 +200,27 @@ public function handle()
$this->error("Failed to write model information to $filename");
}
}

$helperFilename = $this->config->get('ide-helper.filename');
$writeHelper = $this->option('write-eloquent-helper');

if (!$writeHelper && !$this->files->exists($helperFilename) && ($this->write || $this->write_mixin)) {
if ($this->confirm("{$helperFilename} does not exist.
Do you want to generate a minimal helper to generate the Eloquent methods?")) {
$writeHelper = true;
}
}

if ($writeHelper) {
$generator = new Generator($this->config, $this->view, $this->getOutput());
$content = $generator->generateEloquent();
$written = $this->files->put($helperFilename, $content);
if ($written !== false) {
$this->info("Eloquent helper was written to $helperFilename");
} else {
$this->error("Failed to write eloquent helper to $helperFilename");
}
}
}


Expand Down Expand Up @@ -217,6 +251,9 @@ protected function getOptions()
['write-mixin', 'M', InputOption::VALUE_NONE,
"Write models to {$this->filename} and adds @mixin to each model, avoiding IDE duplicate declaration warnings",
],
['write-eloquent-helper', 'E', InputOption::VALUE_NONE,
'Write Eloquent helper file to _ide_helper.php',
],
['nowrite', 'N', InputOption::VALUE_NONE, 'Don\'t write to Model file'],
['reset', 'R', InputOption::VALUE_NONE, 'Remove the original phpdocs instead of appending'],
['smart-reset', 'r', InputOption::VALUE_NONE, 'Retained for compatibility, while it no longer has any effect'],
Expand Down
28 changes: 28 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Barryvdh\LaravelIdeHelper;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Facade;
Expand Down Expand Up @@ -89,6 +90,33 @@ public function generate()
->render();
}

public function generateEloquent()
{
$name = 'Eloquent';
$facade = Model::class;
$magicMethods = array_key_exists($name, $this->magic) ? $this->magic[$name] : [];
$alias = new Alias($this->config, $name, $facade, $magicMethods, $this->interfaces);
if ($alias->isValid()) {
//Add extra methods, from other classes (magic static calls)
if (array_key_exists($name, $this->extra)) {
$alias->addClass($this->extra[$name]);
}

$eloquentAliases['__root'] = [$alias];
}

$app = app();
return $this->view->make('helper')
->with('namespaces_by_extends_ns', [])
->with('namespaces_by_alias_ns', $eloquentAliases)
->with('real_time_facades', [])
->with('helpers', '')
->with('version', $app->version())
->with('include_fluent', false)
->with('factories', [])
->render();
}

protected function detectDrivers()
{
$defaultUserModel = config('auth.providers.users.model', config('auth.model', 'App\User'));
Expand Down
2 changes: 1 addition & 1 deletion src/IdeHelperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function register()
$configPath = __DIR__ . '/../config/ide-helper.php';
$this->mergeConfigFrom($configPath, 'ide-helper');

$this->app->when([GeneratorCommand::class, MetaCommand::class])
$this->app->when([GeneratorCommand::class, MetaCommand::class, ModelsCommand::class])
->needs(\Illuminate\Contracts\View\Factory::class)
->give(function () {
return $this->createLocalViewFactory();
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/EloquentCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console;
namespace Barryvdh\LaravelIdeHelper\Tests\Console\EloquentCommand;

use Barryvdh\LaravelIdeHelper\Console\EloquentCommand;
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
Expand All @@ -23,7 +23,7 @@ public function testCommand()
}

$actualContent = null;
$mockFilesystem = Mockery::mock(Filesystem::class);
$mockFilesystem = Mockery::mock(Filesystem::class)->makePartial();
$mockFilesystem
->shouldReceive('get')
// We don't care about actual args (filename)
Expand Down
25 changes: 25 additions & 0 deletions tests/Console/GeneratorCommand/GenerateEloquentOnly/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\GenerateEloquentOnly;

use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\GeneratorCommand\AbstractGeneratorCommand;

class Test extends AbstractGeneratorCommand
{
public function testGenerator(): void
{
$command = $this->app->make(GeneratorCommand::class);

$tester = $this->runCommand($command, [
'--eloquent' => true,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertStringContainsString('A new helper file was written to _ide_helper.php', $tester->getDisplay());
$this->assertStringNotContainsString('public static function configure($basePath = null)', $this->mockFilesystemOutput);
$this->assertStringContainsString('class Eloquent extends \Illuminate\Database\Eloquent\Model', $this->mockFilesystemOutput);
}
}
Loading

0 comments on commit 347483b

Please sign in to comment.