public function test_merge_config_routes()
 {
     $base = array('foo' => 1, 'routes' => array('one' => array(), 'two' => array('val' => false)));
     $extension = array('bar' => 1, 'routes' => array('three' => array(), 'four' => array(), 'two' => array('val' => true)));
     $result = midgardmvc_core_services_configuration_yaml::merge_configs($base, $extension);
     $i = 0;
     foreach ($result as $key => $val) {
         if ($key == 'foo') {
             $this->assertEquals($i, 0, '"foo" from base configuration should be first in the merged config');
         }
         if ($key == 'bar') {
             $this->assertEquals($i, 2, '"bar" from extended configuration should be third in the merged config');
         }
         if ($key == 'routes') {
             $ii = 0;
             foreach ($val as $route_id => $route_definition) {
                 if ($route_id == 'three') {
                     $this->assertEquals($ii, 0, 'Route "three" should be first route in array');
                 }
                 if ($route_id == 'four') {
                     $this->assertEquals($ii, 1, 'Route "four" should be second route in array');
                 }
                 if ($route_id == 'two') {
                     $this->assertEquals($ii, 2, 'Route "two" should be third route in array');
                     $this->assertTrue($route_definition['val'], '"val" of route "two" should come from the extended configuration, not the base one');
                 }
                 if ($route_id == 'one') {
                     $this->assertEquals($ii, 3, 'Route "one" should be fourth route in array ' . serialize($val));
                 }
                 $ii++;
             }
         }
         $i++;
     }
 }
Exemple #2
0
 public static function merge_configs(array $base, array $extension)
 {
     if (empty($base)) {
         // Original was empty, no need to merge
         return $extension;
     }
     $merged = $base;
     foreach ($extension as $key => $value) {
         if (is_array($value)) {
             if (!isset($merged[$key]) || empty($merged[$key])) {
                 // Original was empty, no need to merge
                 $merged[$key] = $value;
                 continue;
             }
             if ($key == 'routes') {
                 /* 
                  * Routes have special handling to ensure routes from current component
                  * and injectors are run before core routes.
                  *
                  * Routes also don't merge but instead override fully.
                  */
                 $merged[$key] = array();
                 foreach ($extension[$key] as $route_id => $route_definition) {
                     $merged[$key][$route_id] = $route_definition;
                     if (isset($base[$key][$route_id])) {
                         unset($base[$key][$route_id]);
                     }
                 }
                 foreach ($base[$key] as $route_id => $route_definition) {
                     $merged[$key][$route_id] = $route_definition;
                 }
             }
             $merged[$key] = midgardmvc_core_services_configuration_yaml::merge_configs($merged[$key], $value);
             continue;
         }
         $merged[$key] = $value;
     }
     return $merged;
 }