Exemple #1
0
 public function url(array $args = [])
 {
     $url = PRIME_URI;
     $parts = Url::parse($url, true);
     $parts['vars'] = array_replace($parts['vars'], $args);
     return Url::build($parts);
 }
Exemple #2
0
 public function url(array $args = [])
 {
     $grav = Grav::instance();
     $url = $grav['uri']->url;
     $parts = Url::parse($url, true);
     $parts['vars'] = array_replace($parts['vars'], $args);
     return Url::build($parts);
 }
Exemple #3
0
 /**
  * Return URL to the resource.
  *
  * @example {{ url('gantry-theme://images/logo.png')|default('http://www.placehold.it/150x100/f4f4f4') }}
  *
  * @param  string $url         Resource to be located.
  * @param  bool $domain        True to include domain name.
  * @param  int $timestamp_age  Append timestamp to files that are less than x seconds old. Defaults to a week.
  *                             Use value <= 0 to disable the feature.
  * @return string|null         Returns url to the resource or null if resource was not found.
  */
 public static function url($url, $domain = false, $timestamp_age = null)
 {
     if (!is_string($url) || $url === '') {
         // Return null on invalid input.
         return null;
     }
     if ($url[0] === '#') {
         // Handle fragment only.
         return $url;
     }
     $parts = Url::parse($url);
     if (!is_array($parts)) {
         // URL could not be parsed, return null.
         return null;
     }
     // Make sure we always have scheme, host, port and path.
     $scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
     $host = isset($parts['host']) ? $parts['host'] : '';
     $port = isset($parts['port']) ? $parts['port'] : '';
     $path = isset($parts['path']) ? $parts['path'] : '';
     if ($scheme && !$port) {
         // If URL has a scheme, we need to check if it's one of Gantry streams.
         $gantry = static::gantry();
         /** @var UniformResourceLocator $locator */
         $locator = $gantry['locator'];
         if (!$locator->schemeExists($scheme)) {
             // If scheme does not exists as a stream, assume it's external.
             return $url;
         }
         // Attempt to find the resource (because of parse_url() we need to put host back to path).
         $path = $locator->findResource("{$scheme}://{$host}{$path}", false);
         if ($path === false) {
             return null;
         }
     } elseif ($host || $port) {
         // If URL doesn't have scheme but has host or port, it is external.
         return $url;
     }
     // At this point URL is either relative or absolute path; let us find if it is relative and not . or ..
     if ($path && '/' !== $path[0] && '.' !== $path[0]) {
         if ($timestamp_age === null) {
             $timestamp_age = static::$timestamp_age;
         }
         if ($timestamp_age > 0) {
             // We want to add timestamp to the URI: do it only for existing files.
             $realPath = @realpath(GANTRY5_ROOT . '/' . $path);
             if ($realPath && is_file($realPath)) {
                 $time = filemtime($realPath);
                 // Only append timestamp for files that are less than the maximum age.
                 if ($time > time() - $timestamp_age) {
                     $parts['query'] = (!empty($parts['query']) ? "{$parts['query']}&" : '') . sprintf('%x', $time);
                 }
             }
         }
         // We need absolute URI instead of relative.
         $path = rtrim(static::rootUri(), '/') . '/' . $path;
     }
     // Set absolute URI.
     $uri = $path;
     // Add query string back.
     if (!empty($parts['query'])) {
         if (!$uri) {
             $uri = static::rootUri();
         }
         $uri .= '?' . $parts['query'];
     }
     // Add fragment back.
     if (!empty($parts['fragment'])) {
         if (!$uri) {
             $uri = static::rootUri();
         }
         $uri .= '#' . $parts['fragment'];
     }
     return static::domain($domain) . $uri;
 }