Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Autoload Package Providers #19420

Merged
merged 8 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Illuminate/Contracts/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ public function booted($callback);
* @return string
*/
public function getCachedServicesPath();

/**
* Get the path to the cached packages.php file.
*
* @return string
*/
public function getCachedPackagesPath();
}
10 changes: 10 additions & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,16 @@ public function getCachedServicesPath()
return $this->bootstrapPath().'/cache/services.php';
}

/**
* Get the path to the cached services.php file.
*
* @return string
*/
public function getCachedPackagesPath()
{
return $this->bootstrapPath().'/cache/packages.php';
}

/**
* Determine if the application configuration is cached.
*
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/Foundation/Bootstrap/RegisterPackageProviders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Foundation\Bootstrap;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\PackageAssetLoader;
use Illuminate\Contracts\Foundation\Application;

class RegisterPackageProviders
{
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
if (! $app->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);
}
}
}
}
4 changes: 4 additions & 0 deletions src/Illuminate/Foundation/ComposerScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ protected static function clearCompiled()
if (file_exists($servicesPath = $laravel->getCachedServicesPath())) {
@unlink($servicesPath);
}

if (file_exists($packagesPath = $laravel->getCachedPackagesPath())) {
@unlink($packagesPath);
}
}
}
10 changes: 6 additions & 4 deletions src/Illuminate/Foundation/Console/ClearCompiledCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class ClearCompiledCommand extends Command
*/
public function fire()
{
$servicesPath = $this->laravel->getCachedServicesPath();

if (file_exists($servicesPath)) {
if (file_exists($servicesPath = $this->laravel->getCachedServicesPath())) {
@unlink($servicesPath);
}

$this->info('The compiled services file has been removed.');
if (file_exists($packagesPath = $this->laravel->getCachedPackagesPath())) {
@unlink($packagesPath);
}

$this->info('The compiled services & packages files have been removed.');
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ 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,
];

Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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,
];

Expand Down
81 changes: 81 additions & 0 deletions src/Illuminate/Foundation/PackageAssetLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Illuminate\Foundation;

use Exception;
use Illuminate\Filesystem\Filesystem;

class PackageAssetLoader
{
/**
* @var \Illuminate\Filesystem\Filesystem
*/
private $files;

/**
* @var string
*/
private $vendorPath;

/**
* @var string|null
*/
private $manifestPath;

public function __construct(Filesystem $files, string $vendorPath, string $manifestPath = null)
{
$this->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, '<?php return '.var_export($manifest, true).';'
);
}
}
17 changes: 17 additions & 0 deletions tests/Foundation/FoundationPackageAssetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Tests\Foundation;

use PHPUnit\Framework\TestCase;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\PackageAssetLoader;

class FoundationPackageAssetsTest extends TestCase
{
public function testAssetLoading()
{
$assetLoader = new PackageAssetLoader(new Filesystem, __DIR__.'/fixtures/vendor');

$this->assertEquals($assetLoader->get('providers'), ['foo', 'bar', 'baz']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extra": {
"providers": "foo"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extra": {
"providers": [
"bar",
"baz"
]
}
}