ConditionManager.php 2.43 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\Core\Condition\ConditionManager.
 */

namespace Drupal\Core\Condition;

10
use Drupal\Core\Cache\CacheBackendInterface;
11 12
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Executable\ExecutableInterface;
13
use Drupal\Core\Extension\ModuleHandlerInterface;
14
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
15
use Drupal\Core\Plugin\DefaultPluginManager;
16 17 18

/**
 * A plugin manager for condition plugins.
19 20 21 22 23 24
 *
 * @see \Drupal\Core\Condition\Annotation\Condition
 * @see \Drupal\Core\Condition\ConditionInterface
 * @see \Drupal\Core\Condition\ConditionPluginBase
 *
 * @ingroup plugin_api
25
 */
26
class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface {
27

28 29
  use ContextAwarePluginManagerTrait;

30
  /**
31
   * Constructs a ConditionManager object.
32
   *
33 34
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
35 36 37 38 39
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
40
   */
41
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
42
    $this->alterInfo('condition_info');
43
    $this->setCacheBackend($cache_backend, 'condition_plugins');
44

45
    parent::__construct('Plugin/Condition', $namespaces, $module_handler, 'Drupal\Core\Condition\ConditionInterface', 'Drupal\Core\Condition\Annotation\Condition');
46 47 48 49 50 51 52
  }

  /**
   * Override of Drupal\Component\Plugin\PluginManagerBase::createInstance().
   */
  public function createInstance($plugin_id, array $configuration = array()) {
    $plugin = $this->factory->createInstance($plugin_id, $configuration);
53 54 55 56 57 58 59 60

    // If we receive any context values via config set it into the plugin.
    if (!empty($configuration['context'])) {
      foreach ($configuration['context'] as $name => $context) {
        $plugin->setContextValue($name, $context);
      }
    }

61 62 63 64 65 66 67 68 69 70 71 72
    return $plugin->setExecutableManager($this);
  }

  /**
   * Implements Drupal\Core\Executable\ExecutableManagerInterface::execute().
   */
  public function execute(ExecutableInterface $condition) {
    $result = $condition->evaluate();
    return $condition->isNegated() ? !$result : $result;
  }

}