PeclExtension.php in Markdown 8.2
File
src/Annotation/PeclExtension.php
View source
<?php
namespace Drupal\markdown\Annotation;
use Doctrine\Common\Annotations\AnnotationException;
use Drupal\Core\Url;
class PeclExtension extends InstallableLibrary {
protected $packageInfo = [];
public function __construct($values = []) {
parent::__construct($values);
if (($info = $this
->getPackageInfo()) && !empty($info['dependencies']['required']['php']['min'])) {
$this->requirements[] = InstallableRequirement::create([
'value' => PHP_VERSION,
'constraints' => [
'Version' => [
'name' => 'PHP',
'value' => '>=' . $info['dependencies']['required']['php']['min'],
],
],
]);
}
}
protected function detectVersion() {
if (($name = $this
->getName()) && extension_loaded($name) && ($version = phpversion($name))) {
ob_start();
phpinfo(INFO_MODULES);
$contents = ob_get_contents();
ob_clean();
preg_match('/(lib-?' . preg_quote($name, '/') . ').*?v?\\s?(\\d+\\.\\d+(?:\\.\\d+)?)/', $contents, $matches);
if (!empty($matches[2]) && (!$version || $version !== $matches[2])) {
$libName = rtrim($matches[1], '-');
$libVersion = $matches[2];
$versionExtra = "{$version}+{$libName}-{$libVersion}";
}
return isset($versionExtra) ? [
$version,
$versionExtra,
] : $version;
}
}
public function getAvailableVersions() {
if (!isset($this->availableVersions)) {
$this->availableVersions = [];
if (($name = $this
->getName()) && ($data = $this
->requestXml(sprintf('https://pecl.php.net/rest/r/%s/allreleases.xml', $name), TRUE)) && !empty($data['r'])) {
$this->availableVersions = array_column($data['r'], 'v');
}
}
return $this->availableVersions;
}
public function getInstallCommand() {
return 'pecl install ' . $this
->getName();
}
public function getName() {
return $this->id
->removeLeft('ext-');
}
public function getPackageInfo($version = NULL) {
if (!$version && $this->version) {
$version = $this->version;
}
elseif (!$version && ($latestVersion = $this
->getLatestVersion())) {
$version = $latestVersion;
}
if (!$version) {
return [];
}
if (!isset($this->packageInfo[$version])) {
$this->packageInfo[$version] = [];
if (($name = $this
->getName()) && ($data = $this
->requestXml(sprintf('https://pecl.php.net/rest/r/%s/package.%s.xml', $name, $version), TRUE))) {
$this->packageInfo[$version] = $data;
}
}
return $this->packageInfo[$version];
}
public function getUrl(array $options = []) {
if (!$this->url && ($name = $this
->getName())) {
$this->url = sprintf('https://pecl.php.net/package/%s', $name);
}
return parent::getUrl($options);
}
public function getVersionUrl($version = NULL, array $options = []) {
if (!isset($version)) {
$version = $this->version ?: '';
}
if (!isset($this->versionUrls[$version])) {
$this->versionUrls[$version] = FALSE;
if ($this
->isKnownVersion($version) && !$this
->isDev($version) && ($name = $this
->getName())) {
if (!isset($options['attributes']['target'])) {
$options['attributes']['target'] = '_blank';
}
$this->versionUrls[$version] = Url::fromUri(sprintf('https://pecl.php.net/package/%s/%s', $name, $version), $options);
}
}
return $this->versionUrls[$version];
}
protected function validateIdentifier(Identifier $id) {
if (!$id
->startsWith('ext-')) {
throw AnnotationException::semanticalError('A PeclExtension definition must prefix its identifier with "ext-".');
}
}
}