Esempio n. 1
0
 public function disconnect()
 {
     if ($this->client) {
         $parameters = $this->getConfig('parameters');
         $is_persistent = isset($parameters['persistent']) && $parameters['persistent'];
         if (!$is_persistent) {
             $this->client->disconnect();
         }
         $this->client = null;
         App::log('debug', 'redis disconnected', ['parameters' => $parameters]);
     }
 }
Esempio n. 2
0
 public function execute($sql, $params = null)
 {
     $params = $params === null ? [] : is_array($params) ? $params : array_slice(func_get_args(), 1);
     App::log('debug', 'database execute', ['sql' => $sql instanceof \PDOStatement ? $sql->queryString : $sql, 'parameters' => $params]);
     $sth = $sql instanceof \PDOStatement ? $sql : $this->connect()->prepare($sql);
     $sth->execute($params);
     $sth->setFetchMode(\PDO::FETCH_ASSOC);
     return $sth;
 }
Esempio n. 3
0
 /**
  * @param \Owl\Http\Request $request
  * @param \Owl\Http\Response $response
  * @return \Owl\Http\Response $response
  *
  * @throws \Owl\Http\Exception 404
  * @throws \Owl\Http\Exception 501
  */
 protected function respond(\Owl\Http\Request $request, \Owl\Http\Response $response)
 {
     App::log('debug', 'router respond', ['url' => (string) $request->getUri(), 'method' => $request->getMethod()]);
     $path = $this->getRequestPath($request);
     list($class, $parameters) = $this->byRewrite($path) ?: $this->byPath($path);
     App::log('debug', 'router dispatch', ['class' => $class, 'parameters' => $parameters]);
     if (!class_exists($class)) {
         throw \Owl\Http\Exception::factory(404);
     }
     $controller = new $class($request, $response);
     $controller->request = $request;
     $controller->response = $response;
     // 如果__beforeExecute()返回了内容就直接返回内容
     if (method_exists($controller, '__beforeExecute') && ($data = call_user_func_array([$controller, '__beforeExecute'], $parameters))) {
         if ($data instanceof \Psr\Http\Message\StreamInterface) {
             $response->withBody($data);
         } elseif (!$data instanceof \Owl\Http\Response) {
             $response->write($data);
         }
         return $response;
     }
     $method = $request->getMethod();
     if ($method === 'HEAD') {
         $method = 'GET';
     }
     if (!in_array($method, ['HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'])) {
         throw \Owl\Http\Exception::factory(501);
     }
     if (!is_callable([$controller, $method])) {
         throw \Owl\Http\Exception::factory(405);
     }
     $data = call_user_func_array([$controller, $method], $parameters);
     if ($data instanceof \Psr\Http\Message\StreamInterface) {
         $response->withBody($data);
     } elseif ($data !== null && !$data instanceof \Owl\Http\Response) {
         $response->write($data);
     }
     if (method_exists($controller, '__afterExecute')) {
         $controller->__afterExecute($request, $response);
     }
     return $response;
 }