From 791414e5a7b5cb61629bd983c201337d863aba23 Mon Sep 17 00:00:00 2001 From: Jakob Kruse Date: Tue, 20 Mar 2018 17:40:52 +0100 Subject: [PATCH] init --- blueprints.yaml | 8 ++++++ composer.json | 17 ++++++++++++ dotenv.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ dotenv.yaml | 1 + 4 files changed, 97 insertions(+) create mode 100644 blueprints.yaml create mode 100644 composer.json create mode 100644 dotenv.php create mode 100644 dotenv.yaml diff --git a/blueprints.yaml b/blueprints.yaml new file mode 100644 index 0000000..d0d02f4 --- /dev/null +++ b/blueprints.yaml @@ -0,0 +1,8 @@ +name: DotEnv +version: 1.0.0 +description: Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically. +icon: exclamation-circle +author: + name: Jakob Kruse + email: jkonliner@gmail.com + url: http://zenbu.ai diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4c6fdca --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "ralla/grav-plugin-dotenv", + "type": "project", + "description": "DotEnv support for Grav", + "keywords": ["cms","flat-file cms","flat cms","flatfile cms","php","dotenv"], + "license": "MIT", + "require": { + "adbario/php-dot-notation": "^2.0", + "vlucas/phpdotenv": "^2.4" + }, + "authors": [ + { + "name": "Jakob Kruse", + "email": "jkonliner@gmail.com" + } + ] +} diff --git a/dotenv.php b/dotenv.php new file mode 100644 index 0000000..e96d102 --- /dev/null +++ b/dotenv.php @@ -0,0 +1,71 @@ + ['onPluginsInitialized', 0], + ]; + } + + public function onPluginsInitialized() + { + $this->dotenv = new Dotenv(GRAV_ROOT, '.gravenv'); + + try { + $this->init(); + } catch (\Exception $e) { + $this->grav['debugger']->addMessage('DotEnv: ' . $e->getMessage()); + } + } + + protected function init() + { + foreach ($this->dotenv->load() as $setting) { + $settingData = $this->getSetting($setting); + + $this->grav['config']->join($settingData['grav_pointer'], [ + $settingData['key'] => getenv($settingData['env_name']), + ]); + } + } + + protected function getSetting($setting) + { + if (strpos($setting, '=') !== false) { + list($name, $value) = array_map('trim', explode('=', $setting, 2)); + } + + // Name is not dot notated, it has to be. + if (strpos($name, '.') === false) { + return; + } + + $parts = explode('.', $name); + + return [ + 'grav_pointer' => implode('.', array_slice($parts, 0, -1)), + 'env_name' => implode('.', $parts), + 'key' => end($parts), + 'value' => $value, + ]; + } +} diff --git a/dotenv.yaml b/dotenv.yaml new file mode 100644 index 0000000..d4ca941 --- /dev/null +++ b/dotenv.yaml @@ -0,0 +1 @@ +enabled: true