diff --git a/src/Console/EloquentCommand.php b/src/Console/EloquentCommand.php new file mode 100644 index 0000000..0dd8533 --- /dev/null +++ b/src/Console/EloquentCommand.php @@ -0,0 +1,61 @@ + + * @copyright 2017 Charles A. Peterson / Fruitcake Studio (http://www.fruitcakestudio.nl) + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link https://github.com/barryvdh/laravel-ide-helper + */ + +namespace Barryvdh\LaravelIdeHelper\Console; + +use Barryvdh\LaravelIdeHelper\Eloquent; +use Illuminate\Console\Command; +use Illuminate\Filesystem\Filesystem; + +/** + * A command to add \Eloquent mixin to Eloquent\Model + * + * @author Charles A. Peterson + */ +class EloquentCommand extends Command +{ + /** + * The console command name. + * + * @var string + */ + protected $name = 'ide-helper:eloquent'; + + /** + * @var Filesystem $files + */ + protected $files; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Add \Eloquent helper to \Eloquent\Model'; + + /** + * @param Filesystem $files + */ + public function __construct(Filesystem $files) + { + parent::__construct(); + $this->files = $files; + } + + /** + * Execute the console command. + * + * @return void + */ + public function handle() + { + Eloquent::writeEloquentModelHelper($this, $this->files); + } +} diff --git a/src/Console/GeneratorCommand.php b/src/Console/GeneratorCommand.php index be753e0..02cce24 100644 --- a/src/Console/GeneratorCommand.php +++ b/src/Console/GeneratorCommand.php @@ -10,8 +10,8 @@ namespace Barryvdh\LaravelIdeHelper\Console; +use Barryvdh\LaravelIdeHelper\Eloquent; use Barryvdh\LaravelIdeHelper\Generator; -use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; use Symfony\Component\Console\Input\InputOption; @@ -115,6 +115,7 @@ class GeneratorCommand extends Command if ($written !== false) { $this->info("A new helper file was written to $filename"); + Eloquent::writeEloquentModelHelper($this, $this->files); } else { $this->error("The helper file could not be created at $filename"); } diff --git a/src/Eloquent.php b/src/Eloquent.php new file mode 100644 index 0000000..c27b1c1 --- /dev/null +++ b/src/Eloquent.php @@ -0,0 +1,78 @@ + + */ +namespace Barryvdh\LaravelIdeHelper; + +use Illuminate\Console\Command; +use Illuminate\Filesystem\Filesystem; +use Barryvdh\Reflection\DocBlock; +use Barryvdh\Reflection\DocBlock\Context; +use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer; +use Barryvdh\Reflection\DocBlock\Tag; + +class Eloquent +{ + /** + * Write mixin helper to the Eloquent\Model + * This is needed since laravel/framework v5.4.29 + * + * @param Command $command + * @param Filesystem $files + * + * @return void + */ + public static function writeEloquentModelHelper(Command $command, Filesystem $files) + { + $class = 'Illuminate\Database\Eloquent\Model'; + + $reflection = new \ReflectionClass($class); + $namespace = $reflection->getNamespaceName(); + $originalDoc = $reflection->getDocComment(); + if (!$originalDoc) { + $command->info('Unexpected no document on ' . $class); + } + $phpdoc = new DocBlock($reflection, new Context($namespace)); + + $mixins = $phpdoc->getTagsByName('mixin'); + foreach ($mixins as $m) { + if ($m->getContent() === '\Eloquent') { + $command->info('Tag Exists: @mixin \Eloquent in ' . $class); + + return; + } + } + + // add the Eloquent mixin + $phpdoc->appendTag(Tag::createInstance("@mixin \\Eloquent", $phpdoc)); + + $serializer = new DocBlockSerializer(); + $serializer->getDocComment($phpdoc); + $docComment = $serializer->getDocComment($phpdoc); + + $filename = $reflection->getFileName(); + if ($filename) { + $contents = $files->get($filename); + if ($contents) { + $count = 0; + $contents = str_replace($originalDoc, $docComment, $contents, $count); + if ($count > 0) { + if ($files->put($filename, $contents)) { + $command->info('Wrote @mixin \Eloquent to ' . $filename); + } else { + $command->error('File write failed to ' . $filename); + } + } else { + $command->error('Content did not change ' . $contents); + } + } else { + $command->error('No file contents found ' . $filename); + } + } else { + $command->error('Filename not found ' . $class); + } + } +} diff --git a/src/IdeHelperServiceProvider.php b/src/IdeHelperServiceProvider.php index 0b4c6d5..1305fb8 100644 --- a/src/IdeHelperServiceProvider.php +++ b/src/IdeHelperServiceProvider.php @@ -10,10 +10,11 @@ namespace Barryvdh\LaravelIdeHelper; -use Illuminate\Support\ServiceProvider; +use Barryvdh\LaravelIdeHelper\Console\EloquentCommand; +use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; use Barryvdh\LaravelIdeHelper\Console\MetaCommand; use Barryvdh\LaravelIdeHelper\Console\ModelsCommand; -use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand; +use Illuminate\Support\ServiceProvider; use Illuminate\View\Engines\EngineResolver; use Illuminate\View\Engines\PhpEngine; use Illuminate\View\Factory; @@ -80,7 +81,19 @@ class IdeHelperServiceProvider extends ServiceProvider } ); - $this->commands('command.ide-helper.generate', 'command.ide-helper.models', 'command.ide-helper.meta'); + $this->app->singleton( + 'command.ide-helper.eloquent', + function ($app) use ($localViewFactory) { + return new EloquentCommand($app['files']); + } + ); + + $this->commands( + 'command.ide-helper.generate', + 'command.ide-helper.models', + 'command.ide-helper.meta', + 'command.ide-helper.eloquent' + ); } /**