Skip to content

Commit

Permalink
Merge pull request shlinkio#16 from acelaya-forks/feature/env-function
Browse files Browse the repository at this point in the history
Feature/env function
  • Loading branch information
acelaya authored Jan 1, 2022
2 parents 4bde8c5 + 2ecad1e commit 56ab5b8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [1.5.0] - 2022-01-01
### Added
* Added `env` function from `shlinkio/shlink-common` package.

### Changed
* *Nothing*

### Deprecated
* *Nothing*

### Removed
* *Nothing*

### Fixed
* *Nothing*


## [1.4.0] - 2021-12-05
### Added
* Created new factory to tell if either swoole or openswoole are enabled
Expand Down
20 changes: 20 additions & 0 deletions functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Laminas\Config\Factory;
use Laminas\Stdlib\Glob;

use function getenv;
use function strtolower;
use function trim;

/**
* Loads configuration files which match provided glob pattern, and returns the merged result as array
*/
Expand All @@ -16,3 +20,19 @@ function loadConfigFromGlob(string $globPattern): array
$config = Factory::fromFiles(Glob::glob($globPattern, Glob::GLOB_BRACE));
return $config;
}

function env(string $key, mixed $default = null): mixed
{
$value = getenv($key);
if ($value === false) {
return $default;
}

return match (strtolower($value)) {
'true', '(true)' => true,
'false', '(false)' => false,
'empty', '(empty)' => '',
'null', '(null)' => null,
default => trim($value),
};
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
<directory suffix=".php">./functions</directory>
</include>
</coverage>
</phpunit>
65 changes: 65 additions & 0 deletions test/Functions/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace ShlinkioTest\Shlink\Config\Functions;

use PHPUnit\Framework\TestCase;

use function putenv;
use function Shlinkio\Shlink\Config\env;

class FunctionsTest extends TestCase
{
public static function setUpBeforeClass(): void
{
putenv('TRUE_VALUE=true');
putenv('TRUE_VALUE_PARENTHESES=(true)');
putenv('FALSE_VALUE=false');
putenv('FALSE_VALUE_PARENTHESES=(false)');
putenv('EMPTY_VALUE=empty');
putenv('EMPTY_VALUE_PARENTHESES=(empty)');
putenv('NULL_VALUE=null');
putenv('NULL_VALUE_PARENTHESES=(null)');
putenv('REGULAR_VALUE= foo ');
}

public static function tearDownAfterClass(): void
{
putenv('TRUE_VALUE=');
putenv('TRUE_VALUE_PARENTHESES=');
putenv('FALSE_VALUE=');
putenv('FALSE_VALUE_PARENTHESES=');
putenv('EMPTY_VALUE=');
putenv('EMPTY_VALUE_PARENTHESES=');
putenv('NULL_VALUE=');
putenv('NULL_VALUE_PARENTHESES=');
putenv('REGULAR_VALUE=');
}

/** @test */
public function envReturnsDefaultValueForUndefinedEnvVars(): void
{
self::assertEquals(null, env('THIS_DOES_NOT_EXIST'));
self::assertEquals('default', env('THIS_DOES_NOT_EXIST', 'default'));
}

/** @test */
public function specificValueKeywordsAreParsed(): void
{
self::assertTrue(env('TRUE_VALUE'));
self::assertTrue(env('TRUE_VALUE_PARENTHESES'));
self::assertFalse(env('FALSE_VALUE'));
self::assertFalse(env('FALSE_VALUE_PARENTHESES'));
self::assertEmpty(env('EMPTY_VALUE'));
self::assertEmpty(env('EMPTY_VALUE_PARENTHESES'));
self::assertNull(env('NULL_VALUE'));
self::assertNull(env('NULL_VALUE_PARENTHESES'));
}

/** @test */
public function regularValuesAreTrimmed(): void
{
self::assertEquals('foo', env('REGULAR_VALUE'));
}
}

0 comments on commit 56ab5b8

Please sign in to comment.