-
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.
- Loading branch information
1 parent
e7f50b0
commit ff19620
Showing
27 changed files
with
3,412 additions
and
2 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,2 @@ | ||
coverage_clover: tests/_output/coverage.xml | ||
json_path: tests/_output/coveralls-upload.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
vendor | ||
composer.lock | ||
tests/_output/* | ||
|
||
tests/_output/* |
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,26 @@ | ||
language: php | ||
|
||
php: | ||
- 5.6 | ||
- 7.1 | ||
|
||
matrix: | ||
allow_failures: | ||
- php: 7.1 | ||
|
||
cache: | ||
directories: | ||
- vendor | ||
- $HOME/.composer/cache | ||
|
||
install: | ||
- composer global require "fxp/composer-asset-plugin:~1.1.1" | ||
- composer self-update | ||
- composer install --prefer-dist | ||
- ulimit -c unlimited || true | ||
|
||
script: | ||
- php vendor/bin/codecept run unit --coverage --coverage-xml | ||
|
||
after_success: | ||
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;' |
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,27 @@ | ||
actor: Tester | ||
paths: | ||
tests: tests | ||
log: tests/_output | ||
data: tests/_data | ||
support: tests/_support | ||
envs: tests/_envs | ||
settings: | ||
bootstrap: _bootstrap.php | ||
colors: true | ||
memory_limit: 1024M | ||
extensions: | ||
enabled: | ||
- Codeception\Extension\RunFailed | ||
coverage: | ||
enabled: true | ||
remote: false | ||
include: | ||
- src/* | ||
exclude: | ||
modules: | ||
config: | ||
Db: | ||
dsn: '' | ||
user: '' | ||
password: '' | ||
dump: tests/_data/dump.sql |
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,30 @@ | ||
{ | ||
"name": "akademiano/config", | ||
"description": "Akademiano Config", | ||
"license": "Apache-2.0", | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"authors": [ | ||
{ | ||
"name": "Vasiliy Shvakin", | ||
"email": "mrdatamapper@gmail.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Akademiano\\Config\\": "src/" | ||
} | ||
}, | ||
|
||
"require": { | ||
"php": "^5.6 || ^7.0", | ||
"akademiano/utils": "^1.0.0-beta.1", | ||
"pimple/pimple": "^3.0" | ||
}, | ||
"require-dev": { | ||
"codeception/codeception": "^2.2.10", | ||
"satooshi/php-coveralls": "^1.0.1", | ||
"mockery/mockery": "^0.9.9" | ||
} | ||
|
||
} |
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,240 @@ | ||
<?php | ||
|
||
namespace Akademiano\Config; | ||
|
||
use Akademiano\Utils\ArrayTools; | ||
use Akademiano\Utils\Object\Collection; | ||
use Pimple\Container; | ||
|
||
class Config implements \ArrayAccess, \IteratorAggregate | ||
{ | ||
const DYN_CONF = "__dynamic__"; | ||
|
||
protected $configRaw; | ||
|
||
protected $childConfig = []; | ||
|
||
protected $environment; | ||
|
||
/** @var Container */ | ||
protected $diContainer; | ||
|
||
public function __construct(array $config, Container $diContainer = null) | ||
{ | ||
$this->set($config); | ||
if (null !== $diContainer) { | ||
$this->setDiContainer($diContainer); | ||
} | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getEnvironment() | ||
{ | ||
return $this->getDiContainer()["environment"]; | ||
} | ||
|
||
/** | ||
* @return DI | ||
*/ | ||
public function getDiContainer() | ||
{ | ||
return $this->diContainer; | ||
} | ||
|
||
/** | ||
* @param Container $diContainer | ||
*/ | ||
public function setDiContainer(Container $diContainer) | ||
{ | ||
$this->diContainer = $diContainer; | ||
} | ||
|
||
public function set($data, array $path = null) | ||
{ | ||
$this->childConfig = []; | ||
|
||
if (is_null($path)) { | ||
$this->configRaw = (array) $data; | ||
return; | ||
} | ||
$this->configRaw = ArrayTools::set($this->configRaw, $path, $data); | ||
} | ||
|
||
public function joinLeft($data) | ||
{ | ||
if ($data instanceof Config) { | ||
$data = $data->toArray(); | ||
} else { | ||
$data = (array) $data; | ||
} | ||
$this->configRaw = ArrayTools::mergeRecursive($data, $this->configRaw); | ||
$this->childConfig = []; | ||
return $this; | ||
} | ||
|
||
public function joinRight($data) | ||
{ | ||
if ($data instanceof Config) { | ||
$data = $data->toArray(); | ||
} else { | ||
$data = (array) $data; | ||
} | ||
$this->configRaw = ArrayTools::mergeRecursive($this->configRaw, $data); | ||
$this->childConfig = []; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|string $path | ||
* @param null $default | ||
* @return Config|mixed|null | ||
*/ | ||
public function get($path = null, $default = null) | ||
{ | ||
if (is_null($path)) { | ||
return $this; | ||
} | ||
$pathKey = implode('|', (array) $path); | ||
if (!isset($this->childConfig[$pathKey])) { | ||
if (!ArrayTools::issetByPath($this->configRaw, $path)) { | ||
return is_array($default) && !is_callable($default) ? new Config($default, $this->getDiContainer()) : $default; | ||
} | ||
$needConfig = ArrayTools::get($this->configRaw, $path, $default); | ||
if (is_array($needConfig)) { | ||
$firstElement = reset($needConfig); | ||
if (key($needConfig) === self::DYN_CONF && is_callable($firstElement)) { | ||
$needConfig = call_user_func($firstElement, $this->getDiContainer()); | ||
} | ||
} | ||
if (is_array($needConfig) && !is_callable($needConfig)) { | ||
$this->childConfig[$pathKey] = new Config($needConfig, $this->getDiContainer()); | ||
} else { | ||
$this->childConfig[$pathKey] = $needConfig; | ||
} | ||
} | ||
return $this->childConfig[$pathKey]; | ||
} | ||
|
||
public function getAndCall($path, $callback, array $arguments = null) | ||
{ | ||
if (ArrayTools::issetByPath($this->configRaw, $path)) { | ||
$value = $this->get($path); | ||
if (null !== $arguments) { | ||
array_unshift($arguments, $value); | ||
} | ||
else { | ||
$arguments = [$value]; | ||
} | ||
return call_user_func_array($callback, $arguments); | ||
} | ||
} | ||
|
||
public function getOrThrow($path) | ||
{ | ||
$data = $this->get($path); | ||
if (null === $data) { | ||
throw new \Exception("$path not found in config"); | ||
} | ||
return $data; | ||
} | ||
|
||
public function getOneIs(array $paths, $default = null) | ||
{ | ||
foreach ($paths as $path) { | ||
$data = $this->get($path); | ||
if ($data) { | ||
return $data; | ||
} | ||
} | ||
return $default; | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Whether a offset exists | ||
* @link http://php.net/manual/en/arrayaccess.offsetexists.php | ||
* @param mixed $offset <p> | ||
* An offset to check for. | ||
* </p> | ||
* @return boolean true on success or false on failure. | ||
* </p> | ||
* <p> | ||
* The return value will be casted to boolean if non-boolean was returned. | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
return isset($this->configRaw[$offset]); | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Offset to retrieve | ||
* @link http://php.net/manual/en/arrayaccess.offsetget.php | ||
* @param mixed $offset <p> | ||
* The offset to retrieve. | ||
* </p> | ||
* @return mixed Can return all value types. | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return ($this->offsetExists($offset)) ? $this->get($offset) : null; | ||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Offset to set | ||
* @link http://php.net/manual/en/arrayaccess.offsetset.php | ||
* @param mixed $offset <p> | ||
* The offset to assign the value to. | ||
* </p> | ||
* @param mixed $value <p> | ||
* The value to set. | ||
* </p> | ||
* @return void | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
$this->configRaw[$offset] = $value; | ||
if (isset($this->childConfig[$offset])) { | ||
unset($this->childConfig[$offset]); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* (PHP 5 >= 5.0.0)<br/> | ||
* Offset to unset | ||
* @link http://php.net/manual/en/arrayaccess.offsetunset.php | ||
* @param mixed $offset <p> | ||
* The offset to unset. | ||
* </p> | ||
* @return void | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
if (isset($this->configRaw[$offset])) { | ||
unset($this->configRaw[$offset]); | ||
} | ||
if (isset($this->childConfig[$offset])) { | ||
unset($this->childConfig[$offset]); | ||
} | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return (array)$this->configRaw; | ||
} | ||
|
||
public function toCollection() | ||
{ | ||
return new Collection($this->configRaw); | ||
} | ||
|
||
public function getIterator() | ||
{ | ||
return new \ArrayIterator($this->toArray()); | ||
} | ||
|
||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Akademiano\Config; | ||
|
||
|
||
interface ConfigInterface | ||
{ | ||
/** | ||
* @return Config|null | ||
*/ | ||
public function getConfig(); | ||
|
||
} |
Oops, something went wrong.