/** * @param string $pattern * @param \Closure|string $handler * @param string $name * @throws \Exception * @return Route */ public function map($pattern, $handler, $name = \null) { if (\null === $name) { $name = preg_replace('~[^\\w]~', '-', $pattern); $name = preg_replace('~[\\-]+~', '-', $name); $name = trim('route-' . trim($name, '-'), '-'); } if (isset($this->routes[$name])) { throw new CoreException(sprintf('[' . __METHOD__ . '] Route "%s" already exists!', $name), 500); } if (empty($pattern) || $pattern[0] !== static::URL_DELIMITER) { $pattern = static::URL_DELIMITER . $pattern; } if ($pattern !== static::URL_DELIMITER) { $pattern = rtrim($pattern, static::URL_DELIMITER); } $route = new Route($name, $pattern, $handler); $route->setContainer($this->container); if (Route::isStatic($pattern)) { if (isset($this->routesStatic[$pattern])) { throw new CoreException(sprintf('[' . __METHOD__ . '] Route pattern "%s" already exists!', $pattern), 500); } $this->routesStatic[$pattern] = $name; } $this->routes[$name] = $route; return $route; }