public function match(Request $request) { if (!$request instanceof ConsoleRequest) { return null; } $params = $request->getParams()->toArray(); if (!isset($params[0]) || !$this->application->has($params[0])) { return null; } return new RouteMatch($this->defaults); }
/** * match(): defined by Route interface. * * @see \Zend\Router\Route::match() * @param Request $request * @param null|int $pathOffset * @return RouteMatch */ public function match(Request $request, $pathOffset = null) { if (!$request instanceof ConsoleRequest) { return; } $params = $request->getParams()->toArray(); $matches = $this->matcher->match($params); if (null !== $matches) { return new RouteMatch($matches); } return; }
/** * Mimics zf1 Request::getParam behavior * * Route match -> GET -> POST */ public static function staticGetParam(RouteMatch $routeMatch, Request $request, $param = null, $default = null) { if ($param === null) { $params = (array) $routeMatch->getParams(); if ($request instanceof ConsoleRequest) { return $params + (array) $request->getParams(); } return $params + $request->getQuery()->toArray() + $request->getPost()->toArray(); } if ($request instanceof ConsoleRequest) { $default = $request->getParam($param, $default); } else { $default = $request->getQuery($param, $request->getPost($param, $default)); } return $routeMatch->getParam($param, $default); }
/** * match(): defined by Route interface. * * @see Route::match() * @param Request $request * @param null|int $pathOffset * @return RouteMatch */ public function match(Request $request, $pathOffset = null) { if (!$request instanceof ConsoleRequest) { return null; } /** @var $request ConsoleRequest */ /** @var $params \Zend\Stdlib\Parameters */ $params = $request->getParams()->toArray(); $matches = array(); /** * Extract positional and named parts */ $positional = $named = array(); foreach ($this->parts as &$part) { if ($part['positional']) { $positional[] =& $part; } else { $named[] =& $part; } } /** * Scan for named parts inside Console params */ foreach ($named as &$part) { /** * Prepare match regex */ if (isset($part['alternatives'])) { // an alternative of flags $regex = '/^\\-+(?P<name>'; $regex .= join('|', $part['alternatives']); if ($part['hasValue']) { $regex .= ')(?:\\=(?P<value>.*?)$)?$/'; } else { $regex .= ')$/i'; } } else { // a single named flag if ($part['short'] === true) { // short variant if ($part['hasValue']) { $regex = '/^\\-' . $part['name'] . '(?:\\=(?P<value>.*?)$)?$/i'; } else { $regex = '/^\\-' . $part['name'] . '$/i'; } } elseif ($part['short'] === false) { // long variant if ($part['hasValue']) { $regex = '/^\\-{2,}' . $part['name'] . '(?:\\=(?P<value>.*?)$)?$/i'; } else { $regex = '/^\\-{2,}' . $part['name'] . '$/i'; } } } /** * Look for param */ $value = $param = null; for ($x = 0, $count = count($params); $x < $count; $x++) { if (preg_match($regex, $params[$x], $m)) { // found param $param = $params[$x]; // prevent further scanning of this param array_splice($params, $x, 1); if (isset($m['value'])) { $value = $m['value']; } if (isset($m['name'])) { $matchedName = $m['name']; } break; } } if (!$param) { /** * Drop out if that was a mandatory param */ if ($part['required']) { return null; } else { continue; } } /** * Value for flags is always boolean */ if ($param && !$part['hasValue']) { $value = true; } /** * Try to retrieve value if it is expected */ if ((null === $value || "" === $value) && $part['hasValue']) { if ($x < count($params) + 1 && isset($params[$x])) { // retrieve value from adjacent param $value = $params[$x]; // prevent further scanning of this param array_splice($params, $x, 1); } else { // there are no more params available return null; } } /** * Validate the value against constraints */ if ($part['hasValue'] && isset($this->constraints[$part['name']])) { if (!preg_match($this->constraints[$part['name']], $value)) { // constraint failed return null; } } /** * Store the value */ if ($part['hasValue']) { $matches[$part['name']] = $value; } else { $matches[$part['name']] = true; } /** * If there are alternatives, fill them */ if (isset($part['alternatives'])) { if ($part['hasValue']) { foreach ($part['alternatives'] as $alt) { if ($alt === $matchedName && !isset($matches[$alt])) { $matches[$alt] = $value; } elseif (!isset($matches[$alt])) { $matches[$alt] = null; } } } else { foreach ($part['alternatives'] as $alt) { if ($alt === $matchedName && !isset($matches[$alt])) { $matches[$alt] = isset($this->defaults[$alt]) ? $this->defaults[$alt] : true; } elseif (!isset($matches[$alt])) { $matches[$alt] = false; } } } } } /** * Scan for left-out flags that should result in a mismatch */ foreach ($params as $param) { if (preg_match('#^\\-+#', $param)) { return null; // there is an unrecognized flag } } /** * Go through all positional params */ $argPos = 0; foreach ($positional as &$part) { /** * Check if param exists */ if (!isset($params[$argPos])) { if ($part['required']) { // cannot find required positional param return null; } else { // stop matching break; } } $value = $params[$argPos]; /** * Check if literal param matches */ if ($part['literal']) { if (isset($part['alternatives']) && !in_array($value, $part['alternatives']) || !isset($part['alternatives']) && $value != $part['name']) { return null; } } /** * Validate the value against constraints */ if ($part['hasValue'] && isset($this->constraints[$part['name']])) { if (!preg_match($this->constraints[$part['name']], $value)) { // constraint failed return null; } } /** * Store the value */ if ($part['hasValue']) { $matches[$part['name']] = $value; } elseif (isset($part['alternatives'])) { // from all alternativesm set matching parameter to TRUE and the rest to FALSE foreach ($part['alternatives'] as $alt) { if ($alt == $value) { $matches[$alt] = isset($this->defaults[$alt]) ? $this->defaults[$alt] : true; } else { $matches[$alt] = false; } } // set alternatives group value $matches[$part['name']] = $value; } elseif (!$part['required']) { // set optional parameter flag $name = $part['name']; $matches[$name] = isset($this->defaults[$name]) ? $this->defaults[$name] : true; } /** * Advance to next argument */ $argPos++; } /** * Check if we have consumed all positional parameters */ if ($argPos < count($params)) { return null; // there are extraneous params that were not consumed } /** * Any optional flags that were not entered have value false */ foreach ($this->parts as &$part) { if (!$part['required'] && !$part['hasValue']) { if (!isset($matches[$part['name']])) { $matches[$part['name']] = false; } // unset alternatives also should be false if (isset($part['alternatives'])) { foreach ($part['alternatives'] as $alt) { if (!isset($matches[$alt])) { $matches[$alt] = false; } } } } } return new RouteMatch(array_replace($this->defaults, $matches)); }