Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Render / Placeholder / ChainedPlaceholderStrategy.php
1 <?php
2
3 namespace Drupal\Core\Render\Placeholder;
4
5 /**
6  * Renders placeholders using a chain of placeholder strategies.
7  */
8 class ChainedPlaceholderStrategy implements PlaceholderStrategyInterface {
9
10   /**
11    * An ordered list of placeholder strategy services.
12    *
13    * Ordered according to service priority.
14    *
15    * @var \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface[]
16    */
17   protected $placeholderStrategies = [];
18
19   /**
20    * Adds a placeholder strategy to use.
21    *
22    * @param \Drupal\Core\Render\Placeholder\PlaceholderStrategyInterface $strategy
23    *   The strategy to add to the placeholder strategies.
24    */
25   public function addPlaceholderStrategy(PlaceholderStrategyInterface $strategy) {
26     $this->placeholderStrategies[] = $strategy;
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function processPlaceholders(array $placeholders) {
33     if (empty($placeholders)) {
34       return [];
35     }
36
37     // Assert that there is at least one strategy.
38     assert('!empty($this->placeholderStrategies)', 'At least one placeholder strategy must be present; by default the fallback strategy \Drupal\Core\Render\Placeholder\SingleFlushStrategy is always present.');
39
40     $new_placeholders = [];
41
42     // Give each placeholder strategy a chance to replace all not-yet replaced
43     // placeholders. The order of placeholder strategies is well defined
44     // and this uses a variation of the "chain of responsibility" design pattern.
45     foreach ($this->placeholderStrategies as $strategy) {
46       $processed_placeholders = $strategy->processPlaceholders($placeholders);
47       assert('array_intersect_key($processed_placeholders, $placeholders) === $processed_placeholders', 'Processed placeholders must be a subset of all placeholders.');
48       $placeholders = array_diff_key($placeholders, $processed_placeholders);
49       $new_placeholders += $processed_placeholders;
50
51       if (empty($placeholders)) {
52         break;
53       }
54     }
55
56     return $new_placeholders;
57   }
58
59 }