Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Routing / LinkIndex.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Converter\Routing\LinkIndex.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Routing;
9
10 use Doctrine\Common\Collections\ArrayCollection;
11 use Drupal\drupalmoduleupgrader\Routing\LinkBinding\LinkBinding;
12
13 /**
14  * Represents a set of link bindings of a single type (i.e., menu links, local tasks, etc.)
15  */
16 class LinkIndex extends ArrayCollection {
17
18   /**
19    * Tracks link IDs to prevent collisions.
20    *
21    * @var string[]
22    */
23   protected $idiotBox = [];
24
25   /**
26    * Adds a binding to this index.
27    *
28    * @param \Drupal\drupalmoduleupgrader\Routing\LinkBinding\LinkBinding $binding
29    */
30   public function addBinding(LinkBinding $binding) {
31     $id = $binding->getIdentifier();
32
33     if (isset($this->idiotBox[$id])) {
34       $id .= '_' . $this->idiotBox[$id]++;
35     }
36     else {
37       $this->idiotBox[$id] = 0;
38     }
39
40     $this->set($binding->getSource()->getPath()->__toString(), $binding);
41     $binding->onIndexed($id, $this);
42   }
43
44   /**
45    * Builds all the links in this index and returns them as an array of arrays,
46    * keyed by link ID.
47    *
48    * @return array
49    */
50   public function build() {
51     $build = [];
52
53     foreach ($this as $binding) {
54       $build[ $binding->getIdentifier() ] = $binding->build();
55     }
56
57     return $build;
58   }
59
60 }