Skip to content

Commit

Permalink
Improves application builder
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jan 2, 2024
1 parent 06cd2bc commit ea43056
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
44 changes: 44 additions & 0 deletions tests/Foundation/FoundationApplicationBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Illuminate\Tests\Foundation;

use Illuminate\Foundation\Application;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class FoundationApplicationBuilderTest extends TestCase
{
protected function tearDown(): void
{
m::close();

unset($_ENV['APP_BASE_PATH']);

parent::tearDown();
}

public function testBaseDirectoryWithArg()
{
$_ENV['APP_BASE_PATH'] = __DIR__.'/as-env';

$app = Application::configure(__DIR__.'/as-arg')->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());
}
}

0 comments on commit ea43056

Please sign in to comment.