You are here

public static function DefaultFactory::getPluginClass in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php \Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass()
  2. 10 core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php \Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass()

Finds the class relevant for a given plugin.

Parameters

string $plugin_id: The id of a plugin.

\Drupal\Component\Plugin\Definition\PluginDefinitionInterface|mixed[] $plugin_definition: The plugin definition associated with the plugin ID.

string $required_interface: (optional) The required plugin interface.

Return value

string The appropriate class name.

Throws

\Drupal\Component\Plugin\Exception\PluginException Thrown when there is no class specified, the class doesn't exist, or the class does not implement the specified required interface.

24 calls to DefaultFactory::getPluginClass()
ArchiverManager::createInstance in core/lib/Drupal/Core/Archiver/ArchiverManager.php
Creates a pre-configured instance of a plugin.
ContainerFactory::createInstance in core/lib/Drupal/Core/Plugin/Factory/ContainerFactory.php
Creates a pre-configured instance of a plugin.
DefaultFactory::createInstance in core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php
Creates a pre-configured instance of a plugin.
DefaultFactoryTest::testGetPluginClassWithInterfaceAndInvalidClassWithArrayPluginDefinition in core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php
Tests getPluginClass() with a required interface but no implementation.
DefaultFactoryTest::testGetPluginClassWithInterfaceAndInvalidClassWithObjectPluginDefinition in core/tests/Drupal/Tests/Component/Plugin/DefaultFactoryTest.php
Tests getPluginClass() with a required interface but no implementation.

... See full list

1 method overrides DefaultFactory::getPluginClass()
StubReflectionFactory::getPluginClass in core/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php
Finds the class relevant for a given plugin.

File

core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php, line 75

Class

DefaultFactory
Default plugin factory.

Namespace

Drupal\Component\Plugin\Factory

Code

public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
  $missing_class_message = sprintf('The plugin (%s) did not specify an instance class.', $plugin_id);
  if (is_array($plugin_definition)) {
    if (empty($plugin_definition['class'])) {
      throw new PluginException($missing_class_message);
    }
    $class = $plugin_definition['class'];
  }
  elseif ($plugin_definition instanceof PluginDefinitionInterface) {
    if (!$plugin_definition
      ->getClass()) {
      throw new PluginException($missing_class_message);
    }
    $class = $plugin_definition
      ->getClass();
  }
  else {
    $plugin_definition_type = is_object($plugin_definition) ? get_class($plugin_definition) : gettype($plugin_definition);
    throw new PluginException(sprintf('%s can only handle plugin definitions that are arrays or that implement %s, but %s given.', __CLASS__, PluginDefinitionInterface::class, $plugin_definition_type));
  }
  if (!class_exists($class)) {
    throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class));
  }
  if ($required_interface && !is_subclass_of($class, $required_interface)) {
    throw new PluginException(sprintf('Plugin "%s" (%s) must implement interface %s.', $plugin_id, $class, $required_interface));
  }
  return $class;
}