Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / Context / LazyContextRepository.php
1 <?php
2
3 namespace Drupal\Core\Plugin\Context;
4
5 use Symfony\Component\DependencyInjection\ContainerInterface;
6
7 /**
8  * Provides a context repository which uses context provider services.
9  */
10 class LazyContextRepository implements ContextRepositoryInterface {
11
12   /**
13    * The set of available context providers service IDs.
14    *
15    * @var string[]
16    *   Context provider service IDs.
17    */
18   protected $contextProviderServiceIDs = [];
19
20   /**
21    * The service container.
22    *
23    * @var \Symfony\Component\DependencyInjection\ContainerInterface
24    */
25   protected $container;
26
27   /**
28    * The statically cached contexts.
29    *
30    * @var \Drupal\Core\Plugin\Context\ContextInterface[]
31    */
32   protected $contexts = [];
33
34   /**
35    * Constructs a LazyContextRepository object.
36    *
37    * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
38    *   The current service container.
39    * @param string[] $context_provider_service_ids
40    *   The set of the available context provider service IDs.
41    */
42   public function __construct(ContainerInterface $container, array $context_provider_service_ids) {
43     $this->container = $container;
44     $this->contextProviderServiceIDs = $context_provider_service_ids;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function getRuntimeContexts(array $context_ids) {
51     $contexts = [];
52
53     // Create a map of context providers (service IDs) to unqualified context
54     // IDs.
55     $context_ids_by_service = [];
56     foreach ($context_ids as $id) {
57       if (isset($this->contexts[$id])) {
58         $contexts[$id] = $this->contexts[$id];
59         continue;
60       }
61       // The IDs have been passed in @{service_id}:{unqualified_context_id}
62       // format.
63       // @todo Convert to an assert once https://www.drupal.org/node/2408013 is
64       //   in.
65       if ($id[0] === '@' && strpos($id, ':') !== FALSE) {
66         list($service_id, $unqualified_context_id) = explode(':', $id, 2);
67         // Remove the leading '@'.
68         $service_id = substr($service_id, 1);
69       }
70       else {
71         throw new \InvalidArgumentException('You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
72       }
73       $context_ids_by_service[$service_id][] = $unqualified_context_id;
74     }
75
76     // Iterate over all missing context providers (services), gather the
77     // runtime contexts and assign them as requested.
78     foreach ($context_ids_by_service as $service_id => $unqualified_context_ids) {
79       $contexts_by_service = $this->container->get($service_id)->getRuntimeContexts($unqualified_context_ids);
80
81       $wanted_contexts = array_intersect_key($contexts_by_service, array_flip($unqualified_context_ids));
82       foreach ($wanted_contexts as $unqualified_context_id => $context) {
83         $context_id = '@' . $service_id . ':' . $unqualified_context_id;
84         $this->contexts[$context_id] = $contexts[$context_id] = $context;
85       }
86     }
87
88     return $contexts;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getAvailableContexts() {
95     $contexts = [];
96     foreach ($this->contextProviderServiceIDs as $service_id) {
97       $contexts_by_service = $this->container->get($service_id)->getAvailableContexts();
98       foreach ($contexts_by_service as $unqualified_context_id => $context) {
99         $context_id = '@' . $service_id . ':' . $unqualified_context_id;
100         $contexts[$context_id] = $context;
101       }
102     }
103
104     return $contexts;
105   }
106
107 }