DevelDumperPluginManager.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\devel;
  3. use Drupal\Core\Cache\CacheBackendInterface;
  4. use Drupal\Core\Extension\ModuleHandlerInterface;
  5. use Drupal\Core\Plugin\DefaultPluginManager;
  6. use Drupal\devel\Annotation\DevelDumper;
  7. /**
  8. * Plugin type manager for Devel Dumper plugins.
  9. *
  10. * @see \Drupal\devel\Annotation\DevelDumper
  11. * @see \Drupal\devel\DevelDumperInterface
  12. * @see \Drupal\devel\DevelDumperBase
  13. * @see plugin_api
  14. */
  15. class DevelDumperPluginManager extends DefaultPluginManager implements DevelDumperPluginManagerInterface {
  16. /**
  17. * Constructs a DevelDumperPluginManager object.
  18. *
  19. * @param \Traversable $namespaces
  20. * An object that implements \Traversable which contains the root paths
  21. * keyed by the corresponding namespace to look for plugin implementations.
  22. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  23. * Cache backend instance to use.
  24. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  25. * The module handler to invoke the alter hook with.
  26. */
  27. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
  28. parent::__construct('Plugin/Devel/Dumper', $namespaces, $module_handler, DevelDumperInterface::class, DevelDumper::class);
  29. $this->setCacheBackend($cache_backend, 'devel_dumper_plugins');
  30. $this->alterInfo('devel_dumper_info');
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function processDefinition(&$definition, $plugin_id) {
  36. parent::processDefinition($definition, $plugin_id);
  37. $definition['supported'] = (bool) call_user_func([$definition['class'], 'checkRequirements']);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function isPluginSupported($plugin_id) {
  43. $definition = $this->getDefinition($plugin_id, FALSE);
  44. return $definition && $definition['supported'];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function createInstance($plugin_id, array $configuration = []) {
  50. if (!$this->isPluginSupported($plugin_id)) {
  51. $plugin_id = $this->getFallbackPluginId($plugin_id);
  52. }
  53. return parent::createInstance($plugin_id, $configuration);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getFallbackPluginId($plugin_id, array $configuration = []) {
  59. return 'default';
  60. }
  61. }