You are here

class ComposerPackage in Markdown 8.2

Annotation for providing an installable library via Composer.

@Target("ANNOTATION")

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

Hierarchy

Expanded class hierarchy of ComposerPackage

See also

https://www.drupal.org/project/markdown/issues/3200476

File

src/Annotation/ComposerPackage.php, line 19

Namespace

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

  /**
   * Detects the installed version of a Composer package.
   *
   * @return string|void
   *   The detected version of the Composer package or NULL if not enabled.
   */
  protected function detectVersion() {
    $id = $this
      ->getId();

    // Composer 1 support.
    // @todo Remove in 4.0.0.
    // @see https://www.drupal.org/project/markdown/issues/3200476
    if (!class_exists('\\Composer\\InstalledVersions')) {
      return Composer::getInstalledVersion($id) ?: Composer::getVersionFromClass($this->object);
    }

    // Composer 2+ runtime installed versions support.
    // @see https://getcomposer.org/doc/07-runtime.md#knowing-the-version-of-package-x
    if (InstalledVersions::isInstalled($id)) {
      return InstalledVersions::getPrettyVersion($id);
    }
  }

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

      // To ensure we have the latest versions at all times, use the
      // https://repo.packagist.org/p/[vendor]/[package].json URL which are
      // static files and not cached.
      $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;
  }

  /**
   * {@inheritdoc}
   */
  public function getInstallCommand() {
    return 'composer require ' . $this->id;
  }

  /**
   * Retrieves the package name from the library identifier.
   *
   * @return string|void
   *   The package name.
   */
  public function getPackageName() {
    if ($parts = explode('/', $this->id, 2)) {
      return $parts[1];
    }
  }

  /**
   * Retrieves the vendor name from the library identifier.
   *
   * @return string|void
   *   The vendor name.
   */
  public function getVendorName() {
    if ($parts = explode('/', $this->id, 2)) {
      return $parts[0];
    }
  }

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

  /**
   * Retrieves the package JSON data.
   *
   * @return array
   *   The package JSON data.
   */
  protected function requestPackage() {

    // When requesting package information, use the normal API URL which
    // includes a lot more metadata about the package. This, unfortunately,
    // cached and only refreshed once every 12 hours.
    $json = $this
      ->requestJson(sprintf('https://packagist.org/packages/%s.json', $this
      ->getId()));
    return !empty($json['package']) ? $json['package'] : [];
  }

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

}

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
ComposerPackage::detectVersion protected function Detects the installed version of a Composer package. Overrides InstallableLibrary::detectVersion
ComposerPackage::getAvailableVersions public function Retrieves the available versions of the library. Overrides InstallableLibrary::getAvailableVersions
ComposerPackage::getInstallCommand public function Retrieves the CLI command used to install the library, if any. Overrides InstallableLibrary::getInstallCommand
ComposerPackage::getPackageName public function Retrieves the package name from the library identifier.
ComposerPackage::getVendorName public function Retrieves the vendor name from the library identifier.
ComposerPackage::getVersionUrl public function Retrieves the version as a URL. Overrides InstallableLibrary::getVersionUrl
ComposerPackage::requestPackage protected function Retrieves the package JSON data.
ComposerPackage::validateIdentifier protected function Helper method for validating the definition identifier. Overrides AnnotationObject::validateIdentifier
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.
InstallableLibrary::__construct public function AnnotationObject constructor. Overrides AnnotationObject::__construct 1
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::getUrl public function Retrieves the definition's URL property as an object. 1
InstallablePluginTrait::getVersionRequirements public function Retrieves requirements that contain "Version" constraints.
InstallablePluginTrait::validate public function Validates the plugin requirements.