ComposerPackage.php in Markdown 8.2
File
src/Annotation/ComposerPackage.php
View source
<?php
namespace Drupal\markdown\Annotation;
use Composer\InstalledVersions;
use Doctrine\Common\Annotations\AnnotationException;
use Drupal\Core\Url;
use Drupal\markdown\Util\Composer;
class ComposerPackage extends InstallableLibrary {
protected function detectVersion() {
$id = $this
->getId();
if (!class_exists('\\Composer\\InstalledVersions')) {
return Composer::getInstalledVersion($id) ?: Composer::getVersionFromClass($this->object);
}
if (InstalledVersions::isInstalled($id)) {
return InstalledVersions::getPrettyVersion($id);
}
}
public function getAvailableVersions() {
if (!isset($this->availableVersions)) {
$this->availableVersions = [];
$id = $this
->getId();
$json = $this
->requestJson(sprintf('https://repo.packagist.org/p/%s.json', $id));
if (!empty($json['packages'][$id])) {
$this->availableVersions = array_keys($json['packages'][$id]);
}
}
return $this->availableVersions;
}
public function getInstallCommand() {
return 'composer require ' . $this->id;
}
public function getPackageName() {
if ($parts = explode('/', $this->id, 2)) {
return $parts[1];
}
}
public function getVendorName() {
if ($parts = explode('/', $this->id, 2)) {
return $parts[0];
}
}
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];
}
protected function requestPackage() {
$json = $this
->requestJson(sprintf('https://packagist.org/packages/%s.json', $this
->getId()));
return !empty($json['package']) ? $json['package'] : [];
}
protected function validateIdentifier(Identifier $id) {
if (!$id
->contains('/')) {
throw AnnotationException::semanticalError('A ComposerPackage definition must contain a forward-slash (/) in its identifier so that it represents the correct {vendor}/{package} name.');
}
}
}