-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: EnvVarLoader to determine resource-root
- Loading branch information
1 parent
b54181f
commit 012f4fc
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Resource\Env; | ||
|
||
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
class EnvVarLoader implements EnvVarLoaderInterface | ||
{ | ||
private readonly string $baseDir; | ||
|
||
public function __construct( | ||
string $baseDir = null | ||
) { | ||
$this->baseDir = $baseDir ?? (getcwd() ?: ''); | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* RESOURCE_ROOT?: non-empty-string, | ||
* } | ||
*/ | ||
public function loadEnvVars(): array | ||
{ | ||
$env = []; | ||
|
||
$resourceRoot = $_SERVER['RESOURCE_ROOT'] ?? ''; | ||
if (!is_string($resourceRoot) || empty($resourceRoot)) { | ||
$resourceRoot = $this->determineResourceRootForCliCall(); | ||
if (!empty($resourceRoot)) { | ||
$env['RESOURCE_ROOT'] = $resourceRoot; | ||
// other EnvVarLoader needs this value | ||
$_SERVER['RESOURCE_ROOT'] = $resourceRoot; | ||
} | ||
} | ||
return $env; | ||
} | ||
|
||
/** | ||
* If the call was made via a CLI command, an attempt is made to | ||
* the resource root via the path of the `bin/console` script. | ||
* to determine the resource root. | ||
* This is successful if the script is called via the absolute | ||
* path to the `app` folder below the host directory. | ||
* | ||
* E.G. | ||
* /var/www/example.com/www/app/bin/console | ||
* | ||
*/ | ||
private function determineResourceRootForCliCall(): ?string | ||
{ | ||
/** @var string[] $directories */ | ||
$directories = [ | ||
$this->baseDir | ||
]; | ||
|
||
$filename = $_SERVER['SCRIPT_FILENAME'] ?? null; | ||
if (is_string($filename)) { | ||
$binDir = dirname($filename); | ||
$appDir = dirname($binDir); | ||
$hostDir = dirname($appDir); | ||
$directories[] = $hostDir; | ||
} | ||
|
||
foreach ($directories as $dir) { | ||
$realpath = realpath($dir); | ||
if ($realpath === false) { | ||
continue; | ||
} | ||
|
||
if (is_file($realpath . '/resources/context.php')) { | ||
return $realpath . '/resources'; | ||
} | ||
if (is_file($realpath . '/context.php')) { | ||
return $realpath; | ||
} | ||
if (is_file($realpath . '/WEB-IES/context.php')) { | ||
return $realpath; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Resource\Test\Env; | ||
|
||
use Atoolo\Resource\Env\EnvVarLoader; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(EnvVarLoader::class)] | ||
class EnvVarLoaderTest extends TestCase | ||
{ | ||
private string $baseDir = __DIR__ . '/../resources/Env/EnvVarLoader'; | ||
|
||
private string $scriptFileNameBackup; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->scriptFileNameBackup = $_SERVER['SCRIPT_FILENAME'] ?? null; | ||
} | ||
public function tearDown(): void | ||
{ | ||
unset($_SERVER['RESOURCE_ROOT']); | ||
$_SERVER['SCRIPT_FILENAME'] = $this->scriptFileNameBackup; | ||
} | ||
|
||
public function testLoadVarsWithExistsResourceRoot(): void | ||
{ | ||
$_SERVER['RESOURCE_ROOT'] = 'test'; | ||
$loader = new EnvVarLoader(); | ||
$env = $loader->loadEnvVars(); | ||
$this->assertFalse( | ||
isset($env['RESOURCE_ROOT']), | ||
'RESOURCE_ROOT should no set' | ||
); | ||
} | ||
|
||
public function testDetermineResourceWithInvalidDir(): void | ||
{ | ||
$loader = new EnvVarLoader('/invalid-dir'); | ||
$env = $loader->loadEnvVars(); | ||
$this->assertFalse( | ||
isset($env['RESOURCE_ROOT']), | ||
'RESOURCE_ROOT should no set' | ||
); | ||
} | ||
|
||
public function testDetermineResourceViaScriptFilename(): void | ||
{ | ||
$_SERVER['SCRIPT_FILENAME'] = | ||
$this->baseDir . '/hostDir/app/bin/console'; | ||
$loader = new EnvVarLoader('/tmp'); | ||
$env = $loader->loadEnvVars(); | ||
$this->assertEquals( | ||
$this->baseDir . '/hostDir/resources', | ||
isset($env['RESOURCE_ROOT']), | ||
'unexpected RESOURCE_ROOT' | ||
); | ||
} | ||
|
||
public function testDetermineResourceRootInHostDir(): void | ||
{ | ||
$loader = new EnvVarLoader($this->baseDir . '/hostDir'); | ||
$env = $loader->loadEnvVars(); | ||
$this->assertEquals( | ||
$this->baseDir . '/hostDir/resources', | ||
isset($env['RESOURCE_ROOT']), | ||
'unexpected RESOURCE_ROOT' | ||
); | ||
} | ||
|
||
public function testDetermineResourceRootInResourceDir(): void | ||
{ | ||
$loader = new EnvVarLoader($this->baseDir . '/hostDir/resources'); | ||
$env = $loader->loadEnvVars(); | ||
$this->assertEquals( | ||
$this->baseDir . '/hostDir/resources', | ||
isset($env['RESOURCE_ROOT']), | ||
'unexpected RESOURCE_ROOT' | ||
); | ||
} | ||
|
||
public function testDetermineResourceRootWithDocumentRootLayout(): void | ||
{ | ||
$loader = new EnvVarLoader($this->baseDir . '/documentRootLayout'); | ||
$env = $loader->loadEnvVars(); | ||
$this->assertEquals( | ||
$this->baseDir . '/documentRootLayout', | ||
isset($env['RESOURCE_ROOT']), | ||
'unexpected RESOURCE_ROOT' | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/resources/Env/EnvVarLoader/documentRootLayout/WEB-IES/context.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return []; |
5 changes: 5 additions & 0 deletions
5
test/resources/Env/EnvVarLoader/hostDir/resources/context.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
return []; |