-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakob Kruse
committed
Mar 20, 2018
1 parent
39db384
commit 791414e
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enabled: true |