3 namespace Drupal\libraries\ExternalLibrary\Definition;
5 use Drupal\libraries\ExternalLibrary\Exception\LibraryDefinitionNotFoundException;
8 * Provides a definition discovery that checks a list of other discoveries.
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.
15 * @see \Drupal\libraries\ExternalLibrary\Definition\WritableDefinitionDiscoveryInterface
17 class ChainDefinitionDiscovery implements DefinitionDiscoveryInterface {
20 * The list of definition discoveries that will be checked.
22 * @var \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface[]
24 protected $discoveries = [];
29 public function hasDefinition($id) {
30 foreach ($this->discoveries as $discovery) {
31 if ($discovery->hasDefinition($id)) {
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);
50 elseif ($discovery instanceof WritableDefinitionDiscoveryInterface) {
51 $discoveries_to_write[] = $discovery;
55 if (!isset($definition)) {
56 throw new LibraryDefinitionNotFoundException($id);
59 foreach ($discoveries_to_write as $discovery_to_write) {
60 $discovery_to_write->writeDefinition($id, $definition);
67 * Adds a definition discovery to the list to check.
69 * @param \Drupal\libraries\ExternalLibrary\Definition\DefinitionDiscoveryInterface $discovery
70 * The definition discovery to add.
74 public function addDiscovery(DefinitionDiscoveryInterface $discovery) {
75 $this->discoveries[] = $discovery;