Fixing issue where incorrect autoloader unregistered (#1210)

Co-authored-by: Terence Marks <[email protected]>
This commit is contained in:
Terence Marks
2021-04-09 07:54:00 +02:00
committed by GitHub
co-authored by Terence Marks
parent c1ddd30532
commit ca94ffd99a
3 changed files with 48 additions and 8 deletions
+3
View File
@@ -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
-----------------
+12 -8
View File
@@ -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);
}
}
@@ -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.
*