From ea43056034c54d273ae28f4f7bb00828a888fceb Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 2 Jan 2024 15:33:16 +0000 Subject: [PATCH] Improves application builder --- src/Illuminate/Foundation/Application.php | 8 ++-- .../FoundationApplicationBuilderTest.php | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/Foundation/FoundationApplicationBuilderTest.php diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 21456adc1e49..943c81e8ae94 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -224,9 +224,11 @@ public function __construct($basePath = null) */ public static function configure(string $baseDirectory = null) { - $baseDirectory = $ENV['APP_BASE_PATH'] ?? ($baseDirectory ?: dirname(dirname( - debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'] - ))); + $baseDirectory = match (true) { + is_string($baseDirectory) => $baseDirectory, + isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'], + default => dirname(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], 2), + }; return (new Configuration\ApplicationBuilder(new static($baseDirectory))) ->withKernels() diff --git a/tests/Foundation/FoundationApplicationBuilderTest.php b/tests/Foundation/FoundationApplicationBuilderTest.php new file mode 100644 index 000000000000..2808623d4ddd --- /dev/null +++ b/tests/Foundation/FoundationApplicationBuilderTest.php @@ -0,0 +1,44 @@ +create(); + + $this->assertSame(__DIR__.'/as-arg', $app->basePath()); + } + + public function testBaseDirectoryWithEnv() + { + $_ENV['APP_BASE_PATH'] = __DIR__.'/as-env'; + + $app = Application::configure()->create(); + + $this->assertSame(__DIR__.'/as-env', $app->basePath()); + } + + public function testBaseDirectoryWithDebugBacktrace() + { + $app = Application::configure()->create(); + + $this->assertSame(dirname(__DIR__), $app->basePath()); + } +}