Skip to content

Commit

Permalink
Catch non-existant class fatal exceptions
Browse files Browse the repository at this point in the history
Add autoloader that throws an error, so we can catch errors instead of fatal crashes.
Fixes barryvdh#220
  • Loading branch information
barryvdh committed Jun 8, 2015
1 parent 5aa0f55 commit bb3bba2
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Console/MetaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function __construct($files, $view) {
*/
public function fire()
{
$this->registerClassAutoloadExceptions();

$bindings = array();
foreach ($this->getAbstracts() as $abstract) {
try {
Expand All @@ -76,7 +78,7 @@ public function fire()
$bindings[$abstract] = get_class($concrete);
}
}catch (\Exception $e) {
$this->error("Cannot make $abstract: ".$e->getMessage());
$this->comment("Cannot make '$abstract': ".$e->getMessage());
}
}

Expand All @@ -96,28 +98,28 @@ public function fire()
}

/**
* Get a filtered list of abstracts from the Laravel Application.
* Get a list of abstracts from the Laravel Application.
*
* @return array
*/
protected function getAbstracts()
{
$abstracts = $this->laravel->getBindings();

// Remove the S3 cloud driver when not available
if (config('filesystems.cloud') === 's3' && !class_exists('League\Flysystem\AwsS3v2\AwsS3Adapter')) {
unset($abstracts['filesystem.cloud']);
}

// Remove Redis when not available
if (isset($abstracts['redis']) && !class_exists('Predis\Client')) {
unset($abstracts['redis']);
}

// Return the abstract names only
return array_keys($abstracts);
}

/**
* Register an autoloader the throws exceptions when a class is not found.
*/
protected function registerClassAutoloadExceptions()
{
spl_autoload_register(function ($class) {
throw new \Exception("Class '$class' not found.");
});
}

/**
* Get the console command options.
*
Expand Down

0 comments on commit bb3bba2

Please sign in to comment.