From ca94ffd99a55763c5129b47c75087e132e1805db Mon Sep 17 00:00:00 2001 From: Terence Marks Date: Fri, 9 Apr 2021 15:54:00 +1000 Subject: [PATCH] Fixing issue where incorrect autoloader unregistered (#1210) Co-authored-by: Terence Marks --- CHANGELOG.md | 3 ++ src/Console/MetaCommand.php | 20 ++++++----- tests/Console/MetaCommand/MetaCommandTest.php | 33 +++++++++++++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9684aa1..defd85d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ All notable changes to this project will be documented in this file. - Allowing Methods to be set or unset in ModelHooks [\#1198 / jenga201](https://github.com/barryvdh/laravel-ide-helper/pull/1198)\ Note: the visibility of `\Barryvdh\LaravelIdeHelper\Console\ModelsCommand::setMethod` has been changed to **public**! +### Fixed +- Fixing issue where incorrect autoloader unregistered [\#1210 / tezhm](https://github.com/barryvdh/laravel-ide-helper/pull/1210) + 2021-04-02, 2.9.3 ----------------- diff --git a/src/Console/MetaCommand.php b/src/Console/MetaCommand.php index b5cbf72..f9dfbe7 100644 --- a/src/Console/MetaCommand.php +++ b/src/Console/MetaCommand.php @@ -84,7 +84,7 @@ class MetaCommand extends Command // Needs to run before exception handler is registered $factories = $this->config->get('ide-helper.include_factory_builders') ? Factories::all() : []; - $this->registerClassAutoloadExceptions(); + $ourAutoloader = $this->registerClassAutoloadExceptions(); $bindings = []; foreach ($this->getAbstracts() as $abstract) { @@ -106,7 +106,7 @@ class MetaCommand extends Command } } - $this->unregisterClassAutoloadExceptions(); + $this->unregisterClassAutoloadExceptions($ourAutoloader); $content = $this->view->make('meta', [ 'bindings' => $bindings, @@ -143,12 +143,16 @@ class MetaCommand extends Command /** * Register an autoloader the throws exceptions when a class is not found. + * + * @return callable */ - protected function registerClassAutoloadExceptions() + protected function registerClassAutoloadExceptions(): callable { - spl_autoload_register(function ($class) { + $autoloader = function ($class) { throw new \ReflectionException("Class '$class' not found."); - }); + }; + spl_autoload_register($autoloader); + return $autoloader; } /** @@ -167,11 +171,11 @@ class MetaCommand extends Command /** * Remove our custom autoloader that we pushed onto the autoload stack + * + * @param callable $ourAutoloader */ - private function unregisterClassAutoloadExceptions() + private function unregisterClassAutoloadExceptions(callable $ourAutoloader): void { - $autoloadFunctions = spl_autoload_functions(); - $ourAutoloader = array_pop($autoloadFunctions); spl_autoload_unregister($ourAutoloader); } } diff --git a/tests/Console/MetaCommand/MetaCommandTest.php b/tests/Console/MetaCommand/MetaCommandTest.php index 13c6671..daf36dc 100644 --- a/tests/Console/MetaCommand/MetaCommandTest.php +++ b/tests/Console/MetaCommand/MetaCommandTest.php @@ -8,6 +8,7 @@ use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider; use Barryvdh\LaravelIdeHelper\Tests\TestCase; use Illuminate\Filesystem\Filesystem; use Mockery\MockInterface; +use stdClass; class MetaCommandTest extends TestCase { @@ -37,6 +38,38 @@ class MetaCommandTest extends TestCase self::assertStringContainsString('override(', $this->mockFilesystemOutput); } + public function testUnregisterAutoloader(): void + { + $current = spl_autoload_functions(); + $appended = function () { }; + + $this->app->bind('registers-autoloader', function () use ($appended) + { + spl_autoload_register($appended); + return new stdClass(); + }); + + $this->mockFilesystem(); + + /** @var Filesystem|MockInterface $mockFileSystem */ + $mockFileSystem = $this->app->make(Filesystem::class); + $this->instance('files', $mockFileSystem); + + $mockFileSystem + ->shouldReceive('getRequire') + ->andReturnUsing(function ($__path, $__data) { + return (static function () use ($__path, $__data) { + extract($__data, EXTR_SKIP); + + return require $__path; + })(); + }); + + $this->artisan('ide-helper:meta'); + + self::assertSame(array_merge($current, [$appended]), spl_autoload_functions()); + } + /** * Get package providers. *