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++; } }
private function log_with_helper($prefix, $message, $loglevel) { // Temporary non-Midgard logger until midgard_error is backported to Ragnaroek static $logger = null; if (!$logger) { try { $logger = new midgardmvc_core_helpers_log(); } catch (Exception $e) { // Unable to instantiate logger return; } } static $log_levels = array('debug' => 4, 'info' => 3, 'message' => 2, 'warn' => 1); if ($log_levels[$loglevel] > $log_levels[$this->configuration->get('log_level')]) { // Skip logging, too low level return; } $logger->log("{$prefix}: {$message}"); }
private function _process() { $this->context->create(); date_default_timezone_set($this->configuration->get('default_timezone')); $this->dispatcher->get_midgard_connection()->set_loglevel($this->configuration->get('log_level')); // Let dispatcher populate request with the page and other information used $request = $this->dispatcher->get_request(); $request->populate_context(); if (isset($this->context->page->guid)) { // Load per-folder configuration $this->configuration->load_instance($this->context->component, $this->context->page); } $this->log('Midgard MVC', "Serving " . $request->get_method() . " {$this->context->uri} at " . gmdate('r'), 'info'); // Let injectors do their work $this->componentloader = new midgardmvc_core_component_loader(); $this->componentloader->inject_process(); // Load the cache service and check for content cache self::$instance->context->cache_enabled = false; /* if (self::$instance->context->cache_enabled) { $request->generate_identifier(); $this->cache->register_object($this->context->page); $this->cache->content->check($this->context->cache_request_identifier); } */ // Show the world this is Midgard $this->head->add_meta(array('name' => 'generator', 'content' => "Midgard/" . mgd_version() . " MidgardMVC/{$this->componentloader->manifests['midgardmvc_core']['version']} PHP/" . phpversion())); if ($this->configuration->enable_attachment_cache) { $classname = $this->configuration->attachment_handler; $handler = new $classname(); $handler->connect_to_signals(); } // Then initialize the component, so it also goes to template stack $this->dispatcher->initialize($request); try { $this->dispatcher->dispatch(); } catch (midgardmvc_exception_unauthorized $exception) { // Pass the exception to authentication handler $this->authentication->handle_exception($exception); } $this->dispatcher->header('Content-Type: ' . $this->context->mimetype); }
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; }
public function get_routes(array $args) { $this->midgardmvc->authorization->require_user(); $this->prepare_component($args['component'], $this->data); $configuration = new midgardmvc_core_services_configuration_yaml($this->data['component']); $this->data['routes'] = $configuration->get('routes'); if (!$this->data['routes']) { throw new midgardmvc_exception_notfound("Component {$this->data['component']} has no routes"); } foreach ($this->data['routes'] as $route_id => $route_def) { // Some normalization $this->data['routes'][$route_id]['id'] = $route_id; if (!isset($route_def['template_entry_point'])) { $this->data['routes'][$route_id]['template_entry_point'] = 'ROOT'; } if (!isset($route_def['content_entry_point'])) { $this->data['routes'][$route_id]['content_entry_point'] = 'content'; } $this->data['routes'][$route_id]['controller_action'] = "{$route_def['controller']}:{$route_def['action']}"; $this->data['routes'][$route_id]['controller_url'] = $this->midgardmvc->dispatcher->generate_url('midcom_documentation_class', array('class' => $route_def['controller'])); $this->data['routes'][$route_id]['controller_url'] .= "#action_{$route_def['action']}"; } }