Further modules included.
[yaffs-website] / web / modules / contrib / libraries / src / ExternalLibrary / Definition / ChainDefinitionDiscovery.php
1 <?php
2
3 namespace Drupal\libraries\ExternalLibrary\Definition;
4
5 use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
6
7 /**
8  * Provides a definition discovery that checks a list of other discoveries.
9  *
10  * The discoveries are checked sequentially. If the definition was not present
11  * in some discoveries but is found in a later discovery the definition will be
12  * written to the earlier discoveries if they implement
13  * WritableDefinitionDiscoveryInterface.
14  *
15  * @see \Drupal\libraries\ExternalLibrary\Definition\WritableDefinitionDiscoveryInterface
16  */
17 class ChainDefinitionDiscovery implements DefinitionDiscoveryInterface {
18
19   /**
20    * The list of definition discoveries that will be checked.
21    *
22    * @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface[]
23    */
24   protected $discoveries = [];
25
26   /**
27    * {@inheritdoc}
28    */
29   public function hasDefinition($id) {
30     foreach ($this->discoveries as $discovery) {
31       if ($discovery->hasDefinition($id)) {
32         return TRUE;
33       }
34     }
35
36     return FALSE;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public function getDefinition($id) {
43     /** @var \Drupal\libraries\ExternalLibrary\Definition\WritableDefinitionDiscoveryInterface[] $discoveries_to_write */
44     $discoveries_to_write = [];
45     foreach ($this->discoveries as $discovery) {
46       if ($discovery->hasDefinition($id)) {
47         $definition = $discovery->getDefinition($id);
48         break;
49       }
50       elseif ($discovery instanceof WritableDefinitionDiscoveryInterface) {
51         $discoveries_to_write[] = $discovery;
52       }
53     }
54
55     if (!isset($definition)) {
56       throw new LibraryDefinitionNotFoundException($id);
57     }
58
59     foreach ($discoveries_to_write as $discovery_to_write) {
60       $discovery_to_write->writeDefinition($id, $definition);
61     }
62
63     return $definition;
64   }
65
66   /**
67    * Adds a definition discovery to the list to check.
68    *
69    * @param \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface $discovery
70    *   The definition discovery to add.
71    *
72    * @return $this
73    */
74   public function addDiscovery(DefinitionDiscoveryInterface $discovery) {
75     $this->discoveries[] = $discovery;
76     return $this;
77   }
78
79 }