From bb3bba2c5beb6c372dc3c144239ff20f48998bd6 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 8 Jun 2015 09:04:31 +0200 Subject: [PATCH] Catch non-existant class fatal exceptions Add autoloader that throws an error, so we can catch errors instead of fatal crashes. Fixes #220 --- src/Console/MetaCommand.php | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/Console/MetaCommand.php b/src/Console/MetaCommand.php index 77350141e..f7a9e6514 100644 --- a/src/Console/MetaCommand.php +++ b/src/Console/MetaCommand.php @@ -68,6 +68,8 @@ public function __construct($files, $view) { */ public function fire() { + $this->registerClassAutoloadExceptions(); + $bindings = array(); foreach ($this->getAbstracts() as $abstract) { try { @@ -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()); } } @@ -96,7 +98,7 @@ public function fire() } /** - * Get a filtered list of abstracts from the Laravel Application. + * Get a list of abstracts from the Laravel Application. * * @return array */ @@ -104,20 +106,20 @@ 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. *