├── .github └── workflows │ └── test.yaml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Env.php └── env_function.php └── tests └── ConversionTest.php /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: "testing" 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | tests: 11 | name: Tests 12 | runs-on: ubuntu-latest 13 | 14 | strategy: 15 | matrix: 16 | php: 17 | - 7.1 18 | - 7.2 19 | - 7.3 20 | - 7.4 21 | - 8.0 22 | - 8.1 23 | - 8.2 24 | - 8.3 25 | - 8.4 26 | 27 | steps: 28 | - name: Checkout 29 | uses: actions/checkout@v4 30 | 31 | - name: Install PHP 32 | uses: shivammathur/setup-php@v2 33 | with: 34 | php-version: ${{ matrix.php }} 35 | 36 | - name: Cache PHP dependencies 37 | uses: actions/cache@v4 38 | with: 39 | path: vendor 40 | key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} 41 | restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-composer- 42 | 43 | - name: Install dependencies 44 | run: composer install --prefer-dist --no-progress 45 | 46 | - name: Tests 47 | run: composer test 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor/ 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## [2.1.1] - 2024-12-03 8 | ### Fixed 9 | - Support for PHP 8.4 [#12]. 10 | 11 | ## [2.1.0] - 2020-06-11 12 | ### Added 13 | - New constant `Env::USE_SERVER_ARRAY` to get the values using `$_SERVER` array instead `getenv()` [#8]. 14 | 15 | ## [2.0.0] - 2020-06-03 16 | ### Changed 17 | - This library is under the `Env` namespace instead the global. [#7] 18 | - The function `env` is under `Env` namespace and loaded automatically. [#7] 19 | - Included PHP 7 strict typing 20 | 21 | ### Removed 22 | - Support for PHP 5.x and PHP 7.0. 23 | 24 | ## [1.2.0] - 2019-04-03 25 | ### Added 26 | - New constant `Env::LOCAL_FIRST` to get first the values of locally-set environment variables [#6]. 27 | 28 | ### Fixed 29 | - Added requirement for ctype extension in composer.json [#4]. 30 | 31 | ## [1.1.0] - 2017-07-17 32 | ### Added 33 | - New constant `Env::USE_ENV_ARRAY` to get the values using `$_ENV` array instead `getenv()` [#3]. 34 | 35 | ### Fixed 36 | - Fixed test in php versions 5.2 and 7.x 37 | 38 | ## [1.0.2] - 2016-05-08 39 | ### Fixed 40 | - `Env::init()` checks if the function `env()` exists, to prevent fatal errors on declare the function twice. If the funcion couldn't be included, returns false, otherwise returns true [#1]. 41 | 42 | ## [1.0.1] - 2015-12-31 43 | ### Fixed 44 | - Fixed error on strip quotes to empty strings 45 | 46 | ## [1.0.0] - 2015-12-30 47 | First stable version 48 | 49 | [#1]: https://github.com/oscarotero/env/issues/1 50 | [#3]: https://github.com/oscarotero/env/issues/3 51 | [#4]: https://github.com/oscarotero/env/issues/4 52 | [#6]: https://github.com/oscarotero/env/issues/6 53 | [#7]: https://github.com/oscarotero/env/issues/7 54 | [#8]: https://github.com/oscarotero/env/issues/8 55 | [#12]: https://github.com/oscarotero/env/issues/12 56 | 57 | [2.1.1]: https://github.com/oscarotero/env/compare/v2.1.0...v2.1.1 58 | [2.1.0]: https://github.com/oscarotero/env/compare/v2.0.0...v2.1.0 59 | [2.0.0]: https://github.com/oscarotero/env/compare/v1.2.0...v2.0.0 60 | [1.2.0]: https://github.com/oscarotero/env/compare/v1.1.0...v1.2.0 61 | [1.1.0]: https://github.com/oscarotero/env/compare/v1.0.2...v1.1.0 62 | [1.0.2]: https://github.com/oscarotero/env/compare/v1.0.1...v1.0.2 63 | [1.0.1]: https://github.com/oscarotero/env/compare/v1.0.0...v1.0.1 64 | [1.0.0]: https://github.com/oscarotero/env/releases/tag/v1.0.0 65 | 66 | --- 67 | 68 | Previous releases are documented in [github releases](https://github.com/oscarotero/Gettext/releases) 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Oscar Otero Marzoa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # env 2 | 3 | [![Software License][ico-license]](LICENSE) 4 | [![Total Downloads][ico-downloads]][link-downloads] 5 | 6 | Simple library to get environment variables converted to simple types. 7 | 8 | ## Installation 9 | 10 | This package is installable and autoloadable via Composer as 11 | [oscarotero/env](https://packagist.org/packages/oscarotero/env). 12 | 13 | ```sh 14 | composer require oscarotero/env 15 | ``` 16 | 17 | ## Example 18 | 19 | ```php 20 | use Env\Env; 21 | 22 | // Using getenv function: 23 | var_dump(getenv('FOO')); //string(5) "false" 24 | 25 | // Using Env: 26 | var_dump(Env::get('FOO')); //bool(false) 27 | ``` 28 | 29 | ## Available conversions 30 | 31 | - `"false"` is converted to boolean `false` 32 | - `"true"` is converted to boolean `true` 33 | - `"null"` is converted to `null` 34 | - If the string contains only numbers is converted to an integer 35 | - If the string has quotes, remove them 36 | 37 | ## Options 38 | 39 | To configure the conversion, you can use the following constants (all enabled by 40 | default): 41 | 42 | - `Env::CONVERT_BOOL` To convert boolean values 43 | - `Env::CONVERT_NULL` To convert null values 44 | - `Env::CONVERT_INT` To convert integer values 45 | - `Env::STRIP_QUOTES` To remove the quotes of the strings 46 | 47 | There's also additional settings that you can enable (they're disabled by 48 | default) 49 | 50 | - `Env::USE_ENV_ARRAY` To get the values from `$_ENV`, instead `getenv()`. 51 | - `Env::USE_SERVER_ARRAY` To get the values from `$_SERVER`, instead `getenv()`. 52 | - `Env::LOCAL_FIRST` To get first the values of locally-set environment 53 | variables. 54 | 55 | ```php 56 | use Env\Env; 57 | 58 | //Convert booleans and null, but not integers or strip quotes 59 | Env::$options = Env::CONVERT_BOOL | Env::CONVERT_NULL; 60 | 61 | //Add one more option 62 | Env::$options |= Env::USE_ENV_ARRAY; 63 | 64 | //Remove one option 65 | Env::$options ^= Env::CONVERT_NULL; 66 | ``` 67 | 68 | ## Default value 69 | 70 | By default, if the value does not exist, returns `null`, but you can change for 71 | any other value: 72 | 73 | ```php 74 | use Env\Env; 75 | 76 | Env::$default = false; 77 | ``` 78 | 79 | ## The env() function 80 | 81 | You can use the `env()` function, like in Laravel or other frameworks: 82 | 83 | ```php 84 | use function Env\env; 85 | 86 | var_dump(env('FOO')); 87 | ``` 88 | 89 | --- 90 | 91 | Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes. 92 | 93 | The MIT License (MIT). Please see [LICENSE](LICENSE) for more information. 94 | 95 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 96 | [ico-downloads]: https://img.shields.io/packagist/dt/oscarotero/env.svg?style=flat-square 97 | [link-downloads]: https://packagist.org/packages/oscarotero/env 98 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "oscarotero/env", 3 | "type": "library", 4 | "description": "Simple library to consume environment variables", 5 | "keywords": ["env"], 6 | "homepage": "https://github.com/oscarotero/env", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Oscar Otero", 11 | "email": "oom@oscarotero.com", 12 | "homepage": "http://oscarotero.com", 13 | "role": "Developer" 14 | } 15 | ], 16 | "support": { 17 | "email": "oom@oscarotero.com", 18 | "issues": "https://github.com/oscarotero/env/issues" 19 | }, 20 | "require": { 21 | "php": ">=7.1", 22 | "ext-ctype": "*" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Env\\": "src/" 27 | }, 28 | "files": [ 29 | "src/env_function.php" 30 | ] 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit": ">=7.0", 34 | "friendsofphp/php-cs-fixer": "^2.16" 35 | }, 36 | "scripts": { 37 | "test": "phpunit" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ./tests/ 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/Env.php: -------------------------------------------------------------------------------- 1 | assertSame($expected, $result); 37 | } 38 | 39 | public function testFunction() 40 | { 41 | putenv('FOO=123'); 42 | 43 | $this->assertSame(123, env('FOO')); 44 | 45 | //Switch to $_ENV 46 | Env::$options |= Env::USE_ENV_ARRAY; 47 | 48 | $this->assertNull(env('FOO')); 49 | 50 | $_ENV['FOO'] = 456; 51 | 52 | $this->assertSame(456, env('FOO')); 53 | 54 | //Switch to $_SERVER 55 | Env::$options ^= Env::USE_ENV_ARRAY; 56 | Env::$options |= Env::USE_SERVER_ARRAY; 57 | 58 | $this->assertNull(env('FOO')); 59 | 60 | $_SERVER['FOO'] = 789; 61 | 62 | $this->assertSame(789, env('FOO')); 63 | 64 | //Switch to getenv again 65 | Env::$options ^= Env::USE_SERVER_ARRAY; 66 | 67 | $this->assertSame(123, env('FOO')); 68 | } 69 | 70 | public function testClass() 71 | { 72 | putenv('BAR=123'); 73 | 74 | $this->assertSame(123, Env::get('BAR')); 75 | 76 | //Switch to $_ENV 77 | Env::$options |= Env::USE_ENV_ARRAY; 78 | 79 | $this->assertNull(Env::get('BAR')); 80 | 81 | $_ENV['BAR'] = 456; 82 | 83 | $this->assertSame(456, Env::get('BAR')); 84 | 85 | //Switch to $_SERVER 86 | Env::$options ^= Env::USE_ENV_ARRAY; 87 | Env::$options |= Env::USE_SERVER_ARRAY; 88 | 89 | $this->assertNull(Env::get('BAR')); 90 | 91 | $_SERVER['BAR'] = 789; 92 | 93 | $this->assertSame(789, Env::get('BAR')); 94 | 95 | //Switch to getenv again 96 | Env::$options ^= Env::USE_SERVER_ARRAY; 97 | 98 | $this->assertSame(123, Env::get('BAR')); 99 | } 100 | } 101 | --------------------------------------------------------------------------------