forked from directus/v8-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for different config sources (directus#1048)
* Adds configuration schemas, tests and benchmark code * Rename file context, add tests as an example on how to put it to work * Core integration Fixed settings being removed from config Added support for stdout/stdout logger outputs Added some config exceptions Fixed a bug on group defaults Added support for loading configs from ENV Unified how app is created Added schema tests * Fixed unknown project config loading
- Loading branch information
1 parent
92e77c5
commit 289f475
Showing
26 changed files
with
1,098 additions
and
25 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
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,37 @@ | ||
<?php | ||
|
||
use Directus\Config\Source; | ||
|
||
|
||
class SourceBench | ||
{ | ||
/** | ||
* @Revs(10000) | ||
* @Iterations(3) | ||
*/ | ||
public function benchEnv() | ||
{ | ||
$_ENV = [ | ||
"A" => 1 | ||
]; | ||
Context::from_env(); | ||
} | ||
|
||
/** | ||
* @Revs(10000) | ||
* @Iterations(3) | ||
*/ | ||
public function benchFile() | ||
{ | ||
Context::from_file(__DIR__ . "/../../../tests/api/Config/sources/source.php"); | ||
} | ||
|
||
/** | ||
* @Revs(10000) | ||
* @Iterations(3) | ||
*/ | ||
public function benchJson() | ||
{ | ||
Context::from_json(__DIR__ . "/../../../tests/api/Config/sources/source.json"); | ||
} | ||
} |
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
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,20 @@ | ||
{ | ||
"bootstrap": "vendor/autoload.php", | ||
"path": "benchmarks", | ||
"time_unit": "milliseconds", | ||
"outputs": { | ||
"directus": { | ||
"extends": "html", | ||
"file": "benchmark.html", | ||
"title": "Directus Benchmarks" | ||
} | ||
}, | ||
"reports": { | ||
"directus": { | ||
"extends": "aggregate" | ||
} | ||
}, | ||
"php_config": { | ||
"variables_order": "EGPCS" | ||
} | ||
} |
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
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
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,108 @@ | ||
<?php | ||
|
||
namespace Directus\Config; | ||
|
||
/** | ||
* Config context interface | ||
*/ | ||
class Context | ||
{ | ||
/** | ||
* Transforms an array of strings into a complex object | ||
* @example | ||
* $obj = Context::expand(['a', 'b', 'c'], 12345); | ||
* $obj == [ | ||
* 'a' => [ | ||
* 'b' => [ | ||
* 'c' => 12345 | ||
* ] | ||
* ] | ||
* ]; | ||
*/ | ||
private static function expand(&$target, $path, $value) | ||
{ | ||
$segment = array_shift($path); | ||
if (sizeof($path) === 0) { // leaf | ||
if (!is_array($target)) { | ||
// TODO: raise warning - overwriting value | ||
$target = []; | ||
} | ||
if (array_key_exists($segment, $target)) { | ||
// TODO: raise warning - overwriting group | ||
} | ||
$target[$segment] = $value; | ||
return; | ||
} | ||
if (!isset($target[$segment])) { | ||
$target[$segment] = []; | ||
} | ||
Context::expand($target[$segment], $path, $value); | ||
} | ||
|
||
/** | ||
* Normalize the array indexes | ||
*/ | ||
private static function normalize(&$target) { | ||
if (!is_array($target)) { | ||
return; | ||
} | ||
|
||
$sort = false; | ||
foreach ($target as $key => $value) { | ||
Context::normalize($target[$key]); | ||
$sort |= is_numeric($key); | ||
} | ||
|
||
if ($sort) { | ||
// TODO: which one? | ||
sort($target, SORT_NUMERIC); | ||
// vs. | ||
//$target = array_values($target); | ||
} | ||
} | ||
|
||
/** | ||
* Source | ||
*/ | ||
public static function from_map($source) { | ||
$target = []; | ||
ksort($source); | ||
foreach ($source as $key => $value) { | ||
Context::expand($target, explode('_', strtolower($key)), $value); | ||
} | ||
Context::normalize($target); | ||
return $target; | ||
} | ||
|
||
/** | ||
* Create | ||
*/ | ||
public static function from_env() | ||
{ | ||
if (empty($_ENV)) { | ||
throw new \Error('No environment variables available. Check php_ini "variables_order" value.'); | ||
} | ||
return Context::from_map($_ENV); | ||
} | ||
|
||
/** | ||
* Loads variables from PHP file | ||
*/ | ||
public static function from_file($file) { | ||
return require $file; | ||
} | ||
|
||
/** | ||
* Loads variables from PHP file | ||
*/ | ||
public static function from_array($array) { | ||
return $array; | ||
} | ||
|
||
/** | ||
* Loads variables from JSON file | ||
*/ | ||
public static function from_json($file) { | ||
return json_decode(file_get_contents($file)); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/core/Directus/Config/Exception/InvalidProjectException.php
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,22 @@ | ||
<?php | ||
|
||
namespace Directus\Config\Exception; | ||
|
||
use Directus\Exception\ErrorException; | ||
|
||
class InvalidProjectException extends ErrorException | ||
{ | ||
const ERROR_CODE = 22; | ||
|
||
public function __construct($previous = null) | ||
{ | ||
$message = 'Current configuration context only supports default project name (_)'; | ||
|
||
parent::__construct($message, static::ERROR_CODE, $previous); | ||
} | ||
|
||
public function getStatusCode() | ||
{ | ||
return 400; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/core/Directus/Config/Exception/UnknownProjectException.php
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 @@ | ||
<?php | ||
|
||
namespace Directus\Config\Exception; | ||
|
||
use Directus\Exception\ErrorException; | ||
|
||
class UnknownProjectException extends ErrorException | ||
{ | ||
const ERROR_CODE = 22; | ||
|
||
public function __construct($project, $previous = null) | ||
{ | ||
$message = "Unknown project: ${project}"; | ||
|
||
parent::__construct($message, static::ERROR_CODE, $previous); | ||
} | ||
} |
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,116 @@ | ||
<?php | ||
|
||
namespace Directus\Config\Schema; | ||
|
||
/** | ||
* Group node | ||
*/ | ||
abstract class Base implements Node | ||
{ | ||
/** | ||
* Node key | ||
* @var string | ||
*/ | ||
private $_key = null; | ||
|
||
/** | ||
* Node name | ||
* @var string | ||
*/ | ||
private $_name = null; | ||
|
||
/** | ||
* Node children | ||
* @var Node[] | ||
*/ | ||
private $_children = null; | ||
|
||
/** | ||
* Node parent | ||
* @var Node | ||
*/ | ||
private $_parent = null; | ||
|
||
/** | ||
* Node is optional | ||
* @var boolean | ||
*/ | ||
private $_optional = null; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public function __construct($name, $children) | ||
{ | ||
$this->_optional = substr($name, -1) == '?'; | ||
if ($this->_optional) { | ||
$name = substr($name, 0, -1); | ||
} | ||
$this->_key = str_replace('_', '', strtolower($name)); | ||
$this->_name = strtolower($name); | ||
$this->_children = $children; | ||
$this->_parent = null; | ||
foreach ($children as &$child) { | ||
$child->parent($this); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the node key | ||
* @return string | ||
*/ | ||
public function key() | ||
{ | ||
return $this->_key; | ||
} | ||
|
||
/** | ||
* Returns the node name | ||
* @return string | ||
*/ | ||
public function name() | ||
{ | ||
return $this->_name; | ||
} | ||
|
||
/** | ||
* Returns the parent node | ||
* @return Node | ||
*/ | ||
public function parent($value = false) | ||
{ | ||
if ($value !== false) { | ||
$this->_parent = $value; | ||
if ($this->_parent !== null) { | ||
if ($this->_parent->optional() === true) { | ||
$this->_optional = true; | ||
} | ||
} | ||
} | ||
return $this->_parent; | ||
} | ||
|
||
/** | ||
* Returns the children nodes | ||
* @return Node[] | ||
*/ | ||
public function children($value = false) | ||
{ | ||
if ($value !== false) { | ||
$this->_children = $value; | ||
} | ||
return $this->_children; | ||
} | ||
|
||
/** | ||
* Returns wether the node is optional or not | ||
* @return boolean | ||
*/ | ||
public function optional($value = null) | ||
{ | ||
if ($value !== null) { | ||
$this->_optional = $value; | ||
} | ||
return $this->_optional; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/core/Directus/Config/Schema/Exception/OmitException.php
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,10 @@ | ||
<?php | ||
|
||
namespace Directus\Config\Schema\Exception; | ||
|
||
use Directus\Exception\ErrorException; | ||
|
||
class OmitException extends ErrorException | ||
{ | ||
|
||
} |
Oops, something went wrong.