Add ENV loader

This commit is contained in:
Maarten 2020-02-13 19:01:45 +01:00
parent 4b601a7ffa
commit b32fdef1ec
7 changed files with 204 additions and 5 deletions

3
.env
View file

@ -1 +1,2 @@
DEBUG=true
DEBUG=true
VIEW_CACHE=true

View file

@ -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()

View file

@ -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,

115
composer.lock generated
View file

@ -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": [],

View file

@ -0,0 +1,17 @@
<?php
namespace Runtime\Config;
class Config {
/**
* @param $file
* @param $path
* @return mixed
*/
public static function get($file, $path)
{
return 'test';
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Runtime\Config;
use Dotenv\Dotenv;
class Env {
/**
* @param string $key
* @return string
*/
public static function get(string $key)
{
$dotenv = Dotenv::createImmutable('../');
$dotenv->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;
}
}
}

View file

@ -1,6 +1,8 @@
<?php
use Runtime\Bootstrap;
use Runtime\Config\Config;
use Runtime\Config\Env;
use Runtime\Contracts\Container\Container;
use Runtime\Factory\BootstrapFactory;
use Runtime\Http\Input\InputHandler;
@ -27,6 +29,19 @@ if(!function_exists('app'))
}
}
if(!function_exists('config'))
{
/**
* @param $file
* @param $path
* @return bool
*/
function config($file, $path)
{
return Config::get($file, $path);
}
}
if(!function_exists('csrf_token'))
{
/**
@ -73,6 +88,18 @@ if(!function_exists('endsWith'))
}
}
if(!function_exists('env'))
{
/**
* @param string $key
* @return bool
*/
function env($key)
{
return Env::get($key);
}
}
if(!function_exists('input'))
{
/**