laravel 代码追踪

barryvdh 这个作者的 github 有好多不错的组件,大家可以自己看看

  • 安装组件
composer require barryvdh/laravel-ide-helper
  • config/app.phpproviders 数组中添加
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
  • 在项目终端执行命令 php artisan 可以看到我们多了几个可以被执行的指令
    注意,以下几个指令只有在 providers 数组添加 provider 之后才可以看到

    2560633-1ac46f461a5ec800.png
    php artisan

  • 追踪 Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class 代码

<?php
/**
 * Laravel IDE Helper Generator
 *
 * @author    Barry vd. Heuvel <[email protected]>
 * @copyright 2014 Barry vd. Heuvel / 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;

use Barryvdh\LaravelIdeHelper\Console\EloquentCommand;
use Barryvdh\LaravelIdeHelper\Console\GeneratorCommand;
use Barryvdh\LaravelIdeHelper\Console\MetaCommand;
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;

class IdeHelperServiceProvider extends ServiceProvider
{

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot()
    {
        $viewPath = __DIR__.'/../resources/views';
        $this->loadViewsFrom($viewPath, 'ide-helper');

        $configPath = __DIR__ . '/../config/ide-helper.php';
        if (function_exists('config_path')) {
            $publishPath = config_path('ide-helper.php');
        } else {
            $publishPath = base_path('config/ide-helper.php');
        }
        $this->publishes([$configPath => $publishPath], 'config');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $configPath = __DIR__ . '/../config/ide-helper.php';
        $this->mergeConfigFrom($configPath, 'ide-helper');
        $localViewFactory = $this->createLocalViewFactory();

        $this->app->singleton(
            'command.ide-helper.generate',
            function ($app) use ($localViewFactory) {
                return new GeneratorCommand($app['config'], $app['files'], $localViewFactory);
            }
        );

        $this->app->singleton(
            'command.ide-helper.models',
            function ($app) {
                return new ModelsCommand($app['files']);
            }
        );

        $this->app->singleton(
            'command.ide-helper.meta',
            function ($app) use ($localViewFactory) {
                return new MetaCommand($app['files'], $localViewFactory, $app['config']);
            }
        );

        $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'
        );
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('command.ide-helper.generate', 'command.ide-helper.models');
    }

    /**
     * @return Factory
     */
    private function createLocalViewFactory()
    {
        $resolver = new EngineResolver();
        $resolver->register('php', function () {
            return new PhpEngine();
        });
        $finder = new FileViewFinder($this->app['files'], [__DIR__ . '/../resources/views']);
        $factory = new Factory($resolver, $finder, $this->app['events']);
        $factory->addExtension('php', 'php');

        return $factory;
    }
}
  • 如果你的 Laravel 版本比较低,可以试试其他版本,我尝试的是
    composer require barryvdh/laravel-ide-helper:~2.4.3

猜你喜欢

转载自blog.csdn.net/weixin_34059951/article/details/86822927