ConditionManager.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\Core\Condition;
  3. use Drupal\Component\Plugin\CategorizingPluginManagerInterface;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Executable\ExecutableException;
  6. use Drupal\Core\Executable\ExecutableManagerInterface;
  7. use Drupal\Core\Executable\ExecutableInterface;
  8. use Drupal\Core\Extension\ModuleHandlerInterface;
  9. use Drupal\Core\Plugin\CategorizingPluginManagerTrait;
  10. use Drupal\Core\Plugin\DefaultPluginManager;
  11. use Drupal\Core\Plugin\FilteredPluginManagerInterface;
  12. use Drupal\Core\Plugin\FilteredPluginManagerTrait;
  13. /**
  14. * A plugin manager for condition plugins.
  15. *
  16. * @see \Drupal\Core\Condition\Annotation\Condition
  17. * @see \Drupal\Core\Condition\ConditionInterface
  18. * @see \Drupal\Core\Condition\ConditionPluginBase
  19. *
  20. * @ingroup plugin_api
  21. */
  22. class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface, CategorizingPluginManagerInterface, FilteredPluginManagerInterface {
  23. use CategorizingPluginManagerTrait;
  24. use FilteredPluginManagerTrait;
  25. /**
  26. * Constructs a ConditionManager object.
  27. *
  28. * @param \Traversable $namespaces
  29. * An object that implements \Traversable which contains the root paths
  30. * keyed by the corresponding namespace to look for plugin implementations.
  31. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  32. * Cache backend instance to use.
  33. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  34. * The module handler to invoke the alter hook with.
  35. */
  36. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
  37. $this->alterInfo('condition_info');
  38. $this->setCacheBackend($cache_backend, 'condition_plugins');
  39. parent::__construct('Plugin/Condition', $namespaces, $module_handler, 'Drupal\Core\Condition\ConditionInterface', 'Drupal\Core\Condition\Annotation\Condition');
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function getType() {
  45. return 'condition';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function createInstance($plugin_id, array $configuration = []) {
  51. $plugin = $this->getFactory()->createInstance($plugin_id, $configuration);
  52. // If we receive any context values via config set it into the plugin.
  53. if (!empty($configuration['context'])) {
  54. foreach ($configuration['context'] as $name => $context) {
  55. $plugin->setContextValue($name, $context);
  56. }
  57. }
  58. return $plugin->setExecutableManager($this);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function execute(ExecutableInterface $condition) {
  64. if ($condition instanceof ConditionInterface) {
  65. $result = $condition->evaluate();
  66. return $condition->isNegated() ? !$result : $result;
  67. }
  68. throw new ExecutableException("This manager object can only execute condition plugins");
  69. }
  70. }