Example #1
0
 /**
  * @param string $name
  * @param array $data
  * @param boolean $qsa
  * @throws \Exception
  * @return string
  */
 public function assemble($name, array $data = [], $reset = \false, $qsa = \true)
 {
     static $baseUrl;
     if ($name === \null && $this->currentRoute instanceof Route) {
         $name = $this->currentRoute->getName();
     }
     if (!isset($this->routes[$name])) {
         throw new CoreException(sprintf('[' . __METHOD__ . '] Route "%s" not found!', $name), 500);
     }
     $route = $this->routes[$name];
     $pattern = $route->getPattern();
     $data += $this->globalParams;
     if (isset($this->routesStatic[$pattern])) {
         $url = $pattern;
     } else {
         $url = $route->assemble($data, $reset);
     }
     if ($qsa === \true) {
         $qs = $this->request->getQuery();
         if (!empty($data)) {
             $qs = $data + $qs;
         }
         $qs = array_filter($qs);
         if (!empty($qs)) {
             $url .= '?' . http_build_query($qs);
         }
     }
     if ($baseUrl === \null) {
         $baseUrl = $this->request->getBaseUrl();
     }
     return $baseUrl . static::URL_DELIMITER . trim($url, static::URL_DELIMITER);
 }
Example #2
0
 /**
  * @param string $name
  * @param array $data
  * @param boolean $reset
  * @param boolean $qsa
  * @throws \Exception
  * @return string
  */
 public function assemble($name = \null, array $data = [], $reset = \false, $qsa = \true)
 {
     static $request, $basePath, $queryParams;
     if ($name === \null && $this->currentRoute instanceof Route) {
         $name = $this->currentRoute->getName();
     }
     if (!isset($this->routes[$name])) {
         throw new CoreException(\sprintf('[' . __METHOD__ . '] Route "%s" not found!', $name), 500);
     }
     $route = $this->routes[$name];
     $pattern = $route->getPattern();
     $data += $this->globalParams;
     if (isset($this->routesStatic[$pattern])) {
         $url = $pattern;
     } else {
         $url = $route->assemble($data, $reset);
     }
     if ($request === \null) {
         $request = $this->container->get('request');
         if ($basePath === \null) {
             $uri = $request->getUri();
             if (\method_exists($uri, 'getBasePath')) {
                 $basePath = $request->getUri()->getBasePath();
             } else {
                 $basePath = '/';
             }
         }
         if ($queryParams === \null) {
             $queryParams = $request->getQueryParams();
         }
     }
     if ($qsa === \true) {
         $qp = $data + $queryParams;
         $qp = \array_filter($qp);
         if (!empty($qp)) {
             $url .= '?' . \http_build_query($qp);
         }
     }
     if ($url === self::URL_DELIMITER) {
         return $basePath . $url;
     }
     return $basePath . rtrim($url, static::URL_DELIMITER);
 }
Example #3
0
 /**
  * @param string $pattern
  * @param \Closure|string $handler
  * @param string $name
  * @throws \Exception
  * @return \Micro\Router\Route
  */
 public function map($pattern, $handler, $name = \null)
 {
     if (\null === $name) {
         $name = preg_replace('~[^\\w]~', '-', $pattern);
         $name = preg_replace('~[\\-]+~', '-', $name);
         $name = 'route-' . trim($name, '-');
     }
     if (isset($this->routes[$name])) {
         throw new CoreException(sprintf('[' . __METHOD__ . '] Route "%s" already exists!', $name), 500);
     }
     $pattern = static::URL_DELIMITER . trim($pattern, static::URL_DELIMITER);
     $route = new Route($name, $pattern, $handler);
     if (Route::isStatic($pattern)) {
         if (isset($this->routesStatic[$pattern])) {
             throw new CoreException(sprintf('[' . __METHOD__ . '] Route pattern "%s" already exists!', $pattern), 500);
         }
         $this->routesStatic[$pattern] = $name;
     }
     $this->routes[$name] = $route;
     return $route;
 }