Skip to content

Commit

Permalink
[11.x] Infers base path from composer instead (#49553)
Browse files Browse the repository at this point in the history
* Uses composer instead of `debug_backtrace`

* Removes non used import
  • Loading branch information
nunomaduro authored Jan 3, 2024
1 parent ea43056 commit ffa8d7d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation;

use Closure;
use Composer\Autoload\ClassLoader;
use Illuminate\Container\Container;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
Expand Down Expand Up @@ -226,8 +227,7 @@ public static function configure(string $baseDirectory = null)
{
$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),
default => static::inferBaseDirectory(),
};

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

/**
* Infer the application's base directory from the environment.
*
* @return string
*/
public static function inferBaseDirectory()
{
return match (true) {
isset($_ENV['APP_BASE_PATH']) => $_ENV['APP_BASE_PATH'],
default => dirname(array_keys(ClassLoader::getRegisteredLoaders())[0]),
};
}

/**
* Get the version number of the application.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Foundation/FoundationApplicationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function testBaseDirectoryWithEnv()
$this->assertSame(__DIR__.'/as-env', $app->basePath());
}

public function testBaseDirectoryWithDebugBacktrace()
public function testBaseDirectoryWithComposer()
{
$app = Application::configure()->create();

$this->assertSame(dirname(__DIR__), $app->basePath());
$this->assertSame(dirname(__DIR__, 2), $app->basePath());
}
}

0 comments on commit ffa8d7d

Please sign in to comment.