ConditionManager.php 2.4 KB

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