コード例 #1
0
ファイル: Response.php プロジェクト: yeaha/owl-mvc
 public function getReasonPhrase()
 {
     if ($this->reason_phrase) {
         return $this->reason_phrase;
     }
     return \Owl\Http::getStatusPhrase($this->code);
 }
コード例 #2
0
ファイル: Response.php プロジェクト: niceDreamer/owl
 protected function send()
 {
     if (!headers_sent()) {
         $status = $this->getStatus();
         if ($status !== 200) {
             header(sprintf('HTTP/1.1 %d %s', $status, \Owl\Http::getStatusMessage($status)));
         }
         foreach ($this->headers as $key => $value) {
             header(sprintf('%s: %s', $key, $value));
         }
         foreach ($this->cookies as $config) {
             list($name, $value, $expire, $path, $domain, $secure, $httponly) = $config;
             setCookie($name, $value, $expire, $path, $domain, $secure, $httponly);
         }
     }
     if (isset($_SESSION) && $_SESSION instanceof \Owl\Session) {
         $_SESSION->commit();
     }
     $body = $this->body;
     if (in_array($this->getStatus(), [204, 304])) {
         $body = '';
     }
     if ($body instanceof \Closure) {
         echo call_user_func($body);
     } else {
         echo (string) $body;
     }
 }
コード例 #3
0
ファイル: hello_world.php プロジェクト: niceDreamer/owl
require __DIR__ . '/../src/autoload.php';
$ip = '127.0.0.1';
$port = 12345;
$app = new \Owl\Swoole\Application($ip, $port);
$app->middleware(function ($request, $response) {
    $start = microtime(true);
    (yield true);
    $use_time = (microtime(true) - $start) * 1000;
    $response->setHeader('use-time', (int) $use_time . 'ms');
});
$app->middleware(function ($request, $response) {
    if ($request->getRequestPath() === '/') {
        $response->setBody('hello world!');
    } else {
        throw \Owl\Http\Exception::factory(404);
    }
    (yield true);
});
$app->setExceptionHandler(function ($exception, $request, $response) {
    if ($exception instanceof \Owl\Http\Exception) {
        $status = $exception->getCode();
        $message = $exception->getMessage();
    } else {
        $status = 500;
        $message = \Owl\Http::getStatusMessage(500);
    }
    $response->setStatus($status);
    $response->setBody($message);
});
echo sprintf("Listening http://%s:%d ...\n", $ip, $port);
$app->start();
コード例 #4
0
ファイル: Exception.php プロジェクト: niceDreamer/owl
 public static function factory($status, \Exception $previous = null)
 {
     return new self(\Owl\Http::getStatusMessage($status), $status, $previous);
 }
コード例 #5
0
ファイル: Exception.php プロジェクト: yeaha/owl-mvc
 public static function factory($status, \Throwable $previous = null)
 {
     return new self(\Owl\Http::getStatusPhrase($status), $status, $previous);
 }