Permalink
Browse files

Configuration proposal with mergeable interface

  • Loading branch information...
wdalmut committed Dec 29, 2013
1 parent 94e4773 commit 6d695f684806b21d0f01f82286763223f59b80d4
@@ -0,0 +1,27 @@
<?php
namespace UpCloo\App\Config;
class ArrayProcessor implements Mergeable
{
private $configs;
public function __construct()
{
$this->configs = [];
}
public function appendConfig(array $config)
{
$this->configs[] = $config;
}
public function merge()
{
$resultConf = [];
foreach ($this->configs as $confFile) {
$resultConf = array_replace_recursive($resultConf, $confFile);
}
return $resultConf;
}
}
@@ -0,0 +1,10 @@
<?php
namespace UpCloo\App\Config;
interface Mergeable
{
/**
* @return array The configured single array
*/
public function merge();
}
@@ -0,0 +1,42 @@
<?php
namespace UpCloo\App\Config;
abstract class Skeleton
{
public static final function getBaseConfig()
{
return [
"router" => [],
"services" => [
"invokables" => [
"UpCloo\\Listener\\Renderer\\Json" => "UpCloo\\Listener\\Renderer\\Json",
"UpCloo\\Listener\\Renderer\\Jsonp" => "UpCloo\\Listener\\Renderer\\Jsonp",
"Zend\\Stdlib\\Hydrator\\ClassMethods" => "Zend\\Stdlib\\Hydrator\\ClassMethods",
"UpCloo\\Listener\\SendResponseListener" => "UpCloo\\Listener\\SendResponseListener",
],
"factories" => [
"UpCloo\\Listener\\RouteListener" => "UpCloo\\Service\\RouteListenerFactory",
],
"aliases" => [
"renderer.listener" => "UpCloo\\Listener\\Renderer\\Jsonp",
"route.listener" => "UpCloo\\Listener\\RouteListener",
"response.listener" => "UpCloo\\Listener\\SendResponseListener",
"hydrator" => "Zend\\Stdlib\\Hydrator\\ClassMethods",
]
],
"listeners" => [
"route" => [
["route.listener", "prepareControllerToBeExecuted"]
],
"pre.fetch" => [],
"execute" => [],
"renderer" => [
["renderer.listener", "render"]
],
"send.response" => [
["response.listener", "sendResponse"]
],
]
];
}
}
@@ -0,0 +1,25 @@
<?php
namespace UpCloo\App\Config;
class ArrayProcessorTest extends \PHPUnit_Framework_TestCase
{
public function testMergeConfigs()
{
$configurable = new ArrayProcessor();
$configurable->appendConfig([
"one" => []
]);
$configurable->appendConfig([
"one" => [
"two" => "three"
]
]);
$configured = $configurable->merge();
$this->assertInternalType("array", $configured);
$this->assertArrayHasKey("one", $configured);
$this->assertInternalType("array", $configured["one"]);
$this->assertEquals("three", $configured["one"]["two"]);
}
}

0 comments on commit 6d695f6

Please sign in to comment.