示例#1
0
 /**
  * @param int $code
  * @param string|null $message
  * @param string $httpVersion
  * @return void
  * @throws \Hahns\Exception\StatusMessageCannotFindException
  * @throws \InvalidArgumentException
  */
 public function status($code = Response::CODE_OK, $message = null, $httpVersion = '1.1')
 {
     IntegerValidator::hasTo($code, 'code');
     StringValidator::hasToBeStringOrNull($message, 'message');
     StringValidator::hasTo($httpVersion, 'httpVersion');
     // get message
     if (is_null($message)) {
         $constantName = sprintf('\\Hahns\\Response::MSG_%d', $code);
         if (!defined($constantName)) {
             $message = sprintf('Status message for code `%d` cannot find', $code);
             throw new StatusMessageCannotFindException($message);
         }
         $message = constant($constantName);
     }
     header(sprintf('HTTP/%s %d %s', $httpVersion, $code, $message));
 }
示例#2
0
文件: Router.php 项目: pklink/hahns
 /**
  * @param string $route
  * @param \Closure|string $callbackOrNamedRoute
  * @param string|null $name
  * @throws \InvalidArgumentException
  * @throws Exception\RouteDoesNotExistException
  */
 public function add($route, $callbackOrNamedRoute, $name = null)
 {
     StringValidator::hasTo($route, 'route');
     StringValidator::hasToBeStringOrNull($name, 'name');
     // get callback
     if ($callbackOrNamedRoute instanceof \Closure) {
         $callback = $callbackOrNamedRoute;
     } elseif (is_string($callbackOrNamedRoute)) {
         $callback = $this->getRoute($callbackOrNamedRoute)[1];
     } else {
         $message = 'Argumet for `callbackOrNamedRoute` must be a \\Closure or a string';
         throw new \InvalidArgumentException($message);
     }
     if (!is_null($name)) {
         $index = sprintf('named-%s', $name);
         $this->routes[$index] = [$route, $callback];
     } else {
         $this->routes[] = [$route, $callback];
     }
 }
示例#3
0
文件: Hahns.php 项目: pklink/hahns
 /**
  * @throws \InvalidArgumentException
  */
 public function run($route = null, $requestMethod = null)
 {
     $this->trigger(Hahns::EVENT_BEFORE_RUNNING, [$route, $this]);
     StringValidator::hasToBeStringOrNull($route, 'route');
     StringValidator::hasToBeStringOrNull($requestMethod, 'requestMethod');
     // get route
     if ($route !== null) {
         $route = $this->removeLastSlash($route);
     } elseif (isset($_SERVER['PATH_INFO'])) {
         $route = $this->removeLastSlash($_SERVER['PATH_INFO']);
     } else {
         $route = '';
     }
     // save used route
     $usedRoute = $route;
     // get request method
     if (is_null($requestMethod)) {
         $requestMethod = $_SERVER['REQUEST_METHOD'];
     }
     // get method and concat with $route
     $route = strtolower($requestMethod) . '-' . $route;
     try {
         $this->trigger(Hahns::EVENT_BEFORE_ROUTING, [$usedRoute, $this]);
         $this->router()->dispatch($route);
         $this->trigger(Hahns::EVENT_AFTER_ROUTING, [$usedRoute, $this]);
         // get callback
         $callback = $this->router()->getCallback();
         // get attributes for callback
         $attributes = [];
         $callbackReflection = new \ReflectionFunction($callback);
         foreach ($callbackReflection->getParameters() as $parameter) {
             $type = $parameter->getClass()->getName();
             $attributes[] = $this->parameterHolder->get($type);
         }
         // call callback
         $this->trigger(Hahns::EVENT_BEFORE_EXECUTING_ROUTE, [$usedRoute, $callback, $attributes, $this]);
         echo call_user_func_array($callback, $attributes);
         $this->trigger(Hahns::EVENT_AFTER_EXECUTING_ROUTE, [$usedRoute, $callback, $attributes, $this]);
     } catch (NotFoundException $e) {
         $this->trigger(Hahns::EVENT_NOT_FOUND, [$usedRoute, $this, $e]);
     } catch (\Exception $e) {
         $this->trigger(Hahns::EVENT_ERROR, [$e, $this]);
     }
     $this->trigger(Hahns::EVENT_AFTER_RUNNING, [$usedRoute, $this]);
 }