Skip to content

Commit 4deacb2

Browse files
authored
[11.x] Improves Application::configure method (#49552)
* Improves application builder * [11.x] Infers base path from composer instead (#49553) * Uses composer instead of `debug_backtrace` * Removes non used import
1 parent 06cd2bc commit 4deacb2

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 18 additions & 3 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;
@@ -224,16 +225,30 @@ public function __construct($basePath = null)
224225
*/
225226
public static function configure(string $baseDirectory = null)
226227
{
227-
$baseDirectory = $ENV['APP_BASE_PATH'] ?? ($baseDirectory ?: dirname(dirname(
228-
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file']
229-
)));
228+
$baseDirectory = match (true) {
229+
is_string($baseDirectory) => $baseDirectory,
230+
default => static::inferBaseDirectory(),
231+
};
230232

231233
return (new Configuration\ApplicationBuilder(new static($baseDirectory)))
232234
->withKernels()
233235
->withEvents()
234236
->withCommands();
235237
}
236238

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+
237252
/**
238253
* Get the version number of the application.
239254
*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Foundation;
4+
5+
use Illuminate\Foundation\Application;
6+
use Mockery as m;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class FoundationApplicationBuilderTest extends TestCase
10+
{
11+
protected function tearDown(): void
12+
{
13+
m::close();
14+
15+
unset($_ENV['APP_BASE_PATH']);
16+
17+
parent::tearDown();
18+
}
19+
20+
public function testBaseDirectoryWithArg()
21+
{
22+
$_ENV['APP_BASE_PATH'] = __DIR__.'/as-env';
23+
24+
$app = Application::configure(__DIR__.'/as-arg')->create();
25+
26+
$this->assertSame(__DIR__.'/as-arg', $app->basePath());
27+
}
28+
29+
public function testBaseDirectoryWithEnv()
30+
{
31+
$_ENV['APP_BASE_PATH'] = __DIR__.'/as-env';
32+
33+
$app = Application::configure()->create();
34+
35+
$this->assertSame(__DIR__.'/as-env', $app->basePath());
36+
}
37+
38+
public function testBaseDirectoryWithComposer()
39+
{
40+
$app = Application::configure()->create();
41+
42+
$this->assertSame(dirname(__DIR__, 2), $app->basePath());
43+
}
44+
}

0 commit comments

Comments
 (0)