FastRoute-1.3.0/0000755000175000017500000000000013240644777012425 5ustar jamesjamesFastRoute-1.3.0/README.md0000644000175000017500000002555713240644777013722 0ustar jamesjamesFastRoute - Fast request router for PHP ======================================= This library provides a fast implementation of a regular expression based router. [Blog post explaining how the implementation works and why it is fast.][blog_post] Install ------- To install with composer: ```sh composer require nikic/fast-route ``` Requires PHP 5.4 or newer. Usage ----- Here's a basic usage example: ```php addRoute('GET', '/users', 'get_all_users_handler'); // {id} must be a number (\d+) $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler'); // The /{title} suffix is optional $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler'); }); // Fetch method and URI from somewhere $httpMethod = $_SERVER['REQUEST_METHOD']; $uri = $_SERVER['REQUEST_URI']; // Strip query string (?foo=bar) and decode URI if (false !== $pos = strpos($uri, '?')) { $uri = substr($uri, 0, $pos); } $uri = rawurldecode($uri); $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case FastRoute\Dispatcher::NOT_FOUND: // ... 404 Not Found break; case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; // ... 405 Method Not Allowed break; case FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; // ... call $handler with $vars break; } ``` ### Defining routes The routes are defined by calling the `FastRoute\simpleDispatcher()` function, which accepts a callable taking a `FastRoute\RouteCollector` instance. The routes are added by calling `addRoute()` on the collector instance: ```php $r->addRoute($method, $routePattern, $handler); ``` The `$method` is an uppercase HTTP method string for which a certain route should match. It is possible to specify multiple valid methods using an array: ```php // These two calls $r->addRoute('GET', '/test', 'handler'); $r->addRoute('POST', '/test', 'handler'); // Are equivalent to this one call $r->addRoute(['GET', 'POST'], '/test', 'handler'); ``` By default the `$routePattern` uses a syntax where `{foo}` specifies a placeholder with name `foo` and matching the regex `[^/]+`. To adjust the pattern the placeholder matches, you can specify a custom pattern by writing `{bar:[0-9]+}`. Some examples: ```php // Matches /user/42, but not /user/xyz $r->addRoute('GET', '/user/{id:\d+}', 'handler'); // Matches /user/foobar, but not /user/foo/bar $r->addRoute('GET', '/user/{name}', 'handler'); // Matches /user/foo/bar as well $r->addRoute('GET', '/user/{name:.+}', 'handler'); ``` Custom patterns for route placeholders cannot use capturing groups. For example `{lang:(en|de)}` is not a valid placeholder, because `()` is a capturing group. Instead you can use either `{lang:en|de}` or `{lang:(?:en|de)}`. Furthermore parts of the route enclosed in `[...]` are considered optional, so that `/foo[bar]` will match both `/foo` and `/foobar`. Optional parts are only supported in a trailing position, not in the middle of a route. ```php // This route $r->addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler'); // Is equivalent to these two routes $r->addRoute('GET', '/user/{id:\d+}', 'handler'); $r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler'); // Multiple nested optional parts are possible as well $r->addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler'); // This route is NOT valid, because optional parts can only occur at the end $r->addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler'); ``` The `$handler` parameter does not necessarily have to be a callback, it could also be a controller class name or any other kind of data you wish to associate with the route. FastRoute only tells you which handler corresponds to your URI, how you interpret it is up to you. #### Shorcut methods for common request methods For the `GET`, `POST`, `PUT`, `PATCH`, `DELETE` and `HEAD` request methods shortcut methods are available. For example: ```php $r->get('/get-route', 'get_handler'); $r->post('/post-route', 'post_handler'); ``` Is equivalent to: ```php $r->addRoute('GET', '/get-route', 'get_handler'); $r->addRoute('POST', '/post-route', 'post_handler'); ``` #### Route Groups Additionally, you can specify routes inside of a group. All routes defined inside a group will have a common prefix. For example, defining your routes as: ```php $r->addGroup('/admin', function (RouteCollector $r) { $r->addRoute('GET', '/do-something', 'handler'); $r->addRoute('GET', '/do-another-thing', 'handler'); $r->addRoute('GET', '/do-something-else', 'handler'); }); ``` Will have the same result as: ```php $r->addRoute('GET', '/admin/do-something', 'handler'); $r->addRoute('GET', '/admin/do-another-thing', 'handler'); $r->addRoute('GET', '/admin/do-something-else', 'handler'); ``` Nested groups are also supported, in which case the prefixes of all the nested groups are combined. ### Caching The reason `simpleDispatcher` accepts a callback for defining the routes is to allow seamless caching. By using `cachedDispatcher` instead of `simpleDispatcher` you can cache the generated routing data and construct the dispatcher from the cached information: ```php addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }, [ 'cacheFile' => __DIR__ . '/route.cache', /* required */ 'cacheDisabled' => IS_DEBUG_ENABLED, /* optional, enabled by default */ ]); ``` The second parameter to the function is an options array, which can be used to specify the cache file location, among other things. ### Dispatching a URI A URI is dispatched by calling the `dispatch()` method of the created dispatcher. This method accepts the HTTP method and a URI. Getting those two bits of information (and normalizing them appropriately) is your job - this library is not bound to the PHP web SAPIs. The `dispatch()` method returns an array whose first element contains a status code. It is one of `Dispatcher::NOT_FOUND`, `Dispatcher::METHOD_NOT_ALLOWED` and `Dispatcher::FOUND`. For the method not allowed status the second array element contains a list of HTTP methods allowed for the supplied URI. For example: [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']] > **NOTE:** The HTTP specification requires that a `405 Method Not Allowed` response include the `Allow:` header to detail available methods for the requested resource. Applications using FastRoute should use the second array element to add this header when relaying a 405 response. For the found status the second array element is the handler that was associated with the route and the third array element is a dictionary of placeholder names to their values. For example: /* Routing against GET /user/nikic/42 */ [FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']] ### Overriding the route parser and dispatcher The routing process makes use of three components: A route parser, a data generator and a dispatcher. The three components adhere to the following interfaces: ```php 'FastRoute\\RouteParser\\Std', 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', ]); ``` The above options array corresponds to the defaults. By replacing `GroupCountBased` by `GroupPosBased` you could switch to a different dispatching strategy. ### A Note on HEAD Requests The HTTP spec requires servers to [support both GET and HEAD methods][2616-511]: > The methods GET and HEAD MUST be supported by all general-purpose servers To avoid forcing users to manually register HEAD routes for each resource we fallback to matching an available GET route for a given resource. The PHP web SAPI transparently removes the entity body from HEAD responses so this behavior has no effect on the vast majority of users. However, implementers using FastRoute outside the web SAPI environment (e.g. a custom server) MUST NOT send entity bodies generated in response to HEAD requests. If you are a non-SAPI user this is *your responsibility*; FastRoute has no purview to prevent you from breaking HTTP in such cases. Finally, note that applications MAY always specify their own HEAD method route for a given resource to bypass this behavior entirely. ### Credits This library is based on a router that [Levi Morrison][levi] implemented for the Aerys server. A large number of tests, as well as HTTP compliance considerations, were provided by [Daniel Lowrey][rdlowrey]. [2616-511]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 "RFC 2616 Section 5.1.1" [blog_post]: http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html [levi]: https://github.com/morrisonlevi [rdlowrey]: https://github.com/rdlowrey FastRoute-1.3.0/LICENSE0000644000175000017500000000272313240644777013436 0ustar jamesjamesCopyright (c) 2013 by Nikita Popov. Some rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. FastRoute-1.3.0/psalm.xml0000644000175000017500000000156513240644777014272 0ustar jamesjames FastRoute-1.3.0/src/0000755000175000017500000000000013240644777013214 5ustar jamesjamesFastRoute-1.3.0/src/Route.php0000644000175000017500000000163113240644777015024 0ustar jamesjameshttpMethod = $httpMethod; $this->handler = $handler; $this->regex = $regex; $this->variables = $variables; } /** * Tests whether this route matches the given string. * * @param string $str * * @return bool */ public function matches($str) { $regex = '~^' . $this->regex . '$~'; return (bool) preg_match($regex, $str); } } FastRoute-1.3.0/src/DataGenerator/0000755000175000017500000000000013240644777015734 5ustar jamesjamesFastRoute-1.3.0/src/DataGenerator/MarkBased.php0000644000175000017500000000122013240644777020271 0ustar jamesjames $route) { $regexes[] = $regex . '(*MARK:' . $markName . ')'; $routeMap[$markName] = [$route->handler, $route->variables]; ++$markName; } $regex = '~^(?|' . implode('|', $regexes) . ')$~'; return ['regex' => $regex, 'routeMap' => $routeMap]; } } FastRoute-1.3.0/src/DataGenerator/CharCountBased.php0000644000175000017500000000146713240644777021302 0ustar jamesjames $route) { $suffixLen++; $suffix .= "\t"; $regexes[] = '(?:' . $regex . '/(\t{' . $suffixLen . '})\t{' . ($count - $suffixLen) . '})'; $routeMap[$suffix] = [$route->handler, $route->variables]; } $regex = '~^(?|' . implode('|', $regexes) . ')$~'; return ['regex' => $regex, 'suffix' => '/' . $suffix, 'routeMap' => $routeMap]; } } FastRoute-1.3.0/src/DataGenerator/GroupPosBased.php0000644000175000017500000000121013240644777021154 0ustar jamesjames $route) { $regexes[] = $regex; $routeMap[$offset] = [$route->handler, $route->variables]; $offset += count($route->variables); } $regex = '~^(?:' . implode('|', $regexes) . ')$~'; return ['regex' => $regex, 'routeMap' => $routeMap]; } } FastRoute-1.3.0/src/DataGenerator/GroupCountBased.php0000644000175000017500000000143413240644777021513 0ustar jamesjames $route) { $numVariables = count($route->variables); $numGroups = max($numGroups, $numVariables); $regexes[] = $regex . str_repeat('()', $numGroups - $numVariables); $routeMap[$numGroups + 1] = [$route->handler, $route->variables]; ++$numGroups; } $regex = '~^(?|' . implode('|', $regexes) . ')$~'; return ['regex' => $regex, 'routeMap' => $routeMap]; } } FastRoute-1.3.0/src/DataGenerator/RegexBasedAbstract.php0000644000175000017500000001211113240644777022136 0ustar jamesjamesisStaticRoute($routeData)) { $this->addStaticRoute($httpMethod, $routeData, $handler); } else { $this->addVariableRoute($httpMethod, $routeData, $handler); } } /** * @return mixed[] */ public function getData() { if (empty($this->methodToRegexToRoutesMap)) { return [$this->staticRoutes, []]; } return [$this->staticRoutes, $this->generateVariableRouteData()]; } /** * @return mixed[] */ private function generateVariableRouteData() { $data = []; foreach ($this->methodToRegexToRoutesMap as $method => $regexToRoutesMap) { $chunkSize = $this->computeChunkSize(count($regexToRoutesMap)); $chunks = array_chunk($regexToRoutesMap, $chunkSize, true); $data[$method] = array_map([$this, 'processChunk'], $chunks); } return $data; } /** * @param int * @return int */ private function computeChunkSize($count) { $numParts = max(1, round($count / $this->getApproxChunkSize())); return (int) ceil($count / $numParts); } /** * @param mixed[] * @return bool */ private function isStaticRoute($routeData) { return count($routeData) === 1 && is_string($routeData[0]); } private function addStaticRoute($httpMethod, $routeData, $handler) { $routeStr = $routeData[0]; if (isset($this->staticRoutes[$httpMethod][$routeStr])) { throw new BadRouteException(sprintf( 'Cannot register two routes matching "%s" for method "%s"', $routeStr, $httpMethod )); } if (isset($this->methodToRegexToRoutesMap[$httpMethod])) { foreach ($this->methodToRegexToRoutesMap[$httpMethod] as $route) { if ($route->matches($routeStr)) { throw new BadRouteException(sprintf( 'Static route "%s" is shadowed by previously defined variable route "%s" for method "%s"', $routeStr, $route->regex, $httpMethod )); } } } $this->staticRoutes[$httpMethod][$routeStr] = $handler; } private function addVariableRoute($httpMethod, $routeData, $handler) { list($regex, $variables) = $this->buildRegexForRoute($routeData); if (isset($this->methodToRegexToRoutesMap[$httpMethod][$regex])) { throw new BadRouteException(sprintf( 'Cannot register two routes matching "%s" for method "%s"', $regex, $httpMethod )); } $this->methodToRegexToRoutesMap[$httpMethod][$regex] = new Route( $httpMethod, $handler, $regex, $variables ); } /** * @param mixed[] * @return mixed[] */ private function buildRegexForRoute($routeData) { $regex = ''; $variables = []; foreach ($routeData as $part) { if (is_string($part)) { $regex .= preg_quote($part, '~'); continue; } list($varName, $regexPart) = $part; if (isset($variables[$varName])) { throw new BadRouteException(sprintf( 'Cannot use the same placeholder "%s" twice', $varName )); } if ($this->regexHasCapturingGroups($regexPart)) { throw new BadRouteException(sprintf( 'Regex "%s" for parameter "%s" contains a capturing group', $regexPart, $varName )); } $variables[$varName] = $varName; $regex .= '(' . $regexPart . ')'; } return [$regex, $variables]; } /** * @param string * @return bool */ private function regexHasCapturingGroups($regex) { if (false === strpos($regex, '(')) { // Needs to have at least a ( to contain a capturing group return false; } // Semi-accurate detection for capturing groups return (bool) preg_match( '~ (?: \(\?\( | \[ [^\]\\\\]* (?: \\\\ . [^\]\\\\]* )* \] | \\\\ . ) (*SKIP)(*FAIL) | \( (?! \? (?! <(?![!=]) | P< | \' ) | \* ) ~x', $regex ); } } FastRoute-1.3.0/src/Dispatcher.php0000644000175000017500000000112213240644777016007 0ustar jamesjames 'value', ...]] * * @param string $httpMethod * @param string $uri * * @return array */ public function dispatch($httpMethod, $uri); } FastRoute-1.3.0/src/bootstrap.php0000644000175000017500000000044613240644777015746 0ustar jamesjames 'FastRoute\\RouteParser\\Std', 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', 'routeCollector' => 'FastRoute\\RouteCollector', ]; /** @var RouteCollector $routeCollector */ $routeCollector = new $options['routeCollector']( new $options['routeParser'], new $options['dataGenerator'] ); $routeDefinitionCallback($routeCollector); return new $options['dispatcher']($routeCollector->getData()); } /** * @param callable $routeDefinitionCallback * @param array $options * * @return Dispatcher */ function cachedDispatcher(callable $routeDefinitionCallback, array $options = []) { $options += [ 'routeParser' => 'FastRoute\\RouteParser\\Std', 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased', 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased', 'routeCollector' => 'FastRoute\\RouteCollector', 'cacheDisabled' => false, ]; if (!isset($options['cacheFile'])) { throw new \LogicException('Must specify "cacheFile" option'); } if (!$options['cacheDisabled'] && file_exists($options['cacheFile'])) { $dispatchData = require $options['cacheFile']; if (!is_array($dispatchData)) { throw new \RuntimeException('Invalid cache file "' . $options['cacheFile'] . '"'); } return new $options['dispatcher']($dispatchData); } $routeCollector = new $options['routeCollector']( new $options['routeParser'], new $options['dataGenerator'] ); $routeDefinitionCallback($routeCollector); /** @var RouteCollector $routeCollector */ $dispatchData = $routeCollector->getData(); if (!$options['cacheDisabled']) { file_put_contents( $options['cacheFile'], 'routeParser = $routeParser; $this->dataGenerator = $dataGenerator; $this->currentGroupPrefix = ''; } /** * Adds a route to the collection. * * The syntax used in the $route string depends on the used route parser. * * @param string|string[] $httpMethod * @param string $route * @param mixed $handler */ public function addRoute($httpMethod, $route, $handler) { $route = $this->currentGroupPrefix . $route; $routeDatas = $this->routeParser->parse($route); foreach ((array) $httpMethod as $method) { foreach ($routeDatas as $routeData) { $this->dataGenerator->addRoute($method, $routeData, $handler); } } } /** * Create a route group with a common prefix. * * All routes created in the passed callback will have the given group prefix prepended. * * @param string $prefix * @param callable $callback */ public function addGroup($prefix, callable $callback) { $previousGroupPrefix = $this->currentGroupPrefix; $this->currentGroupPrefix = $previousGroupPrefix . $prefix; $callback($this); $this->currentGroupPrefix = $previousGroupPrefix; } /** * Adds a GET route to the collection * * This is simply an alias of $this->addRoute('GET', $route, $handler) * * @param string $route * @param mixed $handler */ public function get($route, $handler) { $this->addRoute('GET', $route, $handler); } /** * Adds a POST route to the collection * * This is simply an alias of $this->addRoute('POST', $route, $handler) * * @param string $route * @param mixed $handler */ public function post($route, $handler) { $this->addRoute('POST', $route, $handler); } /** * Adds a PUT route to the collection * * This is simply an alias of $this->addRoute('PUT', $route, $handler) * * @param string $route * @param mixed $handler */ public function put($route, $handler) { $this->addRoute('PUT', $route, $handler); } /** * Adds a DELETE route to the collection * * This is simply an alias of $this->addRoute('DELETE', $route, $handler) * * @param string $route * @param mixed $handler */ public function delete($route, $handler) { $this->addRoute('DELETE', $route, $handler); } /** * Adds a PATCH route to the collection * * This is simply an alias of $this->addRoute('PATCH', $route, $handler) * * @param string $route * @param mixed $handler */ public function patch($route, $handler) { $this->addRoute('PATCH', $route, $handler); } /** * Adds a HEAD route to the collection * * This is simply an alias of $this->addRoute('HEAD', $route, $handler) * * @param string $route * @param mixed $handler */ public function head($route, $handler) { $this->addRoute('HEAD', $route, $handler); } /** * Returns the collected route data, as provided by the data generator. * * @return array */ public function getData() { return $this->dataGenerator->getData(); } } FastRoute-1.3.0/src/RouteParser/0000755000175000017500000000000013240644777015467 5ustar jamesjamesFastRoute-1.3.0/src/RouteParser/Std.php0000644000175000017500000000504613240644777016737 0ustar jamesjames $segment) { if ($segment === '' && $n !== 0) { throw new BadRouteException('Empty optional part'); } $currentRoute .= $segment; $routeDatas[] = $this->parsePlaceholders($currentRoute); } return $routeDatas; } /** * Parses a route string that does not contain optional segments. * * @param string * @return mixed[] */ private function parsePlaceholders($route) { if (!preg_match_all( '~' . self::VARIABLE_REGEX . '~x', $route, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER )) { return [$route]; } $offset = 0; $routeData = []; foreach ($matches as $set) { if ($set[0][1] > $offset) { $routeData[] = substr($route, $offset, $set[0][1] - $offset); } $routeData[] = [ $set[1][0], isset($set[2]) ? trim($set[2][0]) : self::DEFAULT_DISPATCH_REGEX ]; $offset = $set[0][1] + strlen($set[0][0]); } if ($offset !== strlen($route)) { $routeData[] = substr($route, $offset); } return $routeData; } } FastRoute-1.3.0/src/BadRouteException.php0000644000175000017500000000012113240644777017303 0ustar jamesjamesstaticRouteMap, $this->variableRouteData) = $data; } protected function dispatchVariableRoute($routeData, $uri) { foreach ($routeData as $data) { if (!preg_match($data['regex'], $uri, $matches)) { continue; } list($handler, $varNames) = $data['routeMap'][$matches['MARK']]; $vars = []; $i = 0; foreach ($varNames as $varName) { $vars[$varName] = $matches[++$i]; } return [self::FOUND, $handler, $vars]; } return [self::NOT_FOUND]; } } FastRoute-1.3.0/src/Dispatcher/CharCountBased.php0000644000175000017500000000141113240644777020635 0ustar jamesjamesstaticRouteMap, $this->variableRouteData) = $data; } protected function dispatchVariableRoute($routeData, $uri) { foreach ($routeData as $data) { if (!preg_match($data['regex'], $uri . $data['suffix'], $matches)) { continue; } list($handler, $varNames) = $data['routeMap'][end($matches)]; $vars = []; $i = 0; foreach ($varNames as $varName) { $vars[$varName] = $matches[++$i]; } return [self::FOUND, $handler, $vars]; } return [self::NOT_FOUND]; } } FastRoute-1.3.0/src/Dispatcher/GroupPosBased.php0000644000175000017500000000146713240644777020540 0ustar jamesjamesstaticRouteMap, $this->variableRouteData) = $data; } protected function dispatchVariableRoute($routeData, $uri) { foreach ($routeData as $data) { if (!preg_match($data['regex'], $uri, $matches)) { continue; } // find first non-empty match for ($i = 1; '' === $matches[$i]; ++$i); list($handler, $varNames) = $data['routeMap'][$i]; $vars = []; foreach ($varNames as $varName) { $vars[$varName] = $matches[$i++]; } return [self::FOUND, $handler, $vars]; } return [self::NOT_FOUND]; } } FastRoute-1.3.0/src/Dispatcher/GroupCountBased.php0000644000175000017500000000137213240644777021062 0ustar jamesjamesstaticRouteMap, $this->variableRouteData) = $data; } protected function dispatchVariableRoute($routeData, $uri) { foreach ($routeData as $data) { if (!preg_match($data['regex'], $uri, $matches)) { continue; } list($handler, $varNames) = $data['routeMap'][count($matches)]; $vars = []; $i = 0; foreach ($varNames as $varName) { $vars[$varName] = $matches[++$i]; } return [self::FOUND, $handler, $vars]; } return [self::NOT_FOUND]; } } FastRoute-1.3.0/src/Dispatcher/RegexBasedAbstract.php0000644000175000017500000000531413240644777021513 0ustar jamesjamesstaticRouteMap[$httpMethod][$uri])) { $handler = $this->staticRouteMap[$httpMethod][$uri]; return [self::FOUND, $handler, []]; } $varRouteData = $this->variableRouteData; if (isset($varRouteData[$httpMethod])) { $result = $this->dispatchVariableRoute($varRouteData[$httpMethod], $uri); if ($result[0] === self::FOUND) { return $result; } } // For HEAD requests, attempt fallback to GET if ($httpMethod === 'HEAD') { if (isset($this->staticRouteMap['GET'][$uri])) { $handler = $this->staticRouteMap['GET'][$uri]; return [self::FOUND, $handler, []]; } if (isset($varRouteData['GET'])) { $result = $this->dispatchVariableRoute($varRouteData['GET'], $uri); if ($result[0] === self::FOUND) { return $result; } } } // If nothing else matches, try fallback routes if (isset($this->staticRouteMap['*'][$uri])) { $handler = $this->staticRouteMap['*'][$uri]; return [self::FOUND, $handler, []]; } if (isset($varRouteData['*'])) { $result = $this->dispatchVariableRoute($varRouteData['*'], $uri); if ($result[0] === self::FOUND) { return $result; } } // Find allowed methods for this URI by matching against all other HTTP methods as well $allowedMethods = []; foreach ($this->staticRouteMap as $method => $uriMap) { if ($method !== $httpMethod && isset($uriMap[$uri])) { $allowedMethods[] = $method; } } foreach ($varRouteData as $method => $routeData) { if ($method === $httpMethod) { continue; } $result = $this->dispatchVariableRoute($routeData, $uri); if ($result[0] === self::FOUND) { $allowedMethods[] = $method; } } // If there are no allowed methods the route simply does not exist if ($allowedMethods) { return [self::METHOD_NOT_ALLOWED, $allowedMethods]; } return [self::NOT_FOUND]; } } FastRoute-1.3.0/test/0000755000175000017500000000000013240644777013404 5ustar jamesjamesFastRoute-1.3.0/test/RouteCollectorTest.php0000644000175000017500000000645413240644777017733 0ustar jamesjamesdelete('/delete', 'delete'); $r->get('/get', 'get'); $r->head('/head', 'head'); $r->patch('/patch', 'patch'); $r->post('/post', 'post'); $r->put('/put', 'put'); $expected = [ ['DELETE', '/delete', 'delete'], ['GET', '/get', 'get'], ['HEAD', '/head', 'head'], ['PATCH', '/patch', 'patch'], ['POST', '/post', 'post'], ['PUT', '/put', 'put'], ]; $this->assertSame($expected, $r->routes); } public function testGroups() { $r = new DummyRouteCollector(); $r->delete('/delete', 'delete'); $r->get('/get', 'get'); $r->head('/head', 'head'); $r->patch('/patch', 'patch'); $r->post('/post', 'post'); $r->put('/put', 'put'); $r->addGroup('/group-one', function (DummyRouteCollector $r) { $r->delete('/delete', 'delete'); $r->get('/get', 'get'); $r->head('/head', 'head'); $r->patch('/patch', 'patch'); $r->post('/post', 'post'); $r->put('/put', 'put'); $r->addGroup('/group-two', function (DummyRouteCollector $r) { $r->delete('/delete', 'delete'); $r->get('/get', 'get'); $r->head('/head', 'head'); $r->patch('/patch', 'patch'); $r->post('/post', 'post'); $r->put('/put', 'put'); }); }); $r->addGroup('/admin', function (DummyRouteCollector $r) { $r->get('-some-info', 'admin-some-info'); }); $r->addGroup('/admin-', function (DummyRouteCollector $r) { $r->get('more-info', 'admin-more-info'); }); $expected = [ ['DELETE', '/delete', 'delete'], ['GET', '/get', 'get'], ['HEAD', '/head', 'head'], ['PATCH', '/patch', 'patch'], ['POST', '/post', 'post'], ['PUT', '/put', 'put'], ['DELETE', '/group-one/delete', 'delete'], ['GET', '/group-one/get', 'get'], ['HEAD', '/group-one/head', 'head'], ['PATCH', '/group-one/patch', 'patch'], ['POST', '/group-one/post', 'post'], ['PUT', '/group-one/put', 'put'], ['DELETE', '/group-one/group-two/delete', 'delete'], ['GET', '/group-one/group-two/get', 'get'], ['HEAD', '/group-one/group-two/head', 'head'], ['PATCH', '/group-one/group-two/patch', 'patch'], ['POST', '/group-one/group-two/post', 'post'], ['PUT', '/group-one/group-two/put', 'put'], ['GET', '/admin-some-info', 'admin-some-info'], ['GET', '/admin-more-info', 'admin-more-info'], ]; $this->assertSame($expected, $r->routes); } } class DummyRouteCollector extends RouteCollector { public $routes = []; public function __construct() { } public function addRoute($method, $route, $handler) { $route = $this->currentGroupPrefix . $route; $this->routes[] = [$method, $route, $handler]; } } FastRoute-1.3.0/test/HackTypechecker/0000755000175000017500000000000013240644777016441 5ustar jamesjamesFastRoute-1.3.0/test/HackTypechecker/fixtures/0000755000175000017500000000000013240644777020312 5ustar jamesjamesFastRoute-1.3.0/test/HackTypechecker/fixtures/no_options.php0000644000175000017500000000042013240644777023206 0ustar jamesjames {}); } function no_options_cached(): \FastRoute\Dispatcher { return \FastRoute\cachedDispatcher($collector ==> {}); } FastRoute-1.3.0/test/HackTypechecker/fixtures/empty_options.php0000644000175000017500000000045013240644777023733 0ustar jamesjames {}, shape()); } function empty_options_cached(): \FastRoute\Dispatcher { return \FastRoute\cachedDispatcher($collector ==> {}, shape()); } FastRoute-1.3.0/test/HackTypechecker/fixtures/all_options.php0000644000175000017500000000166613240644777023357 0ustar jamesjames {}, shape( 'routeParser' => \FastRoute\RouteParser\Std::class, 'dataGenerator' => \FastRoute\DataGenerator\GroupCountBased::class, 'dispatcher' => \FastRoute\Dispatcher\GroupCountBased::class, 'routeCollector' => \FastRoute\RouteCollector::class, ), ); } function all_options_cached(): \FastRoute\Dispatcher { return \FastRoute\cachedDispatcher( $collector ==> {}, shape( 'routeParser' => \FastRoute\RouteParser\Std::class, 'dataGenerator' => \FastRoute\DataGenerator\GroupCountBased::class, 'dispatcher' => \FastRoute\Dispatcher\GroupCountBased::class, 'routeCollector' => \FastRoute\RouteCollector::class, 'cacheFile' => '/dev/null', 'cacheDisabled' => false, ), ); } FastRoute-1.3.0/test/HackTypechecker/HackTypecheckerTest.php0000644000175000017500000000245613240644777023056 0ustar jamesjamesmarkTestSkipped('HHVM only'); } if (!version_compare(HHVM_VERSION, '3.9.0', '>=')) { $this->markTestSkipped('classname requires HHVM 3.9+'); } // The typechecker recurses the whole tree, so it makes sure // that everything in fixtures/ is valid when this runs. $output = []; $exit_code = null; exec( 'hh_server --check ' . escapeshellarg(__DIR__ . '/../../') . ' 2>&1', $output, $exit_code ); if ($exit_code === self::SERVER_ALREADY_RUNNING_CODE) { $this->assertTrue( $recurse, 'Typechecker still running after running hh_client stop' ); // Server already running - 3.10 => 3.11 regression: // https://github.com/facebook/hhvm/issues/6646 exec('hh_client stop 2>/dev/null'); $this->testTypechecks(/* recurse = */ false); return; } $this->assertSame(0, $exit_code, implode("\n", $output)); } } FastRoute-1.3.0/test/bootstrap.php0000644000175000017500000000056613240644777016141 0ustar jamesjamesparse($routeString); $this->assertSame($expectedRouteDatas, $routeDatas); } /** @dataProvider provideTestParseError */ public function testParseError($routeString, $expectedExceptionMessage) { $parser = new Std(); $this->setExpectedException('FastRoute\\BadRouteException', $expectedExceptionMessage); $parser->parse($routeString); } public function provideTestParse() { return [ [ '/test', [ ['/test'], ] ], [ '/test/{param}', [ ['/test/', ['param', '[^/]+']], ] ], [ '/te{ param }st', [ ['/te', ['param', '[^/]+'], 'st'] ] ], [ '/test/{param1}/test2/{param2}', [ ['/test/', ['param1', '[^/]+'], '/test2/', ['param2', '[^/]+']] ] ], [ '/test/{param:\d+}', [ ['/test/', ['param', '\d+']] ] ], [ '/test/{ param : \d{1,9} }', [ ['/test/', ['param', '\d{1,9}']] ] ], [ '/test[opt]', [ ['/test'], ['/testopt'], ] ], [ '/test[/{param}]', [ ['/test'], ['/test/', ['param', '[^/]+']], ] ], [ '/{param}[opt]', [ ['/', ['param', '[^/]+']], ['/', ['param', '[^/]+'], 'opt'] ] ], [ '/test[/{name}[/{id:[0-9]+}]]', [ ['/test'], ['/test/', ['name', '[^/]+']], ['/test/', ['name', '[^/]+'], '/', ['id', '[0-9]+']], ] ], [ '', [ [''], ] ], [ '[test]', [ [''], ['test'], ] ], [ '/{foo-bar}', [ ['/', ['foo-bar', '[^/]+']] ] ], [ '/{_foo:.*}', [ ['/', ['_foo', '.*']] ] ], ]; } public function provideTestParseError() { return [ [ '/test[opt', "Number of opening '[' and closing ']' does not match" ], [ '/test[opt[opt2]', "Number of opening '[' and closing ']' does not match" ], [ '/testopt]', "Number of opening '[' and closing ']' does not match" ], [ '/test[]', 'Empty optional part' ], [ '/test[[opt]]', 'Empty optional part' ], [ '[[test]]', 'Empty optional part' ], [ '/test[/opt]/required', 'Optional segments can only occur at the end of a route' ], ]; } } FastRoute-1.3.0/test/Dispatcher/0000755000175000017500000000000013240644777015472 5ustar jamesjamesFastRoute-1.3.0/test/Dispatcher/CharCountBasedTest.php0000644000175000017500000000050313240644777021666 0ustar jamesjames $this->getDataGeneratorClass(), 'dispatcher' => $this->getDispatcherClass() ]; } /** * @dataProvider provideFoundDispatchCases */ public function testFoundDispatches($method, $uri, $callback, $handler, $argDict) { $dispatcher = \FastRoute\simpleDispatcher($callback, $this->generateDispatcherOptions()); $info = $dispatcher->dispatch($method, $uri); $this->assertSame($dispatcher::FOUND, $info[0]); $this->assertSame($handler, $info[1]); $this->assertSame($argDict, $info[2]); } /** * @dataProvider provideNotFoundDispatchCases */ public function testNotFoundDispatches($method, $uri, $callback) { $dispatcher = \FastRoute\simpleDispatcher($callback, $this->generateDispatcherOptions()); $routeInfo = $dispatcher->dispatch($method, $uri); $this->assertArrayNotHasKey(1, $routeInfo, 'NOT_FOUND result must only contain a single element in the returned info array' ); $this->assertSame($dispatcher::NOT_FOUND, $routeInfo[0]); } /** * @dataProvider provideMethodNotAllowedDispatchCases */ public function testMethodNotAllowedDispatches($method, $uri, $callback, $availableMethods) { $dispatcher = \FastRoute\simpleDispatcher($callback, $this->generateDispatcherOptions()); $routeInfo = $dispatcher->dispatch($method, $uri); $this->assertArrayHasKey(1, $routeInfo, 'METHOD_NOT_ALLOWED result must return an array of allowed methods at index 1' ); list($routedStatus, $methodArray) = $dispatcher->dispatch($method, $uri); $this->assertSame($dispatcher::METHOD_NOT_ALLOWED, $routedStatus); $this->assertSame($availableMethods, $methodArray); } /** * @expectedException \FastRoute\BadRouteException * @expectedExceptionMessage Cannot use the same placeholder "test" twice */ public function testDuplicateVariableNameError() { \FastRoute\simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/foo/{test}/{test:\d+}', 'handler0'); }, $this->generateDispatcherOptions()); } /** * @expectedException \FastRoute\BadRouteException * @expectedExceptionMessage Cannot register two routes matching "/user/([^/]+)" for method "GET" */ public function testDuplicateVariableRoute() { \FastRoute\simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/user/{id}', 'handler0'); // oops, forgot \d+ restriction ;) $r->addRoute('GET', '/user/{name}', 'handler1'); }, $this->generateDispatcherOptions()); } /** * @expectedException \FastRoute\BadRouteException * @expectedExceptionMessage Cannot register two routes matching "/user" for method "GET" */ public function testDuplicateStaticRoute() { \FastRoute\simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/user', 'handler0'); $r->addRoute('GET', '/user', 'handler1'); }, $this->generateDispatcherOptions()); } /** * @expectedException \FastRoute\BadRouteException * @expectedExceptionMessage Static route "/user/nikic" is shadowed by previously defined variable route "/user/([^/]+)" for method "GET" */ public function testShadowedStaticRoute() { \FastRoute\simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/nikic', 'handler1'); }, $this->generateDispatcherOptions()); } /** * @expectedException \FastRoute\BadRouteException * @expectedExceptionMessage Regex "(en|de)" for parameter "lang" contains a capturing group */ public function testCapturing() { \FastRoute\simpleDispatcher(function (RouteCollector $r) { $r->addRoute('GET', '/{lang:(en|de)}', 'handler0'); }, $this->generateDispatcherOptions()); } public function provideFoundDispatchCases() { $cases = []; // 0 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; $method = 'GET'; $uri = '/resource/123/456'; $handler = 'handler0'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 1 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/handler0', 'handler0'); $r->addRoute('GET', '/handler1', 'handler1'); $r->addRoute('GET', '/handler2', 'handler2'); }; $method = 'GET'; $uri = '/handler2'; $handler = 'handler2'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 2 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }; $method = 'GET'; $uri = '/user/rdlowrey'; $handler = 'handler2'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 3 --------------------------------------------------------------------------------------> // reuse $callback from #2 $method = 'GET'; $uri = '/user/12345'; $handler = 'handler1'; $argDict = ['id' => '12345']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 4 --------------------------------------------------------------------------------------> // reuse $callback from #3 $method = 'GET'; $uri = '/user/NaN'; $handler = 'handler2'; $argDict = ['name' => 'NaN']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 5 --------------------------------------------------------------------------------------> // reuse $callback from #4 $method = 'GET'; $uri = '/user/rdlowrey/12345'; $handler = 'handler0'; $argDict = ['name' => 'rdlowrey', 'id' => '12345']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 6 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/12345/extension', 'handler1'); $r->addRoute('GET', '/user/{id:[0-9]+}.{extension}', 'handler2'); }; $method = 'GET'; $uri = '/user/12345.svg'; $handler = 'handler2'; $argDict = ['id' => '12345', 'extension' => 'svg']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 7 ----- Test GET method fallback on HEAD route miss ------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/static0', 'handler2'); $r->addRoute('GET', '/static1', 'handler3'); $r->addRoute('HEAD', '/static1', 'handler4'); }; $method = 'HEAD'; $uri = '/user/rdlowrey'; $handler = 'handler0'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 8 ----- Test GET method fallback on HEAD route miss ------------------------------------> // reuse $callback from #7 $method = 'HEAD'; $uri = '/user/rdlowrey/1234'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey', 'id' => '1234']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 9 ----- Test GET method fallback on HEAD route miss ------------------------------------> // reuse $callback from #8 $method = 'HEAD'; $uri = '/static0'; $handler = 'handler2'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 10 ---- Test existing HEAD route used if available (no fallback) -----------------------> // reuse $callback from #9 $method = 'HEAD'; $uri = '/static1'; $handler = 'handler4'; $argDict = []; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 11 ---- More specified routes are not shadowed by less specific of another method ------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('POST', '/user/{name:[a-z]+}', 'handler1'); }; $method = 'POST'; $uri = '/user/rdlowrey'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 12 ---- Handler of more specific routes is used, if it occurs first --------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('POST', '/user/{name:[a-z]+}', 'handler1'); $r->addRoute('POST', '/user/{name}', 'handler2'); }; $method = 'POST'; $uri = '/user/rdlowrey'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 13 ---- Route with constant suffix -----------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/{name}/edit', 'handler1'); }; $method = 'GET'; $uri = '/user/rdlowrey/edit'; $handler = 'handler1'; $argDict = ['name' => 'rdlowrey']; $cases[] = [$method, $uri, $callback, $handler, $argDict]; // 14 ---- Handle multiple methods with the same handler ----------------------------------> $callback = function (RouteCollector $r) { $r->addRoute(['GET', 'POST'], '/user', 'handlerGetPost'); $r->addRoute(['DELETE'], '/user', 'handlerDelete'); $r->addRoute([], '/user', 'handlerNone'); }; $argDict = []; $cases[] = ['GET', '/user', $callback, 'handlerGetPost', $argDict]; $cases[] = ['POST', '/user', $callback, 'handlerGetPost', $argDict]; $cases[] = ['DELETE', '/user', $callback, 'handlerDelete', $argDict]; // 17 ---- $callback = function (RouteCollector $r) { $r->addRoute('POST', '/user.json', 'handler0'); $r->addRoute('GET', '/{entity}.json', 'handler1'); }; $cases[] = ['GET', '/user.json', $callback, 'handler1', ['entity' => 'user']]; // 18 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '', 'handler0'); }; $cases[] = ['GET', '', $callback, 'handler0', []]; // 19 ---- $callback = function (RouteCollector $r) { $r->addRoute('HEAD', '/a/{foo}', 'handler0'); $r->addRoute('GET', '/b/{foo}', 'handler1'); }; $cases[] = ['HEAD', '/b/bar', $callback, 'handler1', ['foo' => 'bar']]; // 20 ---- $callback = function (RouteCollector $r) { $r->addRoute('HEAD', '/a', 'handler0'); $r->addRoute('GET', '/b', 'handler1'); }; $cases[] = ['HEAD', '/b', $callback, 'handler1', []]; // 21 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '/foo', 'handler0'); $r->addRoute('HEAD', '/{bar}', 'handler1'); }; $cases[] = ['HEAD', '/foo', $callback, 'handler1', ['bar' => 'foo']]; // 22 ---- $callback = function (RouteCollector $r) { $r->addRoute('*', '/user', 'handler0'); $r->addRoute('*', '/{user}', 'handler1'); $r->addRoute('GET', '/user', 'handler2'); }; $cases[] = ['GET', '/user', $callback, 'handler2', []]; // 23 ---- $callback = function (RouteCollector $r) { $r->addRoute('*', '/user', 'handler0'); $r->addRoute('GET', '/user', 'handler1'); }; $cases[] = ['POST', '/user', $callback, 'handler0', []]; // 24 ---- $cases[] = ['HEAD', '/user', $callback, 'handler1', []]; // 25 ---- $callback = function (RouteCollector $r) { $r->addRoute('GET', '/{bar}', 'handler0'); $r->addRoute('*', '/foo', 'handler1'); }; $cases[] = ['GET', '/foo', $callback, 'handler0', ['bar' => 'foo']]; // 26 ---- $callback = function(RouteCollector $r) { $r->addRoute('GET', '/user', 'handler0'); $r->addRoute('*', '/{foo:.*}', 'handler1'); }; $cases[] = ['POST', '/bar', $callback, 'handler1', ['foo' => 'bar']]; // x --------------------------------------------------------------------------------------> return $cases; } public function provideNotFoundDispatchCases() { $cases = []; // 0 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; $method = 'GET'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 1 --------------------------------------------------------------------------------------> // reuse callback from #0 $method = 'POST'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 2 --------------------------------------------------------------------------------------> // reuse callback from #1 $method = 'PUT'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 3 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/handler0', 'handler0'); $r->addRoute('GET', '/handler1', 'handler1'); $r->addRoute('GET', '/handler2', 'handler2'); }; $method = 'GET'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 4 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); }; $method = 'GET'; $uri = '/not-found'; $cases[] = [$method, $uri, $callback]; // 5 --------------------------------------------------------------------------------------> // reuse callback from #4 $method = 'GET'; $uri = '/user/rdlowrey/12345/not-found'; $cases[] = [$method, $uri, $callback]; // 6 --------------------------------------------------------------------------------------> // reuse callback from #5 $method = 'HEAD'; $cases[] = [$method, $uri, $callback]; // x --------------------------------------------------------------------------------------> return $cases; } public function provideMethodNotAllowedDispatchCases() { $cases = []; // 0 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; $method = 'POST'; $uri = '/resource/123/456'; $allowedMethods = ['GET']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 1 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/resource/123/456', 'handler0'); $r->addRoute('POST', '/resource/123/456', 'handler1'); $r->addRoute('PUT', '/resource/123/456', 'handler2'); $r->addRoute('*', '/', 'handler3'); }; $method = 'DELETE'; $uri = '/resource/123/456'; $allowedMethods = ['GET', 'POST', 'PUT']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 2 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('POST', '/user/{name}/{id:[0-9]+}', 'handler1'); $r->addRoute('PUT', '/user/{name}/{id:[0-9]+}', 'handler2'); $r->addRoute('PATCH', '/user/{name}/{id:[0-9]+}', 'handler3'); }; $method = 'DELETE'; $uri = '/user/rdlowrey/42'; $allowedMethods = ['GET', 'POST', 'PUT', 'PATCH']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 3 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute('POST', '/user/{name}', 'handler1'); $r->addRoute('PUT', '/user/{name:[a-z]+}', 'handler2'); $r->addRoute('PATCH', '/user/{name:[a-z]+}', 'handler3'); }; $method = 'GET'; $uri = '/user/rdlowrey'; $allowedMethods = ['POST', 'PUT', 'PATCH']; $cases[] = [$method, $uri, $callback, $allowedMethods]; // 4 --------------------------------------------------------------------------------------> $callback = function (RouteCollector $r) { $r->addRoute(['GET', 'POST'], '/user', 'handlerGetPost'); $r->addRoute(['DELETE'], '/user', 'handlerDelete'); $r->addRoute([], '/user', 'handlerNone'); }; $cases[] = ['PUT', '/user', $callback, ['GET', 'POST', 'DELETE']]; // 5 $callback = function (RouteCollector $r) { $r->addRoute('POST', '/user.json', 'handler0'); $r->addRoute('GET', '/{entity}.json', 'handler1'); }; $cases[] = ['PUT', '/user.json', $callback, ['POST', 'GET']]; // x --------------------------------------------------------------------------------------> return $cases; } } FastRoute-1.3.0/test/Dispatcher/MarkBasedTest.php0000644000175000017500000000101313240644777020667 0ustar jamesjamesmarkTestSkipped('PHP 5.6 required for MARK support'); } } protected function getDispatcherClass() { return 'FastRoute\\Dispatcher\\MarkBased'; } protected function getDataGeneratorClass() { return 'FastRoute\\DataGenerator\\MarkBased'; } } FastRoute-1.3.0/.travis.yml0000644000175000017500000000032113240644777014532 0ustar jamesjamessudo: false language: php php: - 5.4 - 5.5 - 5.6 - 7.0 - 7.1 - 7.2 - hhvm script: - ./vendor/bin/phpunit before_install: - travis_retry composer self-update install: - composer install FastRoute-1.3.0/phpunit.xml0000644000175000017500000000120013240644777014627 0ustar jamesjames ./test/ ./src/ FastRoute-1.3.0/.gitignore0000644000175000017500000000012613240644777014414 0ustar jamesjames/vendor/ .idea/ # ignore lock file since we have no extra dependencies composer.lock FastRoute-1.3.0/FastRoute.hhi0000644000175000017500000001072313240644777015036 0ustar jamesjames; } class RouteCollector { public function __construct(RouteParser $routeParser, DataGenerator $dataGenerator); public function addRoute(mixed $httpMethod, string $route, mixed $handler): void; public function getData(): array; } class Route { public function __construct(string $httpMethod, mixed $handler, string $regex, array $variables); public function matches(string $str): bool; } interface DataGenerator { public function addRoute(string $httpMethod, array $routeData, mixed $handler); public function getData(): array; } interface Dispatcher { const int NOT_FOUND = 0; const int FOUND = 1; const int METHOD_NOT_ALLOWED = 2; public function dispatch(string $httpMethod, string $uri): array; } function simpleDispatcher( (function(RouteCollector): void) $routeDefinitionCallback, shape( ?'routeParser' => classname, ?'dataGenerator' => classname, ?'dispatcher' => classname, ?'routeCollector' => classname, ) $options = shape()): Dispatcher; function cachedDispatcher( (function(RouteCollector): void) $routeDefinitionCallback, shape( ?'routeParser' => classname, ?'dataGenerator' => classname, ?'dispatcher' => classname, ?'routeCollector' => classname, ?'cacheDisabled' => bool, ?'cacheFile' => string, ) $options = shape()): Dispatcher; } namespace FastRoute\DataGenerator { abstract class RegexBasedAbstract implements \FastRoute\DataGenerator { protected abstract function getApproxChunkSize(); protected abstract function processChunk($regexToRoutesMap); public function addRoute(string $httpMethod, array $routeData, mixed $handler): void; public function getData(): array; } class CharCountBased extends RegexBasedAbstract { protected function getApproxChunkSize(): int; protected function processChunk(array $regexToRoutesMap): array; } class GroupCountBased extends RegexBasedAbstract { protected function getApproxChunkSize(): int; protected function processChunk(array $regexToRoutesMap): array; } class GroupPosBased extends RegexBasedAbstract { protected function getApproxChunkSize(): int; protected function processChunk(array $regexToRoutesMap): array; } class MarkBased extends RegexBasedAbstract { protected function getApproxChunkSize(): int; protected function processChunk(array $regexToRoutesMap): array; } } namespace FastRoute\Dispatcher { abstract class RegexBasedAbstract implements \FastRoute\Dispatcher { protected abstract function dispatchVariableRoute(array $routeData, string $uri): array; public function dispatch(string $httpMethod, string $uri): array; } class GroupPosBased extends RegexBasedAbstract { public function __construct(array $data); protected function dispatchVariableRoute(array $routeData, string $uri): array; } class GroupCountBased extends RegexBasedAbstract { public function __construct(array $data); protected function dispatchVariableRoute(array $routeData, string $uri): array; } class CharCountBased extends RegexBasedAbstract { public function __construct(array $data); protected function dispatchVariableRoute(array $routeData, string $uri): array; } class MarkBased extends RegexBasedAbstract { public function __construct(array $data); protected function dispatchVariableRoute(array $routeData, string $uri): array; } } namespace FastRoute\RouteParser { class Std implements \FastRoute\RouteParser { const string VARIABLE_REGEX = <<<'REGEX' \{ \s* ([a-zA-Z][a-zA-Z0-9_]*) \s* (?: : \s* ([^{}]*(?:\{(?-1)\}[^{}]*)*) )? \} REGEX; const string DEFAULT_DISPATCH_REGEX = '[^/]+'; public function parse(string $route): array; } } FastRoute-1.3.0/composer.json0000644000175000017500000000070113240644777015145 0ustar jamesjames{ "name": "nikic/fast-route", "description": "Fast request router for PHP", "keywords": ["routing", "router"], "license": "BSD-3-Clause", "authors": [ { "name": "Nikita Popov", "email": "nikic@php.net" } ], "autoload": { "psr-4": { "FastRoute\\": "src/" }, "files": ["src/functions.php"] }, "require": { "php": ">=5.4.0" }, "require-dev": { "phpunit/phpunit": "^4.8.35|~5.7" } } FastRoute-1.3.0/.hhconfig0000644000175000017500000000002113240644777014204 0ustar jamesjamesassume_php=false