Skip to content

Commit ca94ffd

Browse files
tezhmTerence Marks
and
Terence Marks
authored
Fixing issue where incorrect autoloader unregistered (barryvdh#1210)
Co-authored-by: Terence Marks <[email protected]>
1 parent c1ddd30 commit ca94ffd

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ All notable changes to this project will be documented in this file.
99
- Allowing Methods to be set or unset in ModelHooks [\#1198 / jenga201](https://github.com/barryvdh/laravel-ide-helper/pull/1198)\
1010
Note: the visibility of `\Barryvdh\LaravelIdeHelper\Console\ModelsCommand::setMethod` has been changed to **public**!
1111

12+
### Fixed
13+
- Fixing issue where incorrect autoloader unregistered [\#1210 / tezhm](https://github.com/barryvdh/laravel-ide-helper/pull/1210)
14+
1215
2021-04-02, 2.9.3
1316
-----------------
1417

src/Console/MetaCommand.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function handle()
8484
// Needs to run before exception handler is registered
8585
$factories = $this->config->get('ide-helper.include_factory_builders') ? Factories::all() : [];
8686

87-
$this->registerClassAutoloadExceptions();
87+
$ourAutoloader = $this->registerClassAutoloadExceptions();
8888

8989
$bindings = [];
9090
foreach ($this->getAbstracts() as $abstract) {
@@ -106,7 +106,7 @@ public function handle()
106106
}
107107
}
108108

109-
$this->unregisterClassAutoloadExceptions();
109+
$this->unregisterClassAutoloadExceptions($ourAutoloader);
110110

111111
$content = $this->view->make('meta', [
112112
'bindings' => $bindings,
@@ -143,12 +143,16 @@ protected function getAbstracts()
143143

144144
/**
145145
* Register an autoloader the throws exceptions when a class is not found.
146+
*
147+
* @return callable
146148
*/
147-
protected function registerClassAutoloadExceptions()
149+
protected function registerClassAutoloadExceptions(): callable
148150
{
149-
spl_autoload_register(function ($class) {
151+
$autoloader = function ($class) {
150152
throw new \ReflectionException("Class '$class' not found.");
151-
});
153+
};
154+
spl_autoload_register($autoloader);
155+
return $autoloader;
152156
}
153157

154158
/**
@@ -167,11 +171,11 @@ protected function getOptions()
167171

168172
/**
169173
* Remove our custom autoloader that we pushed onto the autoload stack
174+
*
175+
* @param callable $ourAutoloader
170176
*/
171-
private function unregisterClassAutoloadExceptions()
177+
private function unregisterClassAutoloadExceptions(callable $ourAutoloader): void
172178
{
173-
$autoloadFunctions = spl_autoload_functions();
174-
$ourAutoloader = array_pop($autoloadFunctions);
175179
spl_autoload_unregister($ourAutoloader);
176180
}
177181
}

tests/Console/MetaCommand/MetaCommandTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Barryvdh\LaravelIdeHelper\Tests\TestCase;
99
use Illuminate\Filesystem\Filesystem;
1010
use Mockery\MockInterface;
11+
use stdClass;
1112

1213
class MetaCommandTest extends TestCase
1314
{
@@ -37,6 +38,38 @@ public function testCommand(): void
3738
self::assertStringContainsString('override(', $this->mockFilesystemOutput);
3839
}
3940

41+
public function testUnregisterAutoloader(): void
42+
{
43+
$current = spl_autoload_functions();
44+
$appended = function () { };
45+
46+
$this->app->bind('registers-autoloader', function () use ($appended)
47+
{
48+
spl_autoload_register($appended);
49+
return new stdClass();
50+
});
51+
52+
$this->mockFilesystem();
53+
54+
/** @var Filesystem|MockInterface $mockFileSystem */
55+
$mockFileSystem = $this->app->make(Filesystem::class);
56+
$this->instance('files', $mockFileSystem);
57+
58+
$mockFileSystem
59+
->shouldReceive('getRequire')
60+
->andReturnUsing(function ($__path, $__data) {
61+
return (static function () use ($__path, $__data) {
62+
extract($__data, EXTR_SKIP);
63+
64+
return require $__path;
65+
})();
66+
});
67+
68+
$this->artisan('ide-helper:meta');
69+
70+
self::assertSame(array_merge($current, [$appended]), spl_autoload_functions());
71+
}
72+
4073
/**
4174
* Get package providers.
4275
*

0 commit comments

Comments
 (0)