You are here

public function ComposerPackage::getVersionUrl in Markdown 8.2

Retrieves the version as a URL.

Parameters

string $version: A specific version to retrieve a URL for. If not specified, it will default to the currently installed version.

array $options: Optional. Options to pass to the creation of the URL object.

Return value

\Drupal\Core\Url|false A specific version URL, if set; FALSE otherwise.

Overrides InstallableLibrary::getVersionUrl

File

src/Annotation/ComposerPackage.php, line 96

Class

ComposerPackage
Annotation for providing an installable library via Composer.

Namespace

Drupal\markdown\Annotation

Code

public function getVersionUrl($version = NULL, array $options = []) {
  if (!$version) {
    $version = $this->version;
  }
  if (!isset($this->versionUrls[$version])) {
    $this->versionUrls[$version] = FALSE;
    if ($this
      ->isKnownVersion($version) && !$this
      ->isDev($version) && ($json = $this
      ->requestPackage())) {
      $repository = !empty($json['repository']) ? $json['repository'] : sprintf('https://packagist.org/packages/%s', $this
        ->getId());
      if (!isset($json['versions'][$version])) {
        $version = "v{$version}";
        $this->versionUrls[$version] = FALSE;
      }
      if (isset($json['versions'][$version])) {
        if (!isset($options['attributes']['target'])) {
          $options['attributes']['target'] = '_blank';
        }
        switch (parse_url($repository, PHP_URL_HOST)) {
          case 'github.com':
            $uri = sprintf('%s/releases/%s', $repository, $version);
            break;
          case 'packagist.org':
            $uri = sprintf('%s#%s', $repository, $version);
            break;
          default:
            $uri = $repository;
        }
        $this->versionUrls[$version] = Url::fromUri($uri, $options);
      }
    }
  }
  return $this->versionUrls[$version];
}