Skip to content
This repository has been archived by the owner on Dec 27, 2018. It is now read-only.

Commit

Permalink
Apply environment variables to Config. (#370)
Browse files Browse the repository at this point in the history
`PHPOOLE_SITE_TITLE="Title"` -> `$config->set('site.title', 'Title');`
  • Loading branch information
ArnaudLigny committed Nov 2, 2018
1 parent 3069c3b commit 176e3bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.0
2.8.0
21 changes: 21 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ public function __construct($config = null)
} elseif (is_array($config)) {
$data->import($config);
}

// Apply environment variables
$applyEnv = function ($array) use ($data) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveArrayIterator($array),
\RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $value) {
if (!$iterator->hasChildren()) {
for ($p = [], $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
$p[] = $iterator->getSubIterator($i)->key();
}
$path = implode('_', $p);
if ($getEnv = getenv('PHPOOLE_'.strtoupper($path))) {
$data->set(str_replace('_', '.', strtolower($path)), $getEnv);
}
}
}
};
$applyEnv($data->export());

$this->setFromData($data);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function testBuid()
PHPoole::create(
[
'site' => [
'menu' => [
'title' => 'PHPoole test',
'menu' => [
'main' => [
'index' => [
'id' => 'index',
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/website/layouts/include/header.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head lang="en">
<meta charset="UTF-8">
<title>{{ page.title }}</title>
<title>{{ page.title }} - {{ site.title }}</title>
<link rel="stylesheet" href="{{ url(minify('css/style.css')) }}" media="all" />
<script src="{{ url(minify('js/script.js')) }}" integrity="{{ hash('js/script.js') }}"></script>
</head>
Expand Down

0 comments on commit 176e3bd

Please sign in to comment.