Skip to content

Commit ffa8d7d

Browse files
authored
[11.x] Infers base path from composer instead (#49553)
* Uses composer instead of `debug_backtrace` * Removes non used import
1 parent ea43056 commit ffa8d7d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Foundation;
44

55
use Closure;
6+
use Composer\Autoload\ClassLoader;
67
use Illuminate\Container\Container;
78
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
89
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
@@ -226,8 +227,7 @@ public static function configure(string $baseDirectory = null)
226227
{
227228
$baseDirectory = match (true) {
228229
is_string($baseDirectory) => $baseDirectory,
229-
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
230-
default => dirname(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], 2),
230+
default => static::inferBaseDirectory(),
231231
};
232232

233233
return (new Configuration\ApplicationBuilder(new static($baseDirectory)))
@@ -236,6 +236,19 @@ public static function configure(string $baseDirectory = null)
236236
->withCommands();
237237
}
238238

239+
/**
240+
* Infer the application's base directory from the environment.
241+
*
242+
* @return string
243+
*/
244+
public static function inferBaseDirectory()
245+
{
246+
return match (true) {
247+
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
248+
default => dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]),
249+
};
250+
}
251+
239252
/**
240253
* Get the version number of the application.
241254
*

tests/Foundation/FoundationApplicationBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public function testBaseDirectoryWithEnv()
3535
$this->assertSame(__DIR__.'/as-env', $app->basePath());
3636
}
3737

38-
public function testBaseDirectoryWithDebugBacktrace()
38+
public function testBaseDirectoryWithComposer()
3939
{
4040
$app = Application::configure()->create();
4141

42-
$this->assertSame(dirname(__DIR__), $app->basePath());
42+
$this->assertSame(dirname(__DIR__, 2), $app->basePath());
4343
}
4444
}

0 commit comments

Comments
 (0)