You are here

public function InstallableRequirement::getObject in Markdown 8.2

Retrieves the object defined by id/type.

@noinspection PhpDocMissingThrowsInspection

Return value

mixed|void The object defined by id/type.

1 call to InstallableRequirement::getObject()
InstallableRequirement::validate in src/Annotation/InstallableRequirement.php
Validates the requirement.

File

src/Annotation/InstallableRequirement.php, line 104

Class

InstallableRequirement
Markdown Requirement Annotation.

Namespace

Drupal\markdown\Annotation

Code

public function getObject() {
  if ($this
    ->isTyped()) {
    $container = \Drupal::getContainer();
    list($type, $id) = $this
      ->listTypeId();
    switch ($type) {
      case 'parser':
        if (($parserManager = ParserManager::create($container)) && $parserManager
          ->hasDefinition($id)) {
          if (!isset($this->name)) {
            $definition = $parserManager
              ->getDefinition($id);
            if ($library = $definition
              ->getInstalledLibrary() ?: $definition
              ->getPreferredLibrary()) {
              $this->name = $library
                ->getLink();
            }
          }
          return $parserManager
            ->createInstance($id);
        }
        break;
      case 'extension':
        if (($extensionManager = ExtensionManager::create($container)) && $extensionManager
          ->hasDefinition($id)) {
          if (!isset($this->name)) {
            $definition = $extensionManager
              ->getDefinition($id);
            if ($library = $definition
              ->getInstalledLibrary() ?: $definition
              ->getPreferredLibrary()) {
              $this->name = $library
                ->getLink();
            }
          }
          return $extensionManager
            ->createInstance($id);
        }
        break;
      case 'filter':

        /** @var \Drupal\filter\FilterPluginManager $filterManager */
        if (($filterManager = $container
          ->get('plugin.manager.filter')) && $filterManager
          ->hasDefinition($id)) {
          if (!isset($this->name)) {
            $this->name = t('Filter "@id"', [
              '@id' => $id,
            ]);
          }

          /* @noinspection PhpUnhandledExceptionInspection */
          return $filterManager
            ->createInstance($id);
        }
        break;
      case 'service':
        if ($container
          ->has($id)) {
          if (!isset($this->name)) {
            $this->name = t('Service "@id"', [
              '@id' => $id,
            ]);
          }
          return $container
            ->get($id);
        }
        break;
    }
  }
}