Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Routing / ParameterMap.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Routing;
4
5 use Doctrine\Common\Collections\ArrayCollection;
6 use Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathComponent;
7 use Drupal\drupalmoduleupgrader\Utility\Path\Drupal8\PathComponent as PathComponent8x;
8 use Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityInterface;
9 use Symfony\Component\Routing\Route as Drupal8Route;
10
11 /**
12  * Represents a set of parameter bindings for a particular path, callback,
13  * and set of arguments.
14  */
15 class ParameterMap extends ArrayCollection {
16
17   /**
18    * @var \Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityInterface
19    */
20   protected $path;
21
22   /**
23    * @var integer
24    */
25   protected $_length = 0;
26
27   protected $bindings = [];
28
29   /**
30    * {@inheritdoc}
31    */
32   public function __construct(PathUtilityInterface $path, array $parameters, array $arguments = []) {
33     parent::__construct();
34     $this->path = $path;
35     $this->_length = sizeof($path);
36
37     while ($parameters) {
38       $argument = $arguments ? array_shift($arguments) : ParameterBinding::NO_ARGUMENT;
39       $this->addBinding(new ParameterBinding($path, array_shift($parameters), $argument));
40     }
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function toArray() {
47     $output = [];
48
49     foreach ($this->bindings as $key => $bindings) {
50       if (is_integer($key)) {
51         /** @var ParameterBinding[] $bindings */
52         foreach ($bindings as $binding) {
53           $parameter = $binding->getParameter()->getName();
54           $function = $binding->getParameter()->getFunction()->getName()->getText();
55           $output[$function][$parameter]['name'] = $bindings[0]->getParameter()->getName();
56
57           $value = $bindings[0]->getValue();
58           if ($value instanceof PathComponent && $value->isWildcard()) {
59             $output[$function][$parameter]['type'] = ltrim($value, '%');
60           }
61         }
62       }
63     }
64
65     return $output;
66   }
67
68   /**
69    * Merge another parameter map into this one. Bindings from the incoming map
70    * should 'win', although the specifics are up to the implementing classes.
71    *
72    * @param ParameterMap $map
73    *  The parameter map to merge.
74    */
75   public function merge(ParameterMap $map) {
76     foreach ($map as $binding) {
77       $this->addBinding($binding);
78     }
79   }
80
81   /**
82    * Adds a binding to this map, overwriting the existing one if there is a
83    * conflict.
84    *
85    * @param ParameterBinding $binding
86    *  The binding to add.
87    */
88   public function addBinding(ParameterBinding $binding) {
89     $value = $binding->getValue();
90     // The binding will return a PathComponent if it expects to be physically
91     // represented in the path, whether or not it already is.
92     if ($value instanceof PathComponent) {
93       if ($binding->inPath()) {
94         $key = $binding->getArgument();
95       }
96       else {
97         $key = $this->path->indexOf($value);
98         if ($key === FALSE) {
99           $key = $this->_length++;
100         }
101       }
102     }
103     else {
104       $key = $binding->getParameter()->getName();
105     }
106
107     $this->set($key, $binding);
108
109     if (! isset($this->bindings[$key])) {
110       $this->bindings[$key] = [];
111     }
112     array_unshift($this->bindings[$key], $binding);
113   }
114
115   /**
116    * Applies the parameter map to a path, modifying it as needed.
117    *
118    * @param \Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityInterface $path
119    *  The path to modify (in-place).
120    */
121   public function applyPath(PathUtilityInterface $path) {
122     foreach ($this as $key => $binding) {
123       if (is_integer($key)) {
124         $path[$key] = new PathComponent8x('{' . $binding->getParameter()->getName() . '}');
125       }
126     }
127   }
128
129   /**
130    * Apply the parameter map to a Drupal 8 route, modifying it as needed.
131    *
132    * @param \Symfony\Component\Routing\Route $route
133    *  The route to process.
134    */
135   public function applyRoute(Drupal8Route $route) {
136     $this->applyPath($this->path);
137
138     foreach ($this as $key => $binding) {
139       $parameter = $binding->getParameter();
140
141       /** @var ParameterBinding $binding */
142       if (is_integer($key)) {
143         if ($parameter->isOptional()) {
144           // @todo Don't use eval().
145           $value = eval('return ' . $parameter->getValue() . ';');
146           $route->setDefault($parameter->getName(), $value);
147         }
148       }
149       elseif ($binding->hasArgument()) {
150         $route->setDefault($parameter->getName(), $binding->getValue());
151       }
152     }
153     $route->setPath($this->path->__toString());
154   }
155
156 }