annotate core/lib/Drupal/Core/Condition/ConditionManager.php @ 19:fa3358dc1485 tip

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