Ejemplo n.º 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);
 }
Ejemplo n.º 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);
 }