ParameterMap.php in Drupal 7 to 8/9 Module Upgrader 8
File
src/Routing/ParameterMap.php
View source
<?php
namespace Drupal\drupalmoduleupgrader\Routing;
use Doctrine\Common\Collections\ArrayCollection;
use Drupal\drupalmoduleupgrader\Utility\Path\Drupal7\PathComponent;
use Drupal\drupalmoduleupgrader\Utility\Path\Drupal8\PathComponent as PathComponent8x;
use Drupal\drupalmoduleupgrader\Utility\Path\PathUtilityInterface;
use Symfony\Component\Routing\Route as Drupal8Route;
class ParameterMap extends ArrayCollection {
protected $path;
protected $_length = 0;
protected $bindings = [];
public function __construct(PathUtilityInterface $path, array $parameters, array $arguments = []) {
parent::__construct();
$this->path = $path;
$this->_length = sizeof($path);
while ($parameters) {
$argument = $arguments ? array_shift($arguments) : ParameterBinding::NO_ARGUMENT;
$this
->addBinding(new ParameterBinding($path, array_shift($parameters), $argument));
}
}
public function toArray() {
$output = [];
foreach ($this->bindings as $key => $bindings) {
if (is_integer($key)) {
foreach ($bindings as $binding) {
$parameter = $binding
->getParameter()
->getName();
$function = $binding
->getParameter()
->getFunction()
->getName()
->getText();
$output[$function][$parameter]['name'] = $bindings[0]
->getParameter()
->getName();
$value = $bindings[0]
->getValue();
if ($value instanceof PathComponent && $value
->isWildcard()) {
$output[$function][$parameter]['type'] = ltrim($value, '%');
}
}
}
}
return $output;
}
public function merge(ParameterMap $map) {
foreach ($map as $binding) {
$this
->addBinding($binding);
}
}
public function addBinding(ParameterBinding $binding) {
$value = $binding
->getValue();
if ($value instanceof PathComponent) {
if ($binding
->inPath()) {
$key = $binding
->getArgument();
}
else {
$key = $this->path
->indexOf($value);
if ($key === FALSE) {
$key = $this->_length++;
}
}
}
else {
$key = $binding
->getParameter()
->getName();
}
$this
->set($key, $binding);
if (!isset($this->bindings[$key])) {
$this->bindings[$key] = [];
}
array_unshift($this->bindings[$key], $binding);
}
public function applyPath(PathUtilityInterface $path) {
foreach ($this as $key => $binding) {
if (is_integer($key)) {
$path[$key] = new PathComponent8x('{' . $binding
->getParameter()
->getName() . '}');
}
}
}
public function applyRoute(Drupal8Route $route) {
$this
->applyPath($this->path);
foreach ($this as $key => $binding) {
$parameter = $binding
->getParameter();
if (is_integer($key)) {
if ($parameter
->isOptional()) {
$value = eval('return ' . $parameter
->getValue() . ';');
$route
->setDefault($parameter
->getName(), $value);
}
}
elseif ($binding
->hasArgument()) {
$route
->setDefault($parameter
->getName(), $binding
->getValue());
}
}
$route
->setPath($this->path
->__toString());
}
}
Classes
Name |
Description |
ParameterMap |
Represents a set of parameter bindings for a particular path, callback,
and set of arguments. |