Пример #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;
         Logger::log('debug', 'redis disconnected', ['parameters' => $parameters]);
     }
 }
Пример #2
0
 public function execute($sql, $params = null)
 {
     $params = $params === null ? [] : is_array($params) ? $params : array_slice(func_get_args(), 1);
     Logger::log('debug', 'database execute', ['sql' => $sql instanceof \PDOStatement ? $sql->queryString : $sql, 'parameters' => $params]);
     if ($sql instanceof \PDOStatement) {
         $sth = $sql;
         $sth->execute($params);
     } elseif ($params) {
         $sth = $this->connect()->prepare($sql);
         $sth->execute($params);
     } else {
         $sth = $this->connect()->query($sql);
     }
     $sth->setFetchMode(\PDO::FETCH_ASSOC);
     return $sth;
 }
Пример #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)
 {
     Logger::log('debug', 'router respond', ['url' => (string) $request->getUri(), 'method' => $request->getMethod()]);
     $path = $this->getRequestPath($request);
     list($class, $parameters) = $this->byRewrite($path) ?: $this->byPath($path);
     Logger::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;
     $method = $request->getMethod();
     if ($method === 'HEAD') {
         $method = 'GET';
     }
     if (!method_exists($controller, $method)) {
         throw \Owl\Http\Exception::factory(405);
     }
     $this->__beforeRespond($request, $response, $controller, $parameters);
     // 如果__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;
     }
     $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);
     }
     $this->__afterRespond($request, $response, $controller, $parameters);
     return $response;
 }