/** * * */ public function __construct() { // set up references required in controllers $application = Application::getInstance(); $this->request = Request::createFromGlobals(); $this->currentDocument = basename($this->request->getScriptName()); if ($path = trim($this->request->getPathInfo(), '/')) { $this->pathSegments = explode('/', $path); } $this->config = $application->getConfig(); $this->route = $application->getCurrentRoute(); if (is_null($this->route)) { $this->route = Router::getRouteFromPathInfo(); } // skip script name if ($application->hasNiceUris() && $this->currentDocument != 'index.php') { array_shift($this->pathSegments); } // skip locale if one found if (count($this->pathSegments) && Application::getInstance()->hasLocale($this->pathSegments[0])) { array_shift($this->pathSegments); } $this->prepareForXhr(); }
/** * Constructor * * @param string $template filename * @param string $action attribute * @param string $submit method * @param string $encoding type * @param string $css class * @param string $misc string */ public function __construct($template = NULL, $action = FALSE, $method = 'POST', $type = FALSE) { $this->method = strtoupper($method); $this->type = $type; $this->tplFile = $template; $this->request = Request::createFromGlobals(); $this->setAction($action ? $action : $this->request->getRequestUri()); }
/** * callback to turn href shortcuts into site conform valid URLs * * $/foo/bar?baz=1 becomes /foo/bar?baz=1 or index.php/foo/bar?baz=1 * * @param array $matches * @return string */ private function filterHref($matches) { static $script; static $niceUri; if (empty($script)) { $script = trim(Request::createFromGlobals()->getScriptName(), '/'); } if (empty($niceUri)) { $niceUri = Application::getInstance()->getConfig()->site->use_nice_uris == 1; } $matches[4] = html_entity_decode($matches[4]); $uriParts = array(); if ($niceUri) { if ($script !== 'index.php') { $uriParts[] = basename($script, '.php'); } } else { $uriParts[] = $script; } if ($matches[3] !== '') { $uriParts[] = $matches[3]; } $uri = implode('/', $uriParts) . $matches[4]; return "<a{$matches[1]} href={$matches[2]}/{$uri}{$matches[2]}{$matches[5]}>"; }
/** * sets active menu entries, allows addition of dynamic entries and * prints level $level of a menu, identified by $id * * $decorator identifies a decorator class - MenuDecorator{$decorator} * $renderArgs are additional parameters passed to Menu::render() * * @param string $id * @param integer $level (if NULL, the full menu tree is printed) * @param boolean $forceActiveMenu * @param string $decorator * @param mixed $renderArgs * * @return string html */ public function __construct($id = NULL, $level = FALSE, $forceActiveMenu = NULL, $decorator = NULL, $renderArgs = NULL) { $application = Application::getInstance(); $this->config = $application->getConfig(); $this->useNiceUris = $application->hasNiceUris(); if (empty($id) && !is_null($this->config->menus)) { throw new MenuGeneratorException(); } $this->request = Request::createFromGlobals(); $this->route = $application->getCurrentRoute(); if (is_null($this->route)) { $this->route = Router::getRouteFromPathInfo(); } if (empty($id)) { $id = array_shift(array_keys($this->config->menus)); } if (!isset($this->config->menus[$id]) || !count($this->config->menus[$id]->getEntries()) && $this->config->menus[$id]->getType() == 'static') { throw new MenuGeneratorException("Menu '" . $id . "' not found or empty."); } $this->menu = $this->config->menus[$id]; $this->id = $id; $this->level = $level; $this->decorator = $decorator; $this->renderArgs = is_null($renderArgs) ? [] : $renderArgs; // if $forceActiveMenu was initialized before, it will not be overwritten if (is_null(self::$forceActiveMenu)) { self::$forceActiveMenu = (bool) $forceActiveMenu; } }
/** * redirect using configured redirect information * if route has no redirect set, redirect will lead to "start page" * * @param array $queryParams * @param number $statusCode */ public function redirect($queryParams = [], $statusCode = 302) { $request = Request::createFromGlobals(); $application = Application::getInstance(); $urlSegments = [$request->getSchemeAndHttpHost()]; if ($application->hasNiceUris()) { if (($scriptName = basename($request->getScriptName(), '.php')) !== 'index') { $urlSegments[] = $scriptName; } } else { $urlSegments[] = trim($request->getScriptName(), '/'); } if (count($queryParams)) { $query = '?' . http_build_query($queryParams); } else { $query = ''; } return new RedirectResponse(implode('/', $urlSegments) . '/' . $this->redirect . $query); }
/** * * @param string $scriptName (e.g. index.php, admin.php) * @param array $pathSegments * * @return \vxPHP\Routing\Route */ private static function getRouteFromConfig($scriptName, array $pathSegments = NULL) { $routes = Application::getInstance()->getConfig()->routes; // if no page given try to get the first from list if (is_null($pathSegments) && isset($routes[$scriptName])) { return array_shift($routes[$scriptName]); } $pathToCheck = implode('/', $pathSegments); $requestMethod = Request::createFromGlobals()->getMethod(); $foundRoute = NULL; $default = NULL; // iterate over routes and try to find the "best" match foreach ($routes[$scriptName] as $route) { // keep default route as fallback, when no match is found if ($route->getRouteId() === 'default') { $default = $route; } // pick route only when request method requirement is met if (preg_match('~(?:/|^)' . $route->getMatchExpression() . '(?:/|$)~', $pathToCheck) && $route->allowsRequestMethod($requestMethod)) { // if no route was found yet, pick this first match if (!isset($foundRoute)) { $foundRoute = $route; } else { // if a route has been found previously, choose the more "precise" and/or later one // choose the route with more satisfied placeholders // @todo could be optimized if (count(self::getSatisfiedPlaceholders($route, $pathToCheck)) >= count(self::getSatisfiedPlaceholders($foundRoute, $pathToCheck))) { $foundRoute = $route; } } } } // return "normal" route, if found if (isset($foundRoute)) { return $foundRoute; } // return default route as fallback (if available) return $default; }