/** * gets url for route by given params * * @param array $params * @param string $query * @param string $asterisk * @return string * @throws \InvalidArgumentException if route misses params */ public function getUrl(array $params = array(), $query = '', $asterisk = '') { $url = ''; // parse for placeholders in pattern $placeHolders = array(); if (preg_match_all('|' . self::PLACEHOLDER_PATTERN . '|', $this->pattern, $matches)) { $placeHolders = $matches[1]; } if ($placeHolders) { $defaultParams = array('module' => Request::DEFAULT_MODULE, 'controller' => Request::DEFAULT_CONTROLLER, 'action' => Request::DEFAULT_ACTION); $params = array_merge($defaultParams, $params); $url = Util::parsePattern($this->pattern, $params, '%', false); // check if optional placeholders are not replaced through parse $placeHoldersSubPattern = implode('|', $placeHolders); $regExpr = '\\([^\\(]?%(' . $placeHoldersSubPattern . ')%.*?\\)'; if (preg_match(':' . $regExpr . ':', $url, $matches, PREG_OFFSET_CAPTURE)) { $url = substr($url, 0, $matches[0][1]); } // check if mandatory placeholders are not replaced through parse if (preg_match_all(':%(' . $placeHoldersSubPattern . ')%:', $url, $matches)) { throw new \InvalidArgumentException("Missing params for getting url of route '{$this->pattern}': " . implode(', ', $matches[1])); } // remove brackets of optional parts $url = str_replace(array('(', ')'), '', $url); } // add query string if ($query) { $url .= '?' . $query; } // add asterisk if ($asterisk) { $url .= '#' . $asterisk; } return $url; }
/** * gets controller path * * @param string $moduleName * @param string $controllerName * @return string * * @throws HttpKernelException if controller directory is not a directory */ public function getControllerPath($moduleName, $controllerName) { $moduleNameCamelCased = Util::getCamelCased($moduleName); if (!in_array($moduleNameCamelCased, $this->modules, true)) { throw new HttpKernelException("Module '{$moduleNameCamelCased}' is not defined!"); } $params = array('controller' => $controllerName, 'module' => $moduleName); $controllerDir = Util::parsePattern($this->controllerPathPattern, $params); if (!is_dir($controllerDir)) { $controllerNameCamelCased = Util::getCamelCased($controllerName); throw new HttpKernelException("Controller path for module '{$moduleNameCamelCased}' and" . " controller '{$controllerNameCamelCased}' is not defined!" . ' Expected to be: ' . $controllerDir); } return $controllerDir; }
/** * gets action method * * @return string */ public function getActionMethod() { $actionMethod = Util::getCamelCased($this->request->getActionName()) . 'Action'; $actionMethod = lcfirst($actionMethod); return $actionMethod; }
/** * sets template path by request * * @param RequestInterface $request */ public function setTemplatePathByRequest(RequestInterface $request) { $params = array('action' => $request->getActionName(), 'controller' => $request->getControllerName(), 'module' => $request->getModuleName()); $templatePath = Util::parsePattern($this->viewPathPattern, $params); $this->getRenderer()->setTemplatePath($templatePath); }