Пример #1
0
 public function testGettersReturnProperConstructorParameters()
 {
     $object = new Route(Route::ROUTE_TYPE_ABSOLUTE_PROTOCOL, 'GET', '/.*/', 'Class_Name', array(2, 1, 3), '/route/string');
     $this->assertSame(Route::ROUTE_TYPE_ABSOLUTE_PROTOCOL, $object->getRouteType());
     $this->assertSame('GET', $object->getRouteHttpMethod());
     $this->assertSame('/.*/', $object->getRouteRegex());
     $this->assertSame('Class_Name', $object->getClassName());
     $this->assertSame(array(2, 1, 3), $object->getParameterOrder());
     $this->assertSame('/route/string', $object->getRawRouteString());
 }
 public function findMatchingRoute(RequestInterface $request)
 {
     // Build the SQLite database object
     $db = new SQLite3($this->routing_table_file, SQLITE3_OPEN_READONLY);
     // Create a regular expression function in SQLite that determines whether the first parameter matches the regex in the second parameter
     $db->createFunction('IS_REGEX_MATCH', function ($str, $regex) {
         return preg_match($regex, $str) ? 1 : 0;
     }, 2);
     // Map the route types to their possible regex match strings, for this request
     //  Note that relative routes will never reach this point, since all full routes are absolute in some sense.
     $arr_match_types = Route::getPathsByRouteType($request);
     // Build the where clause
     $arr_route_path_where_clause = array();
     foreach ($arr_match_types as $route_type => $route_match) {
         $arr_route_path_where_clause[] = " (route_type = {$route_type} AND IS_REGEX_MATCH('{$route_match}', route_regex) )";
     }
     $where_clause = "WHERE route_http_method='{$request->getMethod()}' " . "AND (" . implode(' OR ', $arr_route_path_where_clause) . ")";
     // Execute the query and fetch the result
     $result = $db->query("SELECT * FROM routing_table {$where_clause} ORDER BY route_type ASC LIMIT 1;");
     $arr_match = $result->fetchArray(SQLITE3_ASSOC);
     // if there was no perfect match, we need to find out if the URL itself was legit, in order to know whether to assemble a 405 response, or a 404 response
     if (empty($arr_match)) {
         // Determine if there were any matches at all for this URL, with any HTTP method
         $error_result = $db->query("SELECT route_http_method FROM routing_table WHERE (" . implode(' OR ', $arr_route_path_where_clause) . ");");
         // Fetch the set of allowed methods (if any) on this route
         $arr_allowed_methods = array();
         while ($arr_result = $error_result->fetchArray(SQLITE3_ASSOC)) {
             $arr_allowed_methods[] = $arr_result['route_http_method'];
         }
         if (!empty($arr_allowed_methods)) {
             // If there's a match on this URL, just not for the given HTTP method, return a 405
             throw new ExceptionMethodNotAllowed($arr_allowed_methods);
         } else {
             // Else this URL has no resource at all.  Return a 404
             throw new ExceptionNotFound();
         }
     }
     // Return the routing table response, suitable for calling the handler
     return new Route($arr_match['route_type'], $arr_match['route_http_method'], $arr_match['route_regex'], $arr_match['class_name'], $arr_match['parameter_order'], $arr_match['raw_route_string']);
 }