annotate core/lib/Drupal/Core/Condition/ConditionManager.php @ 0:4c8ae668cc8c

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