예제 #1
0
 public function initialize(midgardmvc_core_helpers_request $request)
 {
     // In main Midgard request we dispatch the component in connection to a page
     $this->midgardmvc->context->component = $request->get_component();
     $this->midgardmvc->context->component_instance = $this->midgardmvc->componentloader->load($this->midgardmvc->context->component);
     $this->midgardmvc->templating->prepare_stack($request);
 }
예제 #2
0
 /**
  * Parse request URL into components and return a corresponding MVC request object
  *
  * @return midgardmvc_core_helpers_request
  */
 public function get_request()
 {
     $request = new midgardmvc_core_helpers_request();
     $request->set_root_page($this->_root_page);
     $request->set_method($_SERVER['REQUEST_METHOD']);
     // Parse URL into components (Mjolnir doesn't do this for us)
     $url_components = parse_url("http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
     // Handle GET parameters
     if (!empty($url_components['query'])) {
         $get_parameters = array();
         parse_str($url_components['query'], $get_parameters);
         $request->set_query($get_parameters);
     }
     $request->resolve_page($url_components['path']);
     return $request;
 }
예제 #3
0
 public function test_dispatch_all()
 {
     return;
     // Go through the installed components
     foreach (midgardmvc_core::get_instance()->componentloader->manifests as $component_name => $manifest) {
         // Enter new context
         midgardmvc_core::get_instance()->context->create();
         try {
             $request = new midgardmvc_core_helpers_request();
             $request->set_component($component_name);
             midgardmvc_core::get_instance()->dispatcher->initialize($request);
         } catch (Exception $e) {
             echo "Skipping {$component_name}: component failed to load: " . $e->getMessage() . "\n";
             midgardmvc_core::get_instance()->context->delete();
             continue;
         }
         if (!midgardmvc_core::get_instance()->context->component_instance) {
             if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
                 echo "Skipping {$component_name}: component failed to load\n";
             }
             midgardmvc_core::get_instance()->context->delete();
             continue;
         }
         if (!midgardmvc_core::get_instance()->context->component_instance->configuration->exists('routes')) {
             // No routes in this component, skip
             if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
                 echo "Skipping {$component_name}: no routes\n";
             }
             midgardmvc_core::get_instance()->context->delete();
             continue;
         }
         if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
             echo "Running {$component_name}...\n";
         }
         $routes = midgardmvc_core::get_instance()->dispatcher->get_routes();
         foreach ($routes as $route_id => $route_configuration) {
             // Generate fake arguments
             preg_match_all('/\\{(.+?)\\}/', $route_configuration['route'], $route_path_matches);
             $route_string = $route_configuration['route'];
             $args = array();
             foreach ($route_path_matches[1] as $match) {
                 $args[$match] = 'test';
                 $route_string = str_replace("{{$match}}", "[{$match}: {$args[$match]}]", $route_string);
             }
             midgardmvc_core::get_instance()->dispatcher->set_route($route_id, $args);
             if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
                 echo "    {$route_id}: {$route_string}\n";
             }
             try {
                 midgardmvc_core::get_instance()->dispatcher->dispatch();
             } catch (Exception $e) {
                 if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
                     echo "        " . get_class($e) . ': ' . $e->getMessage() . "\n";
                 }
                 continue;
             }
             try {
                 if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
                     echo "        returned keys: " . implode(', ', array_keys(midgardmvc_core::get_instance()->context->{$component_name})) . "\n";
                 }
             } catch (Exception $e) {
                 if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
                     echo "        returned no data\n";
                 }
             }
         }
         // Delete the context
         midgardmvc_core::get_instance()->context->delete();
         if (MIDGARDMVC_TESTS_ENABLE_OUTPUT) {
             echo "\n";
         }
     }
     $this->assertTrue(true);
 }
예제 #4
0
 /**
  * Call a route of a component with given arguments and return the data it generated
  *
  * Dynamic calls may be called for either a specific page that has a component assigned to it
  * by specifying a page GUID or path as the first argument, or to a static instance of a component
  * by specifying component name as the first argument.
  *
  * Here is an example of using dynamic calls inside a TAL template, in this case loading three latest news:
  * 
  * <code>
  * <tal:block tal:define="latest_news php:midgardmvc.templating.dynamic_call('net_nemein_news', 'latest', array('number' => 3))">
  *     <ul tal:condition="latest_news/news">
  *         <li tal:repeat="article latest_news/news">
  *             <a href="#" tal:attributes="href article/url" tal:content="article/title">Headline</a>
  *         </li>
  *     </ul>
  * </tal:block>
  * </code>
  *
  * @param string $component_name Component name, page GUID or page path
  * @param string $route_id     Route identifier
  * @param array $arguments  Arguments to give to the route
  * @param boolean $switch_context Whether to run the route in a new context
  * @return $array data
  */
 public function dynamic_call($component_name, $route_id, array $arguments, $switch_context = true)
 {
     if (is_null($this->dispatcher)) {
         $this->dispatcher = new midgardmvc_core_services_dispatcher_manual();
     }
     if ($switch_context) {
         $this->midgardmvc->context->create();
     }
     $request = new midgardmvc_core_helpers_request();
     if (is_object($component_name) && is_a($component_name, 'midgardmvc_core_node')) {
         $request->set_page($component_name);
     } elseif (mgd_is_guid($component_name)) {
         $request->set_page(new midgardmvc_core_node($component_name));
     } elseif (strpos($component_name, '/') !== false) {
         $request->resolve_page($component_name);
     } else {
         $request->set_component($component_name);
     }
     // Copy HTTP request method of main context to the request
     $request->set_method($this->midgardmvc->context->get_item('request_method', 0));
     $request->populate_context();
     // Run process injector for this context too
     $this->midgardmvc->componentloader->inject_process();
     // Then initialize the component, so it also goes to template stack
     $this->dispatcher->initialize($request);
     $this->dispatcher->set_route($route_id, $arguments);
     $this->dispatcher->dispatch();
     $component_name = $this->midgardmvc->context->component;
     $data = $this->midgardmvc->context->{$component_name};
     if ($switch_context) {
         $this->midgardmvc->context->delete();
     }
     return $data;
 }
예제 #5
0
 /**
  * Generates an URL for given route_id with given arguments
  *
  * @param string $route_id the id of the route to generate a link for
  * @param array $args associative arguments array
  * @return string url
  */
 public function generate_url($route_id, array $args, midgardmvc_core_node $page = null, $component = null)
 {
     static $pages_for_component = array();
     if (is_null($page) && !is_null($component)) {
         if (!isset($pages_for_component[$component])) {
             // Find a page matching the requested component
             $qb = new midgard_query_builder('midgardmvc_core_node');
             $qb->add_constraint('component', '=', $component);
             $qb->begin_group('OR');
             $qb->add_constraint('up', 'INTREE', $this->midgardmvc->context->root_page->id);
             $qb->add_constraint('id', '=', $this->midgardmvc->context->root_page->id);
             $qb->end_group();
             $qb->set_limit(1);
             $pages = $qb->execute();
             if (empty($pages)) {
                 throw new OutOfBoundsException("No page matching component {$component} found");
             }
             $pages_for_component[$component] = $pages[0];
         }
         $page = $pages_for_component[$component];
     }
     if (!is_null($page)) {
         $this->midgardmvc->context->create();
         $request = new midgardmvc_core_helpers_request();
         $request->set_page($page);
         $request->populate_context();
         $this->initialize($request);
     }
     $route_definitions = $this->get_routes();
     if (!isset($route_definitions[$route_id])) {
         throw new OutOfBoundsException("route_id '{$route_id}' not found in routes configuration in context " . $this->midgardmvc->context->get_current_context());
     }
     $route = $route_definitions[$route_id]['route'];
     $link = $route;
     foreach ($args as $key => $value) {
         if (is_array($value)) {
             $value_array = array();
             foreach ($value as $part) {
                 if (empty($part)) {
                     continue;
                 }
                 $value_array[] = $part;
             }
             $value = implode('.', $value_array);
             // This is a token replacement, add the type hint
             $key = "token:{$key}";
         }
         $link = str_replace("{\${$key}}", $value, $link);
     }
     if (preg_match_all('%\\{$(.+?)\\}%', $link, $link_matches)) {
         throw new UnexpectedValueException("Missing arguments matching route '{$route_id}' of {$this->midgardmvc->core->component}: " . implode(', ', $link_remaining_args));
     }
     if (!is_null($page)) {
         $url = preg_replace('%/{2,}%', '/', $this->midgardmvc->context->uri . $link);
         $this->midgardmvc->context->delete();
         return $url;
     }
     return preg_replace('%/{2,}%', '/', $this->midgardmvc->context->uri . $link);
 }