Skip to content

Commit

Permalink
Add namespace Env
Browse files Browse the repository at this point in the history
- Add namespace `Env`
- Remove `Env::init()`
- Remove `env()`
- Bump minimum PHP requirement to v5.6
- Update TravisCI build matrix to build PHP 5.6~7.4 and nightly
- Install phpunit via composer
- Add `$ composer test`
  • Loading branch information
tangrufus committed May 27, 2020
1 parent 4ab45ce commit c722e90
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
composer.lock
vendor/
19 changes: 10 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
language: php
sudo: false
matrix:
include:
- php: 5.2
dist: precise
- php: 5.3
dist: precise
php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- nightly

jobs:
allow_failures:
- php: nightly

install:
- composer install --no-interaction --prefer-dist

script:
- phpunit
- composer test
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $ composer require oscarotero/env
var_dump(getenv('FOO')); //string(5) "false"

// Using Env:
var_dump(Env::get('FOO')); //bool(false)
var_dump(\Env\Env::get('FOO')); //bool(false)
```

## Available conversions
Expand All @@ -49,6 +49,8 @@ There's also additional settings that you can enable (they're disabled by defaul
* `Env::LOCAL_FIRST` To get first the values of locally-set environment variables.

```php
use Env\Env;

//Convert booleans and null, but not integers or strip quotes
Env::$options = Env::CONVERT_BOOL | Env::CONVERT_NULL;

Expand All @@ -64,19 +66,15 @@ Env::$options ^= Env::CONVERT_NULL;
By default, if the value does not exist, returns `null`, but you can change for any other value:

```php
Env::$default = false;
\Env\Env::$default = false;
```

## The env() function

If you don't want to complicate with classes and namespaces, you can use the `env()` function, like in Laravel or other libraries:

```php
Env::init(); //expose the function to globals

//now you can use it

var_dump(env('FOO'));
var_dump(\Env\env('FOO'));
```

---
Expand Down
20 changes: 17 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@
"issues": "https://github.com/oscarotero/env/issues"
},
"require": {
"php": ">=5.2",
"php": ">=5.6",
"ext-ctype": "*"
},
"autoload": {
"psr-0": {
"Env": "src/"
"psr-4": {
"Env\\": "src/"
},
"files": [
"src/env_function.php"
]
},
"config": {
"platform": {
"php": "5.6.40"
}
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"scripts": {
"test": "phpunit"
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="./tests/bootstrap.php">
<phpunit>
<testsuites>
<testsuite name="All tests">
<directory>./tests/</directory>
Expand Down
19 changes: 2 additions & 17 deletions src/Env.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Env;

class Env
{
const CONVERT_BOOL = 1;
Expand All @@ -12,23 +14,6 @@ class Env
public static $options = 15; //All flags enabled
public static $default = null; //Default value if not exists

/**
* Include the global env() function.
* Returns whether the function has been registered or not.
*
* @return bool
*/
public static function init()
{
if (function_exists('env')) {
return false;
}

include_once dirname(__FILE__).'/env_function.php';

return true;
}

/**
* Returns an environment variable.
*
Expand Down
2 changes: 2 additions & 0 deletions src/env_function.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Env;

/**
* Gets the value of an environment variable.
*
Expand Down
32 changes: 24 additions & 8 deletions tests/ConversionTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

if (!class_exists('PHPUnit_Framework_TestCase')) {
class_alias('PHPUnit\\Framework\\TestCase', 'PHPUnit_Framework_TestCase');
}
use Env\Env;
use function Env\env;

class ConversionTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -38,14 +37,10 @@ public function testConversions($value, $options, $expected)

public function testEnv()
{
$this->assertTrue(Env::init());

putenv('FOO=123');

$this->assertSame(123, env('FOO'));

$this->assertFalse(Env::init());

//Switch to $_ENV
Env::$options |= Env::USE_ENV_ARRAY;

Expand All @@ -54,10 +49,31 @@ public function testEnv()
$_ENV['FOO'] = 456;

$this->assertSame(456, env('FOO'));

//Switch to getenv again
Env::$options ^= Env::USE_ENV_ARRAY;

$this->assertSame(123, env('FOO'));
}

public function testEnvGet()
{
putenv('FOO=123');

$this->assertSame(123, Env::get('FOO'));

//Switch to $_ENV
Env::$options |= Env::USE_ENV_ARRAY;

$this->assertNull(Env::get('FOO'));

$_ENV['FOO'] = 456;

$this->assertSame(456, Env::get('FOO'));

//Switch to getenv again
Env::$options ^= Env::USE_ENV_ARRAY;

$this->assertSame(123, Env::get('FOO'));
}
}
5 changes: 0 additions & 5 deletions tests/bootstrap.php

This file was deleted.

0 comments on commit c722e90

Please sign in to comment.