Skip to content

Commit

Permalink
Merge pull request #14 from borschphp/paas-compatibility
Browse files Browse the repository at this point in the history
Upd: higher compatibility with PaaS
  • Loading branch information
debuss authored Sep 27, 2023
2 parents b8ff0d6 + 2984065 commit 2ea8cca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bootstrap/env.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
}

$dotenv = Dotenv\Dotenv::createImmutable(app_path());
$environment = $dotenv->load();
$environment = $dotenv->safeLoad();

// In production, save the environment in a file so that it won't be necessary to parse and load .env file at every call.
if (env('APP_ENV') == 'production') {
if (env('APP_ENV') == 'production' && count($environment)) {
$file = new SplFileObject($environment_file, 'w');
$file->fwrite('<?php'.PHP_EOL.PHP_EOL.'return '.var_export($environment, true).';'.PHP_EOL);
$file = null;
Expand Down
8 changes: 4 additions & 4 deletions bootstrap/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
*/
function env(string $key, mixed $default = null): mixed
{
$value = $_ENV[$key] ?? false;
$value = $_ENV[$key] ?? $_SERVER[$key] ?? false;

if ($value === false) {
return $default;
}

$cleaned_value = trim(strtolower($value), '() \n\r\t\v\x00');
$cleaned_value = trim(strtolower($value), "() \n\r\t\v\x00");
return match ($cleaned_value) {
'true', 'false' => filter_var($cleaned_value, FILTER_VALIDATE_BOOLEAN),
'true', 'false', 'yes', 'no' => filter_var($cleaned_value, FILTER_VALIDATE_BOOLEAN),
'empty' => '',
'null' => null,
default => trim($value, '"\''),
default => trim($value)
};
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,15 @@ public function getContainer(): Container
$this->server_request_to_exception = $factory->createServerRequest('GET', 'https://example.com/to/exception');
})
->in('Unit/Middleware');

uses()
->beforeEach(function () {
$_ENV['TEST1'] = 'true';
$_ENV['TEST2'] = 'yes';
$_ENV['TEST3'] = 'false';
$_ENV['TEST4'] = 'no';
$_ENV['TEST5'] = 'empty';
$_ENV['TEST6'] = 'null';
$_ENV['TEST7'] = 'a value ';
})
->in('Unit/Bootstrap/HelpersTest.php');
23 changes: 23 additions & 0 deletions tests/Unit/Bootstrap/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

test('env can deal with TRUE', function() {
expect(env('TEST1'))->toBeTrue()
->and(env('TEST2'))->toBeTrue();
});

test('env can deal with FALSE', function() {
expect(env('TEST3'))->toBeFalse()
->and(env('TEST4'))->toBeFalse();
});

test('env can deal with EMPTY', function() {
expect(env('TEST5'))->toBe('');
});

test('env can deal with NULL', function() {
expect(env('TEST6'))->toBe(null);
});

test('env can deal with DEFAULT', function() {
expect(env('TEST7'))->toBe('a value');
});

0 comments on commit 2ea8cca

Please sign in to comment.