Skip to content

Commit

Permalink
feat: EnvVarLoader to determine resource-root
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed May 24, 2024
1 parent b54181f commit 012f4fc
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ parameters:

services:

Atoolo\Resource\Env\EnvVarLoader:
tags:
- { name: 'container.env_var_loader', priority: 10 }

atoolo_resource.resource_channel_factory:
class: Atoolo\Resource\SiteKitResourceChannelFactory
arguments:
Expand Down
86 changes: 86 additions & 0 deletions src/Env/EnvVarLoader.php
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;
}
}
94 changes: 94 additions & 0 deletions test/Env/EnvVarLoaderTest.php
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'
);
}
}
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 test/resources/Env/EnvVarLoader/hostDir/resources/context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

return [];

0 comments on commit 012f4fc

Please sign in to comment.