Exemple #1
0
 /**
  * Maps HTTP request to a PresenterRequest object.
  * @param  Nette\Web\IHttpRequest
  * @return PresenterRequest|NULL
  */
 public function match(Nette\Web\IHttpRequest $httpRequest)
 {
     // combine with precedence: get, (post,) defaults
     $params = $httpRequest->getQuery();
     $params += $this->defaults;
     if (!isset($params[self::PRESENTER_KEY])) {
         throw new \InvalidStateException('Missing presenter.');
     }
     $presenter = $this->module . $params[self::PRESENTER_KEY];
     unset($params[self::PRESENTER_KEY]);
     return new PresenterRequest($presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured()));
 }
Exemple #2
0
 public function match(IHttpRequest $httpRequest)
 {
     $path = substr($httpRequest->getUri()->getPath(), strlen($httpRequest->getUri()->getBasePath()));
     foreach ($this->getData() as $id => $url) {
         if ($this->prefix . $url == $path || $this->prefix . $url . "/" == $path) {
             $params = $httpRequest->getQuery();
             $params["id"] = $id;
             $params["action"] = $this->action;
             return new PresenterRequest($this->presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array('secured' => $httpRequest->isSecured()));
         }
     }
     return null;
 }
Exemple #3
0
	/**
	 * Maps HTTP request to a PresenterRequest object.
	 *
	 * @author   Jan Tvrdík
	 * @param    IHttpRequest
	 * @return   PresenterRequest|NULL
	 */
	public function match(IHttpRequest $httpRequest)
	{
		$this->httpRequest = $httpRequest;
		$path = rtrim($httpRequest->getUri()->getRelativeUri(), '/');
		$tmp = $this->resolvePath($path);
		if ($tmp === FALSE) return NULL;
		list($page, $lang) = $tmp;

		return new PresenterRequest(
			$this->presenter,
			$httpRequest->getMethod(),
			array('page' => $page, 'lang' => $lang),
			$httpRequest->getPost(),
			$httpRequest->getFiles(),
			array('secured' => $httpRequest->isSecured())
		);
	}
	/**
	 * Maps HTTP request to a PresenterRequest object.
	 *
	 * @author   Jan Tvrdík
	 * @param    IHttpRequest
	 * @return   PresenterRequest|NULL
	 */
	public function match(IHttpRequest $httpRequest)
	{
		$path = rtrim($httpRequest->getUri()->getRelativeUri(), '/');
		$page = ($path === '' ? $this->homepage : $path);
		$templateLocator = Env::getService('TemplateLocator');
		if (!$templateLocator->existsPage($page)) {
			$page .= '/' . $this->defaultPage;
			if (!$templateLocator->existsPage($page)) {
				return NULL;
			}
		}

		return new PresenterRequest(
			$this->presenter,
			$httpRequest->getMethod(),
			array('page' => $page),
			$httpRequest->getPost(),
			$httpRequest->getFiles(),
			array('secured' => $httpRequest->isSecured())
		);
	}
Exemple #5
0
 /**
  * Maps HTTP request to a PresenterRequest object.
  * @param  Nette\Web\IHttpRequest
  * @return PresenterRequest|NULL
  */
 public function match(Nette\Web\IHttpRequest $httpRequest)
 {
     // combine with precedence: mask (params in URL-path), fixity, query, (post,) defaults
     // 1) URL MASK
     $uri = $httpRequest->getUri();
     if ($this->type === self::HOST) {
         $path = '//' . $uri->getHost() . $uri->getPath();
     } elseif ($this->type === self::RELATIVE) {
         $basePath = $uri->getBasePath();
         if (strncmp($uri->getPath(), $basePath, strlen($basePath)) !== 0) {
             return NULL;
         }
         $path = (string) substr($uri->getPath(), strlen($basePath));
     } else {
         $path = $uri->getPath();
     }
     if ($path !== '') {
         $path = rtrim($path, '/') . '/';
     }
     if (!($matches = String::match($path, $this->re))) {
         // stop, not matched
         return NULL;
     }
     // deletes numeric keys, restore '-' chars
     $params = array();
     foreach ($matches as $k => $v) {
         if (is_string($k) && $v !== '') {
             $params[str_replace('___', '-', $k)] = $v;
             // trick
         }
     }
     // 2) CONSTANT FIXITY
     foreach ($this->metadata as $name => $meta) {
         if (isset($params[$name])) {
             //$params[$name] = $this->flags & self::CASE_SENSITIVE === 0 ? strtolower($params[$name]) : */$params[$name]; // strtolower damages UTF-8
         } elseif (isset($meta['fixity']) && $meta['fixity'] !== self::OPTIONAL) {
             $params[$name] = NULL;
             // cannot be overwriten in 3) and detected by isset() in 4)
         }
     }
     // 3) QUERY
     if ($this->xlat) {
         $params += self::renameKeys($httpRequest->getQuery(), array_flip($this->xlat));
     } else {
         $params += $httpRequest->getQuery();
     }
     // 4) APPLY FILTERS & FIXITY
     foreach ($this->metadata as $name => $meta) {
         if (isset($params[$name])) {
             if (!is_scalar($params[$name])) {
             } elseif (isset($meta[self::FILTER_TABLE][$params[$name]])) {
                 // applyies filterTable only to scalar parameters
                 $params[$name] = $meta[self::FILTER_TABLE][$params[$name]];
             } elseif (isset($meta[self::FILTER_IN])) {
                 // applyies filterIn only to scalar parameters
                 $params[$name] = call_user_func($meta[self::FILTER_IN], (string) $params[$name]);
                 if ($params[$name] === NULL && !isset($meta['fixity'])) {
                     return NULL;
                     // rejected by filter
                 }
             }
         } elseif (isset($meta['fixity'])) {
             $params[$name] = $meta[self::VALUE];
         }
     }
     // 5) BUILD PresenterRequest
     if (!isset($params[self::PRESENTER_KEY])) {
         throw new \InvalidStateException('Missing presenter in route definition.');
     }
     if (isset($this->metadata[self::MODULE_KEY])) {
         if (!isset($params[self::MODULE_KEY])) {
             throw new \InvalidStateException('Missing module in route definition.');
         }
         $presenter = $params[self::MODULE_KEY] . ':' . $params[self::PRESENTER_KEY];
         unset($params[self::MODULE_KEY], $params[self::PRESENTER_KEY]);
     } else {
         $presenter = $params[self::PRESENTER_KEY];
         unset($params[self::PRESENTER_KEY]);
     }
     return new PresenterRequest($presenter, $httpRequest->getMethod(), $params, $httpRequest->getPost(), $httpRequest->getFiles(), array(PresenterRequest::SECURED => $httpRequest->isSecured()));
 }