Skip to content

Commit

Permalink
feat(env): fixing env detection
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Feb 5, 2024
1 parent 8379ab1 commit 7736dba
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 64 deletions.
2 changes: 1 addition & 1 deletion bin/start
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $resources = $resources->findValue('string', true) ?? '';
$dieOnChange = $dieOnChange->findValue('bool') ?? false;
$watch = $watch->findValue('bool') ?? false;
$php = $php->findValue('string', true) ?? 'php';
$environment = $environment->findValue('string', true) ?? 'env';
$environment = $environment->findValue('string', true) ?? '';

try {
if ($watch) {
Expand Down
1 change: 0 additions & 1 deletion build.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: catpaw
entry: ./src/main.php
libraries: ./src
environment: ./env.yaml
match: /(^\.\/(\.build-cache|src|vendor|bin)\/.*)|(^\.\/(\.env|env\.yaml|env\.yml))/
23 changes: 11 additions & 12 deletions src/lib/Core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,28 @@ public static function start(
}

Container::set(LoggerInterface::class, $logger);
$env = new EnvironmentService($logger);

if ($environment) {
if (File::exists($environment)) {
$env->setFileName($environment);
$env->load(skipErrors:true)->try($error);
if ($error) {
self::kill((string)$error);
}
}
}

/** @var array<string> $librariesList */
$librariesList = !$libraries ? [] : preg_split('/[,;]/', $libraries);

/** @var array<string> $resourcesList */
$resourcesList = !$resources ? [] : preg_split('/[,;]/', $resources);

$env = new EnvironmentService($logger);
Container::set(EnvironmentService::class, $env);

$env->set('ENTRY', $entry);
$env->set('LIBRARIES', $librariesList);
$env->set('RESOURCES', $resourcesList);
$env->set('DIE_ON_CHANGE', $dieOnChange);

if ($environment) {
$env->setFileName($environment);
$env->load()->try($error);
if ($error) {
self::kill((string)$error);
}
}

foreach ($librariesList as $library) {
if (!str_starts_with($library, './')) {
if (!$isWindows) {
Expand Down
72 changes: 24 additions & 48 deletions src/lib/Core/Services/EnvironmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function __construct(
private string $fileName = './env.yaml';


/**
* Merge `$_ENV` and `getenv()` with this service's internal variables map.\
* \
* This may overwrite keys defined in your environment file.\
* Call `load()` again to recover the lost keys.
* @return void
*/
public function includeSystemEnvironment() {
$this->variables = [
...$this->variables,
Expand All @@ -44,61 +51,30 @@ public function setFileName(string $fileName):void {
$this->fileName = $fileName;
}

/**
* Clear all environment variables.
* @return void
*/
public function clear() {
$this->variables = [];
}

/**
* Parse the first valid environment file and update all variables in memory.
* Multiple calls are allowed.\
* This function is invoked automatically when the application starts.
* @param bool $skipErrors When set to `true`, the function will never return any errors.
* @return Unsafe<void>
*/
public function load(bool $skipErrors = false):Unsafe {
$this->variables = [];
$fileName = $this->fileName;

if (!File::exists($fileName)) {
$variants = [];

if (str_ends_with($fileName, '.yml')) {
$variants[] = substr($fileName, -3).'.yaml';
} else if (str_ends_with($fileName, '.yaml')) {
$variants[] = substr($fileName, -5).'.yml';
} else {
$variants[] = "$fileName.yaml";
$variants[] = "$fileName.yml";
$variants[] = ".$fileName";
}

foreach ($variants as $variant) {
if (!str_starts_with($variant, '/') && !str_starts_with($variant, '../') && !str_starts_with($variant, './')) {
$variant = "./$variant";
}

if (File::exists($variant)) {
$fileName = $variant;
break;
}
}
} else {
if (!str_starts_with($fileName, '/') && !str_starts_with($fileName, '../') && !str_starts_with($fileName, './')) {
$fileName = "./$fileName";
}
}

public function load():Unsafe {
$fileName = $this->fileName;

$file = File::open($fileName)->try($error);
if ($error) {
if ($skipErrors) {
return ok();
}
return error($error);
}

$content = $file->readAll()->await()->try($error);
if ($error) {
if ($skipErrors) {
return ok();
}
return error($error);
}

Expand All @@ -108,20 +84,20 @@ public function load(bool $skipErrors = false):Unsafe {
if (function_exists('yaml_parse')) {
$vars = yaml_parse($content);
if (false === $vars) {
if ($skipErrors) {
return ok();
}
return error("Error while parsing environment yaml file.");
}
$this->variables = $vars?:[];
$this->variables = [
...$this->variables,
...($vars?:[]),
];
} else {
if ($skipErrors) {
return ok();
}
return error("Could not parse environment file, the yaml extension is needed in order to parse yaml environment files.");
}
} else {
$this->variables = Dotenv::parse($content);
$this->variables = [
...$this->variables,
...Dotenv::parse($content),
];
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/scripts/Build/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ function build(
require 'vendor/autoload.php';
\$environment = new Option('--environment');
\$environment = \$environment->findValue('string', true);
\$environment = \$environment->findValue('string', true)??'';
if(null === \$environment){
if(\$environment){
\$environment = \Phar::running().'/'.$environmentFallbackStringified;
}
Expand Down

0 comments on commit 7736dba

Please sign in to comment.