Example #1
0
 /**
  *  Dispatch a request against the routes held by the router
  *  @param string $request
  *  @param bool   $ignore_qs
  *  @return mixed|null
  */
 public function dispatch($request, $ignore_qs = false)
 {
     if (strpos($request, '#') !== false) {
         $request = substr($request, 0, strpos($request, '#'));
         //no fragments.
     }
     if ($ignore_qs && strpos($request, '?') !== false) {
         $request = substr($request, 0, strpos($request, '?'));
         //no QS
     }
     if ($this->router === null) {
         throw new BadMethodCallException("You are missing a router. Set a router");
     }
     $bindings = $this->router->toArray();
     $routes = array_keys($bindings);
     $lot_size = (int) self::$ROUTES_PER_LOT > 0 ? (int) self::$ROUTES_PER_LOT : 35;
     $chunks = array_chunk($routes, $lot_size);
     foreach ($chunks as $pack) {
         $as_one_regex = self::asJoinedRegex($pack);
         preg_match_all($as_one_regex, $request, $one_regex);
         $matched = self::clearNonMatched($one_regex);
         $compiled = self::compileFound($pack, $matched);
         $bound_key = key($compiled);
         if ($bound_key === null) {
             continue;
         }
         $bound = $bindings[$bound_key];
         $vars = array_pop($compiled);
         $request_object = (object) $vars;
         if ($bound_key !== null) {
             return call_user_func($bound, $request_object);
         }
     }
     return $this->fail($request);
 }