Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Kruse committed Mar 20, 2018
1 parent 39db384 commit 791414e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 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
17 changes: 17 additions & 0 deletions 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"
}
]
}
71 changes: 71 additions & 0 deletions dotenv.php
@@ -0,0 +1,71 @@
<?php
namespace Grav\Plugin;

use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
use Dotenv\Dotenv;

class DotenvPlugin extends Plugin
{
/**
* DotEnv
*
* @var dotenv
*/
private $dotenv;

/**
* Subscribed events.
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['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,
];
}
}
1 change: 1 addition & 0 deletions dotenv.yaml
@@ -0,0 +1 @@
enabled: true

0 comments on commit 791414e

Please sign in to comment.