Example #1
0
 public function fire()
 {
     $safer = str_replace("\n", ' - ', $this->getMessage());
     if (!headers_sent()) {
         //header($_SERVER['SERVER_PROTOCOL'] . " $this->HttpCode " . $safer);
         Response::errorResponse($this->HttpCode, $this->getMessage(), $this->Error);
         exit;
     } else {
         print "\n------\nFatal error after initial output: ";
         print $safer;
         exit;
     }
 }
Example #2
0
 public function run()
 {
     $method = $_SERVER['REQUEST_METHOD'];
     if (!isset(self::$allowedMethods[$method])) {
         throw new Exception\InvalidRequest("{$method} request method not allowed");
     }
     $path = isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : '';
     $parts = explode(self::DELIM, trim($path, self::DELIM));
     foreach ($this->routes as $route) {
         if (false === strpos($route[self::METHODS], $method)) {
             continue;
         }
         $spec = $route[self::PATHSPEC];
         $specParts = explode(self::DELIM, $spec);
         if (count($specParts) !== count($parts)) {
             continue;
         }
         $vars = array();
         foreach ($specParts as $i => $part) {
             if ($part[0] === self::MARK) {
                 $vars[substr($part, 1)] = $parts[$i];
             } else {
                 if ($part[0] === self::MARKNUMERIC) {
                     $partName = substr($part, 1);
                     if (is_numeric($parts[$i])) {
                         $vars[$partName] = intval($parts[$i]);
                     } else {
                         throw new Exception\InvalidParameter("{$part} must be an integer in {$spec}");
                     }
                 } else {
                     if ($part[0] === self::MARKECHO) {
                         $partName = substr($part, 1);
                         $vars[$partName] = $partName;
                     } else {
                         if ($part === $parts[$i]) {
                             // matches
                         } else {
                             continue 2;
                         }
                     }
                 }
             }
         }
         $handler = $route[self::CONTROLLER];
         $class = $this->namespace . '\\' . $handler;
         $object = new $class($vars);
         foreach ($vars as $key => $value) {
             $object->{$key} = $value;
         }
         if (isset($route[self::OVERRIDE_METHOD])) {
             $method = $route[self::OVERRIDE_METHOD];
         }
         $call = array($object, $method);
         if (!is_callable($call)) {
             throw new Exception\InvalidRequest("Bad Request - route error '{$method}'");
         }
         call_user_func($call);
         return;
     }
     Response::errorResponse(400, "Bad Request");
 }