/** * 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; }
/** * 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); }