public function route($verb, $pattern) { if (!in_array(strtoupper($verb), array_merge($this->allowed, ['*']))) { throw new \Exception("HTTP verb not supported"); } if (!$pattern) { throw new \Exception("Pattern can't be empty."); } else { $this->next($this->parser->parsePattern($pattern))->verb = $verb; } return $this; }
public function testBestMatch() { $parser = new Parser(); $routes = [$parser->parsePattern("api/v1/users/{id}/*"), $parser->parsePattern("api/v1/users/{id}"), $parser->parsePattern("api/v1/*"), $parser->parsePattern("api/v1"), $parser->parsePattern("api/*"), $parser->parsePattern("*")]; $this->users->shouldReceive('getAuthUser')->andReturn(null); $this->routes->shouldReceive('getOrderedRoutes')->andReturn($routes); $this->assertEquals(".*", $this->manager->bestMatch('get', 'test')->pattern); $this->assertEquals("api/.*", $this->manager->bestMatch('get', 'api/1')->pattern); $this->assertEquals("api/.*", $this->manager->bestMatch('get', 'api/a')->pattern); $this->assertEquals("api/.*", $this->manager->bestMatch('get', 'api/v')->pattern); $this->assertEquals("api/v1", $this->manager->bestMatch('get', 'api/v1')->pattern); $this->assertEquals("api/v1", $this->manager->bestMatch('get', 'api/v1/')->pattern); $this->assertEquals("api/v1/.*", $this->manager->bestMatch('get', 'api/v1/whatever/1')->pattern); $this->assertEquals("api/v1/.*", $this->manager->bestMatch('get', 'api/v1/users/')->pattern); $this->assertEquals("api/v1/users/[0-9]+", $this->manager->bestMatch('get', 'api/v1/users/10')->pattern); $this->assertEquals("api/v1/users/[0-9]+/.*", $this->manager->bestMatch('get', 'api/v1/users/10/whatever')->pattern); }
/** * Override default implementation so that pattern * is properly trimmed before being persisted. * * @param $route * @return Route */ function update($route) { $route->pattern = $this->parser->trimPath($route->pattern); return parent::update($route); }