You are here

public static function Composer::getVersionFromClass in Markdown 8.2

Retrieves the version based on a provided class name.

@internal

Parameters

string|object $className: The class name or an object where the class name can be extracted from.

mixed $default: The default value to provide if no version could be determined.

bool $cache: Flag indicating whether to use caching to prevent potential and numerous I/O operations when locating the composer.json file and generating a hash based on the composer package directory.

Return value

string|mixed A known version or $default if one could not be determined.

Deprecated

in markdown:8.x-2.0 and is removed from markdown:4.0.0. No replacement.

See also

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

2 calls to Composer::getVersionFromClass()
ComposerPackage::detectVersion in src/Annotation/ComposerPackage.php
Detects the installed version of a Composer package.
InstallablePluginManager::processLibraryDefinition in src/PluginManager/InstallablePluginManager.php
Processes the library definition.

File

src/Util/Composer.php, line 309

Class

Composer
Helper class used to deal with composer.

Namespace

Drupal\markdown\Util

Code

public static function getVersionFromClass($className, $default = NULL, $cache = TRUE) {
  if (is_object($className)) {
    $className = get_class($className);
  }

  // Ensure it exists in the first place.
  if (!is_string($className) || empty($className) || !class_exists($className) && !interface_exists($className) && !trait_exists($className)) {
    return $default;
  }

  // Retrieve any cached results.
  $cid = "composer.version:{$className}";
  if ($cache && ($cached = \Drupal::cache('markdown')
    ->get($cid)) && isset($cached->data)) {
    $json = $cached->data['json'];
    $hash = $cached->data['hash'];
    $name = $cached->data['name'];
    $version = $cached->data['version'];
  }
  else {
    $json = Composer::getJsonFromClass($className, $name, $file);
    $version = Composer::getInstalledVersion($name, $json);
    $hash = !$version ? Composer::generateHash(dirname($file)) : FALSE;
    if ($cache) {
      \Drupal::cache('markdown')
        ->set($cid, [
        'json' => $json,
        'hash' => $hash,
        'name' => $name,
        'version' => $version,
      ]);
    }
  }

  // If a known version was found, use that.
  if (!empty($version)) {
    return $version;
  }

  // Find a matching version for a specific package hash.
  if ($hash && ($versions = static::getVersionHash($name)) && ($version = array_search($hash, $versions, TRUE))) {
    return $version;
  }

  // If no specific version could be determined but a dev-master branch has
  // been specified, use that instead.
  if (!empty($json['extra']['branch-alias']['dev-master'])) {
    return $json['extra']['branch-alias']['dev-master'];
  }

  // Otherwise, return the default value provided.
  return $default;
}