Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jun 1, 2017
1 parent a5a0f3e commit 2954091
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Illuminate/Foundation/Console/PackageDiscoverCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\Command;
use Illuminate\Foundation\PackageManifest;

class PackageDiscoverCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'package:discover';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Rebuild the cached package manifest';

/**
* Execute the console command.
*
* @param \Illuminate\Foundation\PackageManifest
* @return void
*/
public function handle(PackageManifest $manifest)
{
$manifest->build();

$this->info('Package manifest generated successfully.');
}
}
174 changes: 174 additions & 0 deletions src/Illuminate/Foundation/PackageManifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace Illuminate\Foundation;

use Exception;
use Illuminate\Filesystem\Filesystem;

class PackageManifest
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
public $files;

/**
* The base path.
*
* @var string
*/
public $basePath;

/**
* The vendor path.
*
* @var string
*/
public $vendorPath;

/**
* The manifest path.
*
* @var string|null
*/
public $manifestPath;

/**
* The loaded manifest array.
*
* @var array
*/
public $manifest;

/**
* Create a new package manifest instance.
*
* @param \Illuminate\Foundation\Filesystem $files
* @param string $basePath
* @param string $manifestPath
* @return void
*/
public function __construct(Filesystem $files, $basePath, $manifestPath)
{
$this->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, '<?php return '.var_export($manifest, true).';'
);
}
}

0 comments on commit 2954091

Please sign in to comment.