Ejemplo n.º 1
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);
 }
Ejemplo n.º 2
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;
 }