From a5a0f3e7b82a1a4dc00037c5463a31d42c94903a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 1 Jun 2017 11:20:34 -0500 Subject: [PATCH] rewrite package auto-discovery --- src/Illuminate/Foundation/Application.php | 9 ++- .../Foundation/Bootstrap/RegisterFacades.php | 6 +- .../Bootstrap/RegisterPackageProviders.php | 31 ------- src/Illuminate/Foundation/ComposerScripts.php | 13 +++ src/Illuminate/Foundation/Console/Kernel.php | 1 - .../Foundation/Console/OptimizeCommand.php | 32 +------- .../Foundation/Exceptions/Handler.php | 12 ++- src/Illuminate/Foundation/Http/Kernel.php | 1 - .../Foundation/PackageAssetLoader.php | 81 ------------------- .../Providers/ArtisanServiceProvider.php | 14 ++++ 10 files changed, 50 insertions(+), 150 deletions(-) delete mode 100644 src/Illuminate/Foundation/Bootstrap/RegisterPackageProviders.php delete mode 100644 src/Illuminate/Foundation/PackageAssetLoader.php diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index caad4f0570f4..17b808b087ab 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -11,6 +11,7 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Log\LogServiceProvider; use Illuminate\Support\ServiceProvider; +use Illuminate\Foundation\PackageManifest; use Illuminate\Events\EventServiceProvider; use Illuminate\Routing\RoutingServiceProvider; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -176,6 +177,10 @@ protected function registerBaseBindings() $this->instance('app', $this); $this->instance(Container::class, $this); + + $this->instance(PackageManifest::class, new PackageManifest( + new Filesystem, $this->basePath(), $this->getCachedPackagesPath() + )); } /** @@ -545,8 +550,10 @@ public function runningUnitTests() */ public function registerConfiguredProviders() { + $providers = $this->make(PackageManifest::class)->providers(); + (new ProviderRepository($this, new Filesystem, $this->getCachedServicesPath())) - ->load($this->config['app.providers']); + ->load(array_merge($this->config['app.providers'], $providers)); } /** diff --git a/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php b/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php index 0c2d5cfce405..62a42d074a5e 100644 --- a/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php +++ b/src/Illuminate/Foundation/Bootstrap/RegisterFacades.php @@ -4,6 +4,7 @@ use Illuminate\Foundation\AliasLoader; use Illuminate\Support\Facades\Facade; +use Illuminate\Foundation\PackageManifest; use Illuminate\Contracts\Foundation\Application; class RegisterFacades @@ -20,6 +21,9 @@ public function bootstrap(Application $app) Facade::setFacadeApplication($app); - AliasLoader::getInstance($app->make('config')->get('app.aliases', []))->register(); + AliasLoader::getInstance(array_merge( + $app->make('config')->get('app.aliases', []), + $app->make(PackageManifest::class)->aliases() + ))->register(); } } diff --git a/src/Illuminate/Foundation/Bootstrap/RegisterPackageProviders.php b/src/Illuminate/Foundation/Bootstrap/RegisterPackageProviders.php deleted file mode 100644 index 25ce19ef15a3..000000000000 --- a/src/Illuminate/Foundation/Bootstrap/RegisterPackageProviders.php +++ /dev/null @@ -1,31 +0,0 @@ -make('config')->get('app.autoload_package_providers', true)) { - return; - } - - $assetLoader = new PackageAssetLoader(new Filesystem, base_path('vendor'), $app->getCachedPackagesPath()); - - foreach ($assetLoader->get('providers') as $provider) { - if (class_exists($provider)) { - $app->register($provider); - } - } - } -} diff --git a/src/Illuminate/Foundation/ComposerScripts.php b/src/Illuminate/Foundation/ComposerScripts.php index cf35b99ff9f8..fcda187fd261 100644 --- a/src/Illuminate/Foundation/ComposerScripts.php +++ b/src/Illuminate/Foundation/ComposerScripts.php @@ -32,6 +32,19 @@ public static function postUpdate(Event $event) static::clearCompiled(); } + /** + * Handle the post-autoload-dump Composer event. + * + * @param \Composer\Script\Event $event + * @return void + */ + public static function postAutoloadDump(Event $event) + { + require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; + + static::clearCompiled(); + } + /** * Clear the cached Laravel bootstrapping files. * diff --git a/src/Illuminate/Foundation/Console/Kernel.php b/src/Illuminate/Foundation/Console/Kernel.php index 1aaac0fba3a8..7c7a1f45acae 100644 --- a/src/Illuminate/Foundation/Console/Kernel.php +++ b/src/Illuminate/Foundation/Console/Kernel.php @@ -63,7 +63,6 @@ class Kernel implements KernelContract \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, - \Illuminate\Foundation\Bootstrap\RegisterPackageProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ]; diff --git a/src/Illuminate/Foundation/Console/OptimizeCommand.php b/src/Illuminate/Foundation/Console/OptimizeCommand.php index b64156e17e45..69debc82d237 100644 --- a/src/Illuminate/Foundation/Console/OptimizeCommand.php +++ b/src/Illuminate/Foundation/Console/OptimizeCommand.php @@ -20,27 +20,7 @@ class OptimizeCommand extends Command * * @var string */ - protected $description = 'Optimize the framework for better performance'; - - /** - * The composer instance. - * - * @var \Illuminate\Support\Composer - */ - protected $composer; - - /** - * Create a new optimize command instance. - * - * @param \Illuminate\Support\Composer $composer - * @return void - */ - public function __construct(Composer $composer) - { - parent::__construct(); - - $this->composer = $composer; - } + protected $description = 'Optimize the framework for better performance (deprecated)'; /** * Execute the console command. @@ -49,15 +29,7 @@ public function __construct(Composer $composer) */ public function fire() { - $this->info('Generating optimized class loader'); - - if ($this->option('psr')) { - $this->composer->dumpAutoloads(); - } else { - $this->composer->dumpOptimized(); - } - - $this->call('clear-compiled'); + // } /** diff --git a/src/Illuminate/Foundation/Exceptions/Handler.php b/src/Illuminate/Foundation/Exceptions/Handler.php index 5b927daf649d..eb5d69e3c3c2 100644 --- a/src/Illuminate/Foundation/Exceptions/Handler.php +++ b/src/Illuminate/Foundation/Exceptions/Handler.php @@ -127,10 +127,14 @@ protected function shouldntReport(Exception $e) */ protected function context() { - return array_filter([ - 'userId' => Auth::id(), - 'email' => Auth::user()->email ?? null, - ]); + try { + return array_filter([ + 'userId' => Auth::id(), + 'email' => Auth::user() ? Auth::user()->email : null, + ]); + } catch (Exception $e) { + return []; + } } /** diff --git a/src/Illuminate/Foundation/Http/Kernel.php b/src/Illuminate/Foundation/Http/Kernel.php index 239e9d599862..9a9c4265b83d 100644 --- a/src/Illuminate/Foundation/Http/Kernel.php +++ b/src/Illuminate/Foundation/Http/Kernel.php @@ -39,7 +39,6 @@ class Kernel implements KernelContract \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, - \Illuminate\Foundation\Bootstrap\RegisterPackageProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ]; diff --git a/src/Illuminate/Foundation/PackageAssetLoader.php b/src/Illuminate/Foundation/PackageAssetLoader.php deleted file mode 100644 index f4a7f4c6117c..000000000000 --- a/src/Illuminate/Foundation/PackageAssetLoader.php +++ /dev/null @@ -1,81 +0,0 @@ -files = $files; - $this->vendorPath = $vendorPath; - $this->manifestPath = $manifestPath; - } - - public function get(string $key): array - { - $manifest = []; - - if (file_exists($this->manifestPath)) { - $manifest = $this->files->getRequire($this->manifestPath); - - // If the manifest has a key for the given asset type, - // we'll simply return the assets without loading - // all of the assets again from the packages. - if (isset($manifest[$key])) { - return $manifest[$key]; - } - } - - $manifest[$key] = $this->retrieveAssets($key); - - if ($this->manifestPath) { - $this->writeManifest($manifest); - } - - return $manifest[$key]; - } - - private function retrieveAssets(string $key) - { - $assets = []; - - foreach ($this->files->directories($this->vendorPath) as $vendor) { - foreach ($this->files->directories($vendor) as $package) { - $config = json_decode($this->files->get($package.'/composer.json'), true); - - $assets = array_merge($assets, (array) ($config['extra'][$key] ?? [])); - } - } - - return array_unique($assets); - } - - private function writeManifest(array $manifest) - { - if (! is_writable(dirname($this->manifestPath))) { - throw new Exception('The bootstrap/cache directory must be present and writable.'); - } - - $this->files->put( - $this->manifestPath, ' 'command.migrate.rollback', 'MigrateStatus' => 'command.migrate.status', 'Optimize' => 'command.optimize', + 'PackageDiscover' => 'command.package.discover', 'Preset' => 'command.preset', 'QueueFailed' => 'command.queue.failed', 'QueueFlush' => 'command.queue.flush', @@ -567,6 +569,18 @@ protected function registerOptimizeCommand() }); } + /** + * Register the command. + * + * @return void + */ + protected function registerPackageDiscoverCommand() + { + $this->app->singleton('command.package.discover', function ($app) { + return new PackageDiscoverCommand; + }); + } + /** * Register the command. *