From b32fdef1ec6f64dfbf5fffd8be638b52c3e1e1dd Mon Sep 17 00:00:00 2001 From: Maarten Date: Thu, 13 Feb 2020 19:01:45 +0100 Subject: [PATCH] Add ENV loader --- .env | 3 +- app/Http/Controllers/HomeController.php | 3 +- composer.json | 3 +- composer.lock | 115 +++++++++++++++++++++++- src/Runtime/Config/Config.php | 17 ++++ src/Runtime/Config/Env.php | 41 +++++++++ src/Runtime/Helpers/helpers.php | 27 ++++++ 7 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 src/Runtime/Config/Config.php create mode 100644 src/Runtime/Config/Env.php diff --git a/.env b/.env index 352bc6c..f17a4a9 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -DEBUG=true \ No newline at end of file +DEBUG=true +VIEW_CACHE=true \ No newline at end of file diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 8c3819e..50feedf 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -3,13 +3,12 @@ namespace App\Http\Controllers; use Runtime\Http\Controllers\Controller; -use Runtime\Http\View\Factory; class HomeController extends Controller { public function index() { - + dump(env('debug')); } public function test() diff --git a/composer.json b/composer.json index 40292c9..3bc77fd 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "filp/whoops": "^2.6", "kint-php/kint": "^3.3", "php-di/php-di": "^6.0", - "twig/twig": "^3.0" + "twig/twig": "^3.0", + "vlucas/phpdotenv": "^4.1" }, "config": { "optimize-autoloader": true, diff --git a/composer.lock b/composer.lock index 19b37a1..4fb8d26 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "01196a6854719a6769583f7e66f1b40b", + "content-hash": "1ddd13bd5ad6627f558a1db1382afc64", "packages": [ { "name": "filp/whoops", @@ -390,6 +390,61 @@ ], "time": "2019-09-26T11:24:58+00:00" }, + { + "name": "phpoption/phpoption", + "version": "1.7.2", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-option.git", + "reference": "77f7c4d2e65413aff5b5a8cc8b3caf7a28d81959" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/77f7c4d2e65413aff5b5a8cc8b3caf7a28d81959", + "reference": "77f7c4d2e65413aff5b5a8cc8b3caf7a28d81959", + "shasum": "" + }, + "require": { + "php": "^5.5.9 || ^7.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.3", + "phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7-dev" + } + }, + "autoload": { + "psr-4": { + "PhpOption\\": "src/PhpOption/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com" + }, + { + "name": "Graham Campbell", + "email": "graham@alt-three.com" + } + ], + "description": "Option Type for PHP", + "keywords": [ + "language", + "option", + "php", + "type" + ], + "time": "2019-12-15T19:35:24+00:00" + }, { "name": "psr/container", "version": "1.0.0", @@ -774,6 +829,64 @@ "templating" ], "time": "2019-11-15T20:38:32+00:00" + }, + { + "name": "vlucas/phpdotenv", + "version": "v4.1.0", + "source": { + "type": "git", + "url": "https://github.com/vlucas/phpdotenv.git", + "reference": "0176075a1b7ee9cf86f70143ec79edf7072c975a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/vlucas/phpdotenv/zipball/0176075a1b7ee9cf86f70143ec79edf7072c975a", + "reference": "0176075a1b7ee9cf86f70143ec79edf7072c975a", + "shasum": "" + }, + "require": { + "php": "^5.5.9 || ^7.0", + "phpoption/phpoption": "^1.7.1", + "symfony/polyfill-ctype": "^1.9" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.3", + "phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0 || ^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-4": { + "Dotenv\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "graham@alt-three.com", + "homepage": "https://gjcampbell.co.uk/" + }, + { + "name": "Vance Lucas", + "email": "vance@vancelucas.com", + "homepage": "https://vancelucas.com/" + } + ], + "description": "Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.", + "keywords": [ + "dotenv", + "env", + "environment" + ], + "time": "2019-12-14T13:59:29+00:00" } ], "packages-dev": [], diff --git a/src/Runtime/Config/Config.php b/src/Runtime/Config/Config.php new file mode 100644 index 0000000..88ecd1f --- /dev/null +++ b/src/Runtime/Config/Config.php @@ -0,0 +1,17 @@ +load(); + + $result = getenv(strtoupper($key)); + + return self::filter($result); + } + + /** + * @param string $result + * @return mixed + */ + private static function filter(string $result) + { + switch ($result) { + case filter_var($result, FILTER_VALIDATE_BOOLEAN): + return (bool) $result; + break; + case filter_var($result, FILTER_VALIDATE_INT): + return (int) $result; + break; + default: + return $result; + } + } + +} \ No newline at end of file diff --git a/src/Runtime/Helpers/helpers.php b/src/Runtime/Helpers/helpers.php index ea08936..8be95e1 100644 --- a/src/Runtime/Helpers/helpers.php +++ b/src/Runtime/Helpers/helpers.php @@ -1,6 +1,8 @@