diff --git a/src/Illuminate/Foundation/Console/PackageDiscoverCommand.php b/src/Illuminate/Foundation/Console/PackageDiscoverCommand.php new file mode 100644 index 000000000000..be7398365692 --- /dev/null +++ b/src/Illuminate/Foundation/Console/PackageDiscoverCommand.php @@ -0,0 +1,36 @@ +build(); + + $this->info('Package manifest generated successfully.'); + } +} diff --git a/src/Illuminate/Foundation/PackageManifest.php b/src/Illuminate/Foundation/PackageManifest.php new file mode 100644 index 000000000000..62ed7858d39c --- /dev/null +++ b/src/Illuminate/Foundation/PackageManifest.php @@ -0,0 +1,174 @@ +files = $files; + $this->basePath = $basePath; + $this->manifestPath = $manifestPath; + $this->vendorPath = $basePath.'/vendor'; + } + + /** + * Get all of the service provider class names for all packages. + * + * @return array + */ + public function providers() + { + $this->ensureManifestIsLoaded(); + + return collect($this->manifest)->flatMap(function ($configuration, $name) { + return (array) ($configuration['providers'] ?? []); + })->filter()->all(); + } + + /** + * Get all of the aliases for all packages. + * + * @return array + */ + public function aliases() + { + $this->ensureManifestIsLoaded(); + + return collect($this->manifest)->flatMap(function ($configuration, $name) { + return (array) ($configuration['aliases'] ?? []); + })->filter()->all(); + } + + /** + * Ensure the manifest has been loaded into memory. + * + * @return void + */ + protected function ensureManifestIsLoaded() + { + if (! is_null($this->manifest)) { + return; + } + + if (! file_exists($this->manifestPath)) { + $this->build(); + } + + if (file_exists($this->manifestPath)) { + $this->manifest = $this->files->getRequire($this->manifestPath); + } else { + $this->manifest = []; + } + } + + /** + * Build the manifest and write it to disk. + * + * @return void + */ + public function build() + { + $vendors = $this->files->directories($this->vendorPath); + + $ignore = $this->packagesToIgnore(); + + $this->write(collect($vendors)->flatMap(function ($vendor) { + return $this->files->directories($vendor); + })->filter(function ($package) { + return $this->files->exists($package.'/composer.json'); + })->mapWithKeys(function ($package) { + return [$this->format($package) => json_decode( + $this->files->get($package.'/composer.json'), true + )['extra']['laravel'] ?? []]; + })->reject(function ($configuration, $package) use ($ignore) { + return in_array($package, $ignore); + })->filter()->all()); + } + + /** + * Format the given package name. + * + * @param string $package + * @return string + */ + protected function format($package) + { + return str_replace($this->vendorPath.'/', '', $package); + } + + /** + * Get all of the package names that should be ignored. + * + * @return array + */ + protected function packagesToIgnore() + { + return json_decode(file_get_contents( + $this->basePath.'/composer.json' + ), true)['extra']['laravel']['dont-discover'] ?? []; + } + + /** + * Write the given manifest array to disk. + * + * @param arary $manifest + * @return void + */ + protected function write(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, '