public function testMethod_reset_resets_TargetRoute_too() { TargetRoute::setAction('testclass'); $this->assertEqual('testclass', TargetRoute::getAction()); $this->router->reset(); // default module action is "list" // "list" was formerly "index" (Response Action for GET "/foos") $this->assertEqual('list', TargetRoute::getAction()); }
/** * Adds breadcrumbs dynamically based on current module, submodule and action. * This might look a bit rough to the user. * Please prefer adding breadcrumbs manually via add(). */ public static function addDynamicBreadcrumbs() { $moduleName = strtolower(TargetRoute::getModuleName()); $submoduleName = strtolower(TargetRoute::getSubModuleName()); $actionName = TargetRoute::getActionNameWithoutPrefix(); if (isset($moduleName) and $moduleName !== 'controlcenter') { $url = 'index.php?mod=' . $moduleName; // Level 2 if ($submoduleName !== '') { $url .= '&sub=' . $submoduleName; $moduleName .= ' ' . $submoduleName; } self::add($moduleName, $url); // Level 3 if ($actionName !== '') { $url .= '&action=' . $actionName; self::add($actionName, $url); } } }
/** * Adds breadcrumbs dynamically based on current module, submodule and action. * This might look a bit rough to the user. * Please prefer adding breadcrumbs manually via add(). */ public static function addDynamicBreadcrumbs() { $module = strtolower(TargetRoute::getModule()); $controller = strtolower(TargetRoute::getController()); $action = TargetRoute::getActionNameWithoutPrefix(); if (isset($module) and $module !== 'controlcenter') { $url = 'index.php?mod=' . $module; // Level 2 // do not add ctrl part, if controller and module are the same if ($controller !== '' and $controller !== $module) { $url .= '&ctrl=' . $controller; $module .= ' ' . $controller; } self::add($module, $url); // Level 3 if ($action !== '') { $url .= '&action=' . $action; self::add($action, $url); } } }
public function executeFilter(HttpRequestInterface $request, HttpResponseInterface $response) { $modulename = TargetRoute::getController(); $this->locale->loadTextDomain('LC_ALL', $modulename, $this->locale->getLocale(), $modulename); }
/** * Renderer_Smarty->render. * * Returns the mainframe layout with inserted modulcontent (templatename). * * 1. assign common values and constants * 2. fetch the modultemplate and assigns it as $content * 3. return the wrapper layout tpl * * @param string $template Template Filename * * @return null|string tpl layout */ public function render($template = null, $viewdata = null) { if ($viewdata !== null) { $this->assign($viewdata); } // assign common template values and Application constants as Smarty Template Variables. $constants = $this->getConstants(); foreach ($constants as $const => $value) { $this->renderer->assignGlobal($const, $value); } /* * Assign the original template name and the requested module * This is used in template_not_found.tpl to provide a link to the templateeditor */ $this->renderer->assignGlobal('modulename', TargetRoute::getModule()); $this->renderer->assignGlobal('actionname', TargetRoute::getActionName()); $this->renderer->assignGlobal('templatename', $template); /* * Rendering depends on the RenderMode. * * The RenderMode "PARTIAL" means, that only the content template is rendered. * * The RenderMode "LAYOUT" means, that the content template is embedded, * into a layout template, by replacing the {$content} placeholder. */ if ($this->getRenderMode() === 'PARTIAL') { return $this->fetch($template); } if ($this->getRenderMode() === 'LAYOUT') { // assign the modulecontent $this->assign('content', $this->fetch($template)); return $this->fetch($this->getLayoutTemplate()); } }
/** * Write module configuration file. * * @param $array The configuration array to write. * @param string $module The name of a module. * * @return bool */ public function writeModuleConfig(array $array, $module = null) { // if no modulename is set, determine the name of the current module $module = $module === null ? \Koch\Router\TargetRoute::getModule() : ucfirst($module); $file = realpath(APPLICATION_MODULES_PATH . $module . DIRECTORY_SEPARATOR . $module . '.config.php'); return $this->write($file, $array); }
/** * Renderer_Smarty->render * * Returns the mainframe layout with inserted modulcontent (templatename). * * 1. assign common values and constants * 2. fetch the modultemplate and assigns it as $content * 3. return the wrapper layout tpl * * @param string $templatename Template Filename * @param array|object $data Data to assign to the view. * @return wrapper tpl layout */ public function render($template, $viewdata = null) { if ($viewdata !== null) { $this->assign($viewdata); } // 1. assign common template values and Application constants as Smarty Template Variables. $this->renderer->assignGlobal($this->getConstants()); /** * Assign the original template name and the requested module * This is used in template_not_found.tpl to provide a link to the templateeditor */ $this->renderer->assignGlobal('modulename', TargetRoute::getModuleName()); $this->renderer->assignGlobal('actionname', TargetRoute::getActionName()); $this->renderer->assignGlobal('templatename', $template); /** * Rendering depends on the RenderMode. * * RenderMode "NOLAYOUT" means that only the (module) content template is rendered. * * RenderMode "LAYOUT" means that the (module) content template is embedded, * into a layout template, by replacing the {$content} placeholder. */ if ($this->getRenderMode() === 'NOLAYOUT') { return $this->fetch($template); } if ($this->getRenderMode() === 'LAYOUT') { // ensure that smarty tags {$content} and {copyright} are present in the layout template #if(true === $this->preRenderChecks()) #{ // assign the modulecontent $this->assign('content', $this->fetch($template)); return $this->fetch($this->getLayoutTemplate()); #} } }
/** * Smarty Currentmodule. * * Displays the name of the current module * * Examples: * <pre> * {pagination} * </pre> * * Type: function<br> * Name: currentmodule<br> * Purpose: display name of current module<br> * * @return string */ function Smarty_function_currentmodule() { return \Koch\Router\TargetRoute::getModule(); }
/** * @todo Implement testAddDynamicBreadcrumbs(). */ public function testAddDynamicBreadcrumbs() { /** * case A - normal module - frontend access => module = news, action = action_show * * expected path = Home >> News >> Show */ // add Level 1 - Home $this->object->resetBreadcrumbs(); $this->object->initialize(); TargetRoute::reset(); TargetRoute::setController('news'); TargetRoute::setAction('action_show'); // fetch with dynamical trail building $t_array = $this->object->getTrail(true); #var_dump($t_array); // Level 1 - expected Home $this->assertIdentical('Home', $t_array[0]['title']); $this->assertIdentical(WWW_ROOT, $t_array[0]['link']); // Level 2 - Modulename News $this->assertIdentical('News', $t_array[1]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=news', $t_array[1]['link']); // Level 3 - Action Show $this->assertIdentical('Show', $t_array[2]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=news&action=show', $t_array[2]['link']); /** * case B - normal module - backend access => module = news, sub = admin, action = action_admin_show * * expected path = Controlcenter >> News Admin >> Show */ // add Level 1 - Home $this->object->resetBreadcrumbs(); $this->object->initialize('news', 'admin'); TargetRoute::reset(); TargetRoute::setController('news'); TargetRoute::setSubController('admin'); TargetRoute::setAction('action_show'); // fetch with dynamical trail building $t_array = $this->object->getTrail(true); #var_dump($t_array); // Level 1 - expected $this->assertIdentical('Control Center', $t_array[0]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=controlcenter', $t_array[0]['link']); // Level 2 - Modulename News $this->assertIdentical('News Admin', $t_array[1]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=news&sub=admin', $t_array[1]['link']); // Level 3 - Action Show $this->assertIdentical('Show', $t_array[2]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=news&sub=admin&action=show', $t_array[2]['link']); /** * case c - Control Center => module = controlcenter, action = show * * expected path = Controlcenter */ // add Level 1 - Home $this->object->resetBreadcrumbs(); $this->object->initialize('controlcenter'); TargetRoute::reset(); TargetRoute::setController('news'); TargetRoute::setSubController(null); TargetRoute::setAction('show'); // fetch with dynamical trail building $t_array = $this->object->getTrail(true); #var_dump($t_array); // Level 1 - expected $this->assertIdentical('Control Center', $t_array[0]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=controlcenter', $t_array[0]['link']); // Level 2 - Modulename News $this->assertIdentical('News', $t_array[1]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=news', $t_array[1]['link']); // Level 3 - Action Show $this->assertIdentical('Show', $t_array[2]['title']); $this->assertIdentical(WWW_ROOT . 'index.php?mod=news&action=show', $t_array[2]['link']); }
/** * Returns Module Template Paths. * * @param string $module * * @return string[] Module Template Paths */ public static function getModuleTemplatePaths($module = null) { // fetch modulename for template path construction if (empty($module)) { $module = TargetRoute::getModule(); } // fetch renderer name for template path construction $renderer = TargetRoute::getRenderEngine(); // compose templates paths in the module dir $module_paths = [APPLICATION_MODULES_PATH, APPLICATION_MODULES_PATH . $module . DIRECTORY_SEPARATOR, APPLICATION_MODULES_PATH . $module . '/View/', APPLICATION_MODULES_PATH . $module . '/View/' . ucfirst($renderer) . DIRECTORY_SEPARATOR]; return $module_paths; }
/** * Matches the URI against the Routes Mapping Table. * Taking static, dynamic and regexp routings into account. * In other words, it "map matches the URI". * * @return TargetRoute|null */ public function match() { // do we have some routes now? if (0 === count($this->routes)) { throw new \OutOfBoundsException('The routes lookup table is empty. Define some routes.'); } /* * Detects if Mod_Rewrite engine is active and * calls the proper URL Parser/Segmentizer method for the extraction of uri segments. */ if ($this->isRewriteEngineOn() or isset($_ENV['FORCE_MOD_REWRITE_ON']) and true === empty($_GET['mod']) and true === empty($_GET['ctrl'])) { $this->uriSegments = $this->parseUrlRewrite($this->uri); } else { $this->uriSegments = $this->parseUrlNoRewrite($this->uri); $this->uriSegments = self::fixNoRewriteShorthands($this->uriSegments); $targetRoute = TargetRoute::setSegmentsToTargetRoute($this->uriSegments); if ($targetRoute::dispatchable()) { return $targetRoute; } } /* * Reduce the map lookup table, by dropping all routes * with more segments than the current requested uri. */ if (count($this->routes) > 1 and count($this->uriSegments) >= 1) { self::reduceRoutesToSegmentCount(); } /* * Process: Static Route * * Do we have a direct match ? * This matches "static routes". Without any preg_match overhead. * * Example: * The request URI "/news/index" relates 1:1 to $routes['/news/index']. * The request URI "/login" relates 1:1 to $routes['/login'] */ if (isset($this->routes[$this->uri])) { // we have a direct match $found_route = $this->routes[$this->uri]; // return the TargetRoute object return TargetRoute::setSegmentsToTargetRoute($found_route); } else { /* * No, there wasn't a 1:1 match. * Now we have to check the uri segments. * * Let's loop over the remaining routes and try to map match the uri_segments. */ foreach ($this->routes as $route_pattern => $route_values) { unset($route_pattern); $matches = ''; /** * Process: Dynamic Regular Expression Parameters. * * Example: * URI: /news * Rule /:controller * Regexp: "#(?P<controller>[a-z0-9_-]+)\/?#" * Matches: $matches['controller'] = 'news'; */ if (1 === preg_match($route_values['regexp'], $this->uri, $matches)) { // matches[0] contains $this->uri unset($matches[0]); // remove duplicate values // e.g. [controller] = news // [1] = news $matches = array_unique($matches); # @todo # fetch key and its position from $route_values['requirements'] if (count($route_values['requirements']) > 0) { foreach ($route_values['requirements'] as $array_position => $key_name) { // insert a new key // with name from requirements array // and value from matches array // ([id] => 42) $pos = $array_position + 1; $matches[$key_name] = $matches[$pos]; // remove the old not-named key ([2] => 42) unset($matches[$pos]); } } // insert $matches[<controller>] etc TargetRoute::setSegmentsToTargetRoute($matches); } if (TargetRoute::dispatchable()) { // route found, stop foreach break; } else { TargetRoute::reset(); continue; } } } return TargetRoute::instantiate(); }
/** * Returns the object Koch_Theme for accessing Configuration Values. * * @return object Koch_Theme */ public function getTheme() { if ($this->theme === null) { $theme = \Koch\Router\TargetRoute::getThemeName(); $this->theme = new \Koch\View\Helper\Theme($theme); } return $this->theme; }
/** * Returns Module Template Paths * * @return array Module Template Paths */ public static function getModuleTemplatePaths() { // fetch modulename for template path construction $module = TargetRoute::getModuleName(); // fetch renderer name for template path construction $renderer = HttpRequest::getRoute()->getRenderEngine(); // compose templates paths in the module dir $module_paths = array(ROOT_MOD, ROOT_MOD . $module . DIRECTORY_SEPARATOR, ROOT_MOD . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR, ROOT_MOD . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . $renderer . DIRECTORY_SEPARATOR); return $module_paths; }