Exemplo n.º 1
0
 /**
  * Create a new Url class representing the given url
  *
  * If $params are given, those will be added to the urls parameters
  * and overwrite any existing parameters
  *
  * @param   string          $url        The string representation of the url to parse
  * @param   array           $params     An array of parameters that should additionally be considered for the url
  * @param   Zend_Request    $request    A request to use instead of the default one
  *
  * @return  Url
  */
 public static function fromPath($url, array $params = array(), $request = null)
 {
     if ($request === null) {
         $request = self::getRequest();
     }
     if (!is_string($url)) {
         throw new ProgrammingError(sprintf('url "%s" is not a string', $url));
     }
     $urlObject = new Url();
     $baseUrl = $request->getBaseUrl();
     $urlObject->setBaseUrl($baseUrl);
     // Fetch fragment manually and remove it from the url, to 'help' the parse_url() function
     // parsing the url properly. Otherwise calling the function with a fragment, but without a
     // query will cause unpredictable behaviour.
     $url = self::stripUrlFragment($url);
     $urlParts = parse_url($url);
     if (isset($urlParts['path'])) {
         if ($baseUrl !== '' && strpos($urlParts['path'], $baseUrl) === 0) {
             $urlObject->setPath(substr($urlParts['path'], strlen($baseUrl)));
         } else {
             $urlObject->setPath($urlParts['path']);
         }
     }
     if (isset($urlParts['query'])) {
         $urlParams = array();
         parse_str($urlParts['query'], $urlParams);
         $params = array_merge($urlParams, $params);
     }
     $fragment = self::getUrlFragment($url);
     if ($fragment !== '') {
         $urlObject->setAnchor($fragment);
     }
     $urlObject->setParams($params);
     return $urlObject;
 }
Exemplo n.º 2
0
 /**
  * Create a new Url class representing the given url
  *
  * If $params are given, those will be added to the urls parameters
  * and overwrite any existing parameters
  *
  * @param   string          $url        The string representation of the url to parse
  * @param   array           $params     An array of parameters that should additionally be considered for the url
  * @param   Zend_Request    $request    A request to use instead of the default one
  *
  * @return  Url
  */
 public static function fromPath($url, array $params = array(), $request = null)
 {
     if ($request === null) {
         $request = self::getRequest();
     }
     if (!is_string($url)) {
         throw new ProgrammingError('url "%s" is not a string', $url);
     }
     $urlObject = new Url();
     $baseUrl = $request->getBaseUrl();
     $urlObject->setBaseUrl($baseUrl);
     $urlParts = parse_url($url);
     if (isset($urlParts['path'])) {
         if ($baseUrl !== '' && strpos($urlParts['path'], $baseUrl) === 0) {
             $urlObject->setPath(substr($urlParts['path'], strlen($baseUrl)));
         } else {
             $urlObject->setPath($urlParts['path']);
         }
     }
     // TODO: This has been used by former filter implementation, remove it:
     if (isset($urlParts['query'])) {
         $params = UrlParams::fromQueryString($urlParts['query'])->mergeValues($params);
     }
     if (isset($urlParts['fragment'])) {
         $urlObject->setAnchor($urlParts['fragment']);
     }
     $urlObject->setParams($params);
     return $urlObject;
 }
Exemplo n.º 3
0
 /**
  * Create a new Url class representing the given url
  *
  * If $params are given, those will be added to the urls parameters
  * and overwrite any existing parameters
  *
  * @param   string          $url        The string representation of the url to parse
  * @param   array           $params     An array of parameters that should additionally be considered for the url
  * @param   Zend_Request    $request    A request to use instead of the default one
  *
  * @return  Url
  */
 public static function fromPath($url, array $params = array(), $request = null)
 {
     if ($request === null) {
         $request = self::getRequest();
     }
     if (!is_string($url)) {
         throw new ProgrammingError('url "%s" is not a string', $url);
     }
     $urlObject = new Url();
     if ($url === '#') {
         $urlObject->setPath($url);
         return $urlObject;
     }
     $urlParts = parse_url($url);
     if (isset($urlParts['scheme']) && ($urlParts['scheme'] !== $request->getScheme() || isset($urlParts['host']) && $urlParts['host'] !== $request->getServer('SERVER_NAME') || isset($urlParts['port']) && $urlParts['port'] != $request->getServer('SERVER_PORT'))) {
         $urlObject->setIsExternal();
     }
     if (isset($urlParts['path'])) {
         $urlPath = $urlParts['path'];
         if ($urlPath && $urlPath[0] === '/') {
             if ($urlObject->isExternal() || isset($urlParts['user'])) {
                 $urlPath = substr($urlPath, 1);
             } else {
                 $requestBaseUrl = $request->getBaseUrl();
                 if ($requestBaseUrl && $requestBaseUrl !== '/' && strpos($urlPath, $requestBaseUrl) === 0) {
                     $urlPath = substr($urlPath, strlen($requestBaseUrl) + 1);
                     $urlObject->setBasePath($requestBaseUrl);
                 }
             }
         } elseif (!$urlObject->isExternal()) {
             $urlObject->setBasePath($request->getBaseUrl());
         }
         $urlObject->setPath($urlPath);
     } elseif (!$urlObject->isExternal()) {
         $urlObject->setBasePath($request->getBaseUrl());
     }
     // TODO: This has been used by former filter implementation, remove it:
     if (isset($urlParts['query'])) {
         $params = UrlParams::fromQueryString($urlParts['query'])->mergeValues($params);
     }
     if (isset($urlParts['fragment'])) {
         $urlObject->setAnchor($urlParts['fragment']);
     }
     if (isset($urlParts['user']) || $urlObject->isExternal()) {
         if (isset($urlParts['user'])) {
             $urlObject->setUsername($urlParts['user']);
         }
         if (isset($urlParts['host'])) {
             $urlObject->setHost($urlParts['host']);
         }
         if (isset($urlParts['port'])) {
             $urlObject->setPort($urlParts['port']);
         }
         if (isset($urlParts['scheme'])) {
             $urlObject->setScheme($urlParts['scheme']);
         }
         if (isset($urlParts['pass'])) {
             $urlObject->setPassword($urlParts['pass']);
         }
     }
     $urlObject->setParams($params);
     return $urlObject;
 }
Exemplo n.º 4
0
 /**
  * Create a badge
  *
  * @param   string $state
  * @param   Navigation $badges
  *
  * @return  $this
  */
 public function createBadge($state, Navigation $badges)
 {
     if ($this->has($state)) {
         $badge = $this->get($state);
         $url = clone $this->url->setParams($badge->filter);
         if (isset($this->baseFilter)) {
             $url->addFilter($this->baseFilter);
         }
         $badges->addItem(new NavigationItem($state, array('attributes' => array('class' => 'badge ' . $state), 'label' => $badge->count, 'priority' => $this->priority++, 'title' => vsprintf(mtp('monitoring', $badge->translateSingular, $badge->translatePlural, $badge->count), $badge->translateArgs), 'url' => $url)));
     }
     return $this;
 }