You are here

class PeclExtension in Markdown 8.2

PeclExtension Annotation.

@Target("ANNOTATION")

@todo Move upstream to https://www.drupal.org/project/installable_plugins.

Hierarchy

Expanded class hierarchy of PeclExtension

File

src/Annotation/PeclExtension.php, line 16

Namespace

Drupal\markdown\Annotation
View source
class PeclExtension extends InstallableLibrary {

  /**
   * An associative array of package information, keyed by version.
   *
   * @var array[]
   */
  protected $packageInfo = [];

  /**
   * {@inheritdoc}
   */
  public function __construct($values = []) {
    parent::__construct($values);

    // Add the necessary PHP requirement.
    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'],
          ],
        ],
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function detectVersion() {
    if (($name = $this
      ->getName()) && extension_loaded($name) && ($version = phpversion($name))) {

      // Extract any compiled "library" version associated with the extension.
      // @todo Revisit this to see if there's a better way.
      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;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableVersions() {
    if (!isset($this->availableVersions)) {
      $this->availableVersions = [];

      // @see https://github.com/php/web-pecl/blob/e98cb34ebcb26b75b4001d1b3458afdad6ba6f83/src/Rest.php#L519-L520
      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;
  }

  /**
   * {@inheritdoc}
   */
  public function getInstallCommand() {
    return 'pecl install ' . $this
      ->getName();
  }

  /**
   * Retrieves the name of a PHP extension.
   *
   * The name is extracted from a plugin identifier that starts with "ext-".
   *
   * @return string|null
   *   The PHP extension name, if it exists.
   */
  public function getName() {
    return $this->id
      ->removeLeft('ext-');
  }

  /**
   * Retrieves the package information for the PECL package.
   *
   * @param string|null $version
   *   A specific version of package information to retrieve. If not specified,
   *   it will default to the currently installed version or the latest version
   *   available if not installed.
   *
   * @return array
   *   An associative array of PECL package information.
   */
  public function getPackageInfo($version = NULL) {

    // Attempt to use installed version if none was explicitly specified.
    if (!$version && $this->version) {
      $version = $this->version;
    }
    elseif (!$version && ($latestVersion = $this
      ->getLatestVersion())) {
      $version = $latestVersion;
    }

    // Immediately return if version couldn't be determined.
    if (!$version) {
      return [];
    }
    if (!isset($this->packageInfo[$version])) {
      $this->packageInfo[$version] = [];

      // @see https://github.com/php/web-pecl/blob/e98cb34ebcb26b75b4001d1b3458afdad6ba6f83/src/Rest.php#L519-L520
      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];
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  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];
  }

  /**
   * {@inheritdoc}
   */
  protected function validateIdentifier(Identifier $id) {
    if (!$id
      ->startsWith('ext-')) {
      throw AnnotationException::semanticalError('A PeclExtension definition must prefix its identifier with "ext-".');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnnotationBase::$class protected property The class used for this annotated class.
AnnotationBase::$id public property The annotated class ID. 1
AnnotationBase::$provider protected property The provider of the annotated class.
AnnotationBase::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass
AnnotationBase::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider
AnnotationBase::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass
AnnotationBase::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider
AnnotationObject::$description public property The description of the plugin.
AnnotationObject::$label public property A human-readable label.
AnnotationObject::$weight public property The weight of the plugin.
AnnotationObject::$_deprecated protected property Stores deprecated values.
AnnotationObject::$_deprecatedProperties protected property A list of deprecation messages, keyed by the deprecated property name.
AnnotationObject::$_triggeredDeprecations private property A list of triggered deprecations.
AnnotationObject::create public static function Allows the creation of new objects statically, for easier chainability.
AnnotationObject::DEPRECATED_REGEX constant
AnnotationObject::doMerge protected function Merges values with this plugin.
AnnotationObject::get public function Gets the value of an annotation. Overrides AnnotationInterface::get
AnnotationObject::getId public function Gets the unique ID for this annotated class. Overrides AnnotationBase::getId
AnnotationObject::getIterator public function
AnnotationObject::id public function Gets the unique identifier of the plugin. Overrides PluginDefinitionInterface::id
AnnotationObject::merge public function Merges values with this plugin.
AnnotationObject::normalizeValue protected function Normalizes a value to ensure its ready to be merged with the definition.
AnnotationObject::offsetExists public function
AnnotationObject::offsetGet public function
AnnotationObject::offsetSet public function
AnnotationObject::offsetUnset public function
AnnotationObject::protectedProperties protected function Indicates properties that should never be overridden after instantiation. 1
AnnotationObject::triggerDeprecation private function Triggers a deprecation notice for a given property.
AnnotationObject::__get public function
AnnotationObject::__isset public function
AnnotationObject::__set public function
AnnotationObject::__sleep public function
AnnotationObject::__unset public function
AnnotationObject::__wakeup public function
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Aliased as: __sleepTrait 1
DependencySerializationTrait::__wakeup public function Aliased as: __wakeupTrait 2
HttpClientTrait::$httpClient protected static property The HTTP client.
HttpClientTrait::httpClient protected static function Retrieves an HTTP client.
InstallableLibrary::$availableVersions protected property All available versions, regardless of stability.
InstallableLibrary::$customLabel public property Optional. A customized human-readable label for the library.
InstallableLibrary::$latestVersion protected property The latest version, based on currently available versions and stability.
InstallableLibrary::$newerVersions protected property An array of newer versions, based on currently set version and stability.
InstallableLibrary::$requestException protected property The last exception thrown when attempting to initiate a request.
InstallableLibrary::$versionExtra public property The version with extra metadata.
InstallableLibrary::$versionUrls protected property A specific version URL, if known.
InstallableLibrary::createObjectRequirement public function
InstallableLibrary::getLatestVersion public function Retrieves the latest version based on available versions.
InstallableLibrary::getNewerVersions public function Retrieves the newer versions of the library.
InstallableLibrary::getStatus public function Retrieves the current status of the library.
InstallableLibrary::getVersionLink public function Retrieves the version as a link to a specific release.
InstallableLibrary::hasRequestFailure public function Indicates whether there is an issue performing requests for the library.
InstallableLibrary::isAlpha public function Indicates whether this is an alpha version.
InstallableLibrary::isBeta public function Indicates whether this is a beta version.
InstallableLibrary::isDev public function Indicates whether this is an alpha version.
InstallableLibrary::isKnownVersion public function Indicates whether this is a known version.
InstallableLibrary::isPrerelease public function Indicates whether this is any prerelease version.
InstallableLibrary::isRc public function Indicates whether this is a release candidate version.
InstallableLibrary::isStable public function Indicates whether this is a stable version.
InstallableLibrary::request protected function Requests a URL.
InstallableLibrary::requestJson protected function Retrieves JSON from a URL.
InstallableLibrary::requestXml protected function Retrieves XML from a URL.
InstallablePluginTrait::$deprecated public property Indicates the plugin has been deprecated by providing a message.
InstallablePluginTrait::$experimental public property Indicates the plugin is experimental by providing a message.
InstallablePluginTrait::$installed Deprecated public property Flag indicating whether plugin is installed.
InstallablePluginTrait::$object public property The class name of the primary object that is implemented by the library.
InstallablePluginTrait::$preferred public property Flag indicating whether it is the preferred library.
InstallablePluginTrait::$requirements public property An array of requirements for the plugin.
InstallablePluginTrait::$requirementViolations public property A list of requirement violation messages.
InstallablePluginTrait::$runtimeRequirements public property An array of runtime requirements for the plugin.
InstallablePluginTrait::$ui public property Flag indicating whether this plugin is to be visible in UI areas.
InstallablePluginTrait::$url public property A URL for the plugin, typically for installation instructions.
InstallablePluginTrait::$version public property The installed version.
InstallablePluginTrait::$versionConstraint Deprecated public property The constraint the version must satisfy to be considered "installable".
InstallablePluginTrait::getLink public function Retrieves the plugin as a link using its label and URL.
InstallablePluginTrait::getVersionRequirements public function Retrieves requirements that contain "Version" constraints.
InstallablePluginTrait::validate public function Validates the plugin requirements.
PeclExtension::$packageInfo protected property An associative array of package information, keyed by version.
PeclExtension::detectVersion protected function Detects the version of the library installed. Overrides InstallableLibrary::detectVersion
PeclExtension::getAvailableVersions public function Retrieves the available versions of the library. Overrides InstallableLibrary::getAvailableVersions
PeclExtension::getInstallCommand public function Retrieves the CLI command used to install the library, if any. Overrides InstallableLibrary::getInstallCommand
PeclExtension::getName public function Retrieves the name of a PHP extension.
PeclExtension::getPackageInfo public function Retrieves the package information for the PECL package.
PeclExtension::getUrl public function Retrieves the definition's URL property as an object. Overrides InstallablePluginTrait::getUrl
PeclExtension::getVersionUrl public function Retrieves the version as a URL. Overrides InstallableLibrary::getVersionUrl
PeclExtension::validateIdentifier protected function Helper method for validating the definition identifier. Overrides AnnotationObject::validateIdentifier
PeclExtension::__construct public function AnnotationObject constructor. Overrides InstallableLibrary::__construct