laravel 阻止包自动发现

想要替换掉 ElasticsearchServiceProvider 不生效。

vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php

    public function load(array $providers)
    {
        $manifest = $this->loadManifest();

        // First we will load the service manifest, which contains information on all
        // service providers registered with the application and which services it
        // provides. This is used to know which services are "deferred" loaders.
        if ($this->shouldRecompile($manifest, $providers)) {
            $manifest = $this->compileManifest($providers);
        }
    public function loadManifest()
    {
        // The service manifest is a file containing a JSON representation of every
        // service provided by the application and whether its provider is using
        // deferred loading or should be eagerly loaded on each request to us.
        if ($this->files->exists($this->manifestPath)) {
            $manifest = $this->files->getRequire($this->manifestPath);

            if ($manifest) {
                return array_merge(['when' => []], $manifest);
            }
        }
    }

vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php

    AliasLoader::getInstance(array_merge(
            $app->make('config')->get('app.aliases', []),
            $app->make(PackageManifest::class)->aliases()
        ))->register();

vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php

public function aliases()
{
    return collect($this->getManifest())->flatMap(function ($configuration) {
        return (array) ($configuration['aliases'] ?? []);
    })->filter()->all();
}

vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php

    protected function getManifest()
    {
        if (! is_null($this->manifest)) {
            return $this->manifest;
        }

        if (! file_exists($this->manifestPath)) {
            $this->build();
        }

        return $this->manifest = file_exists($this->manifestPath) ?
            $this->files->getRequire($this->manifestPath) : [];
    }

开始追溯 $this->manifestPath vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php

public function __construct(Filesystem $files, $basePath, $manifestPath)
{
   $this->files = $files;
   $this->basePath = $basePath;
   $this->manifestPath = $manifestPath;
   $this->vendorPath = $basePath.'/vendor';

}

完成追溯 $this->manifestPath vendor/laravel/framework/src/Illuminate/Foundation/Application.php

protected function registerBaseBindings()
{
    static::setInstance($this);

    $this->instance('app', $this);

    $this->instance(Container::class, $this);

    $this->instance(PackageManifest::class, new PackageManifest(
        new Filesystem, $this->basePath(), $this->getCachedPackagesPath()
    ));
}

public function getCachedPackagesPath()
{
    return $this->bootstrapPath().'/cache/packages.php';
}

清除上图中的缓存文件php artisan clear-compiled

开始追溯 $this->build() vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php

    public function build()
    {
        $packages = [];

        if ($this->files->exists($path = $this->vendorPath.'/composer/installed.json')) {
            $packages = json_decode($this->files->get($path), true);
        }

        $ignoreAll = in_array('*', $ignore = $this->packagesToIgnore());

        $this->write(collect($packages)->mapWithKeys(function ($package) {
            return [$this->format($package['name']) => $package['extra']['laravel'] ?? []];
        })->each(function ($configuration) use (&$ignore) {
            $ignore = array_merge($ignore, $configuration['dont-discover'] ?? []);
        })->reject(function ($configuration, $package) use ($ignore, $ignoreAll) {
            return $ignoreAll || in_array($package, $ignore);
        })->filter()->all());
    }

开始追溯 $this->packagesToIgnore() vendor/laravel/framework/src/Illuminate/Foundation/PackageManifest.php

    protected function packagesToIgnore()
    {
        if (! file_exists($this->basePath.'/composer.json')) {
            return [];
        }

        return json_decode(file_get_contents(
            $this->basePath.'/composer.json'
        ), true)['extra']['laravel']['dont-discover'] ?? [];
    }

发现composer.json中的 ['extra']['laravel']['dont-discover'] 可以阻止包自动发现

composer.json


    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
                "basemkhirat/elasticsearch"
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },