You are here

protected function InstallablePluginManager::processLibraryDefinition in Markdown 8.2

Processes the library definition.

Parameters

\Drupal\markdown\Annotation\InstallablePlugin $definition: The plugin definition.

\Drupal\markdown\Annotation\InstallableLibrary $library: A library definition.

bool $preferred: A flag indicating whether a library was explicitly set as "preferred", passed by reference.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

1 call to InstallablePluginManager::processLibraryDefinition()
InstallablePluginManager::processDefinition in src/PluginManager/InstallablePluginManager.php
Performs extra processing on plugin definitions.

File

src/PluginManager/InstallablePluginManager.php, line 529

Class

InstallablePluginManager
Installable Plugin Manager.

Namespace

Drupal\markdown\PluginManager

Code

protected function processLibraryDefinition(InstallablePlugin $definition, InstallableLibrary $library, &$preferred = FALSE) {
  if (!$preferred && $library->preferred) {
    $preferred = TRUE;
  }

  // Normalize the object.
  if ($library->object) {
    $library->object = static::normalizeClassName($library->object);

    // Prepend a new runtime requirement to ensure that the object exists
    // before any other requirements are executed. This helps to ensure that
    // if a requirement depends on the object existing, it doesn't fatal and
    // instead treated as "uninstalled".
    if ($requirement = $this
      ->createObjectRequirement($definition, $library)) {
      array_unshift($library->requirements, $requirement);
    }
  }

  // Convert versionConstraint into a requirement.
  // @todo Deprecated property, remove in 3.0.0.
  if ($versionConstraint = $library->versionConstraint) {
    $requirement = new InstallableRequirement();
    $requirement->constraints['Version'] = $versionConstraint;
    $library->requirements[] = $requirement;
    unset($library->versionConstraint);
  }
  $versionDefinition = NULL;

  // If version is populated with a callback or constant, add a requirement
  // that it should exist. Then, if the requirement is met, it will be
  // populated below with the validated value.
  if (!empty($library->version) && is_string($library->version) && !Semver::isValid($library->version)) {
    $versionDefinition = static::normalizeClassName($library->version);
    unset($library->version);
  }

  // Process requirements.
  $versionRequirement = NULL;
  if ($library->requirements) {
    foreach ($library->requirements as $key => $requirement) {
      if ($requirement instanceof InstallableLibrary) {
        $requirement = $requirement
          ->createObjectRequirement();
      }

      // Version constraints that have not explicitly specified a value
      // or callback should be validated against this library's installed
      // version which can only be determined later below; save it.
      if (!isset($requirement->value) && !isset($requirement->callback) && count($requirement->constraints) === 1 && key($requirement->constraints) === 'Version' && !empty($requirement->constraints['Version'])) {
        $versionRequirement = $requirement;
        unset($library->requirements[$key]);
        continue;
      }

      // Move parser and extension requirements to runtime.
      // Note: this helps to prevent recursion while building definitions.
      if (in_array($requirement
        ->getType(), [
        'parser',
        'extension',
      ], TRUE)) {
        $library->runtimeRequirements[] = $requirement;
        unset($library->requirements[$key]);
        continue;
      }

      // Attempt to validate all requirements. Up until the point that any
      // errors or exceptions occur.
      Error::suppress(function () use ($requirement, $library) {
        foreach ($requirement
          ->validate() as $violation) {
          $key = (string) $violation
            ->getMessage();
          if (!isset($library->requirementViolations[$key])) {
            $library->requirementViolations[$key] = $violation
              ->getMessage();
          }
        }
      });
    }
  }

  // Now that requirements have been met, actually extract the version
  // from the definition that was provided.
  if (isset($versionDefinition)) {
    if (!$versionRequirement) {
      $versionRequirement = new InstallableRequirement();
      $versionRequirement->constraints = [
        'Version' => [
          'name' => $definition
            ->id(),
        ],
      ];
    }
    if (defined($versionDefinition) && ($version = constant($versionDefinition))) {
      $versionRequirement->value = $version;
    }
    elseif (is_callable($versionDefinition) && ($version = call_user_func_array($versionDefinition, [
      $library,
      $definition,
    ]))) {
      $versionRequirement->value = $version;
    }
    elseif ($library->object && ($version = Composer::getVersionFromClass($library->object))) {
      $versionRequirement->value = $version;
    }

    /** @var \Symfony\Component\Validator\ConstraintViolationListInterface $violations */
    $violations = Error::suppress(function () use ($versionRequirement) {
      return $versionRequirement
        ->validate();
    });

    // Now, validate the version.
    if ($violations && $violations
      ->count()) {
      foreach ($violations as $violation) {
        $key = (string) $violation
          ->getMessage();
        if (!isset($library->requirementViolations[$key])) {
          $library->requirementViolations[$key] = $violation
            ->getMessage();
        }
      }
    }
    elseif ($violations !== null) {
      $library->version = $versionRequirement->value;
    }
  }
}