diff --git a/composer.json b/composer.json index 75120950..9db2f5c7 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,9 @@ "ext-json": "*" }, "autoload": { + "files": [ + "flight/autoload.php" + ], "classmap": [ "flight/Flight.php" ], diff --git a/flight/autoload.php b/flight/autoload.php index e4017284..9ae0c69d 100644 --- a/flight/autoload.php +++ b/flight/autoload.php @@ -8,7 +8,8 @@ require_once __DIR__ . '/core/Loader.php'; if (file_exists(__DIR__ . '/../vendor/autoload.php')) { - return require_once __DIR__ . '/../vendor/autoload.php'; + require_once __DIR__ . '/../vendor/autoload.php'; + Loader::autoload(true); +} else { + Loader::autoload(true, [dirname(__DIR__)]); } - -Loader::autoload(true, dirname(__DIR__)); diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php index 97ee31aa..a6ad8631 100644 --- a/tests/AutoloadTest.php +++ b/tests/AutoloadTest.php @@ -4,9 +4,12 @@ namespace tests; +use Flight; +use flight\core\Loader; use flight\Engine; use tests\classes\User; use PHPUnit\Framework\TestCase; +use ReflectionClass; class AutoloadTest extends TestCase { @@ -18,6 +21,17 @@ protected function setUp(): void $this->app->path(__DIR__ . '/classes'); } + protected function tearDown(): void + { + $dirsProperty = (new ReflectionClass(Loader::class))->getProperty('dirs'); + + if (PHP_VERSION_ID < 80100) { + $dirsProperty->setAccessible(true); + } + + $dirsProperty->setValue(null, []); + } + // Autoload a class public function testAutoload(): void { @@ -44,4 +58,17 @@ public function testMissingClass(): void self::assertNull($test); } + + // Flight::path() must load namespaced classes that Composer PSR-4 does not map + public function testPathAutoloadsNamespacedClassOutsideComposerPsr4(): void + { + $class = \app\middleware\Something::class; + + self::assertFalse(class_exists($class)); + + Flight::path(__DIR__ . '/path_autoload_fixtures'); + + self::assertTrue(class_exists($class)); + self::assertTrue(class_exists(Engine::class)); + } } diff --git a/tests/path_autoload_fixtures/app/middleware/Something.php b/tests/path_autoload_fixtures/app/middleware/Something.php new file mode 100644 index 00000000..67675bb8 --- /dev/null +++ b/tests/path_autoload_fixtures/app/middleware/Something.php @@ -0,0 +1,9 @@ +