Ejemplo n.º 1
0
 public static function route($server)
 {
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . $server->getCtrl();
     $class = Factory::getInstance($action);
     if (!$class instanceof IController) {
         throw new \Exception("ctrl error");
     }
     $class->setServer($server);
     $before = $class->_before();
     $view = $exception = null;
     if ($before) {
         try {
             $method = $server->getMethod();
             if (\method_exists($class, $method)) {
                 $view = $class->{$method}();
             } else {
                 throw new \Exception("no method {$method}");
             }
         } catch (\Exception $e) {
             $exception = $e;
         }
     }
     $class->_after();
     if ($exception !== null) {
         throw $exception;
     }
     if (null === $view) {
         return;
     }
     return $server->display($view);
 }
Ejemplo n.º 2
0
 public static function route()
 {
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . Request::getCtrl();
     $view = null;
     try {
         $class = Factory::getInstance($action);
         if (!$class instanceof IController) {
             throw new \Exception("ctrl error");
         } else {
             $class->_before();
             $method = Request::getMethod();
             if (!method_exists($class, $method)) {
                 throw new \Exception("method error");
             }
             $view = $class->{$method}();
             $class->_after();
             if (null === $view) {
                 return null;
             }
             return Response::display($view);
         }
     } catch (\Exception $e) {
         if (Request::isLongServer()) {
             return \call_user_func(Config::getField('project', 'exception_handler', 'ZPHP\\ZPHP::exceptionHandler'), $e);
         }
         throw $e;
     }
 }
Ejemplo n.º 3
0
 public static function route($server)
 {
     $ctrl = $server->getCtrl();
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . $ctrl;
     $class = Factory::getInstance($action);
     if (!$class instanceof IController) {
         throw new \Exception("ctrl error");
     }
     $class->setServer($server);
     $before = $class->_before();
     $exception = null;
     if ($before) {
         try {
             $method = $server->getMethod();
             if (\method_exists($class, $method)) {
                 $class->{$method}();
             } else {
                 throw new \Exception("no method {$method}");
             }
         } catch (\Exception $e) {
             $exception = $e;
         }
     }
     $class->_after();
     if ($exception !== null) {
         $exception->e_no = $exception->getCode() < 0 ? 0 : $exception->getCode();
         $exception->e_msg = $exception->getMessage();
         $exception->e_data = $class->data;
         throw $exception;
     }
     //正确返回
     $result = array('e_no' => $class->e_no, 'e_msg' => '', 'api' => $ctrl, 'data' => $class->data);
     return $server->display($result);
 }
Ejemplo n.º 4
0
 public static function getInstance($adapter = 'Http')
 {
     if (is_file(__DIR__ . DS . 'Adapter' . DS . $adapter . '.php')) {
         $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     } else {
         $className = $adapter;
     }
     return CFactory::getInstance($className);
 }
Ejemplo n.º 5
0
 public static function getInstance($adapter = 'Redis', $config = null)
 {
     if (empty($config)) {
         $config = ZConfig::get('rank');
         if (!empty($config['adapter'])) {
             $adapter = $config['adapter'];
         }
     }
     $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     return CFactory::getInstance($className, $config);
 }
Ejemplo n.º 6
0
 public function run()
 {
     $config = Config::get('socket');
     if (empty($config)) {
         throw new \Exception("socket config empty");
     }
     $socket = SFactory::getInstance($config['adapter'], $config);
     $client = CFactory::getInstance($config['client_class']);
     $socket->setClient($client);
     $socket->run();
 }
Ejemplo n.º 7
0
 public static function getInstance($adapter = 'Swoole', $config = null)
 {
     if (empty($config)) {
         $config = ZConfig::get('socket');
         if (!empty($config['adapter'])) {
             $adapter = $config['adapter'];
         }
     }
     if (is_file(__DIR__ . DS . 'Adapter' . DS . $adapter . '.php')) {
         $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     } else {
         $className = $adapter;
     }
     return CFactory::getInstance($className, $config);
 }
Ejemplo n.º 8
0
 public function run()
 {
     $config = Config::get('socket');
     if (empty($config)) {
         throw new \Exception("socket config empty");
     }
     $socket = SFactory::getInstance($config['adapter'], $config);
     if (method_exists($socket, 'setClient')) {
         $client = CFactory::getInstance($config['client_class']);
         $socket->setClient($client);
     }
     Request::setServer(ZProtocol::getInstance(Config::getField('socket', 'protocol')));
     Request::setLongServer();
     Request::setHttpServer(0);
     $socket->run();
 }
Ejemplo n.º 9
0
Archivo: Route.php Proyecto: xifat/zphp
 public static function route($server)
 {
     $action = Config::get('ctrl_path', 'ctrl') . '\\' . $server->getCtrl();
     $class = Factory::getInstance($action);
     if (!$class instanceof IController) {
         throw new \Exception("ctrl error");
     }
     $class->setServer($server);
     $view = $exception = null;
     try {
         $before = $class->_before();
     } catch (\Exception $e) {
         $exception = $e;
         $before = false;
     }
     if ($before) {
         try {
             $method = $server->getMethod();
             if (\method_exists($class, $method)) {
                 $view = $class->{$method}();
             } else {
                 throw new \Exception("no method {$method}");
             }
         } catch (\Exception $e) {
             $exception = $e;
         }
     }
     $class->_after();
     if ($exception !== null) {
         if ('Socket' == Config::get('server_mode', 'Http')) {
             call_user_func(Config::getField('project', 'exception_handler', 'ZPHP\\ZPHP::exceptionHandler'), $exception);
             return;
         }
         throw $exception;
     }
     if (null === $view) {
         return;
     }
     return $server->display($view);
 }
Ejemplo n.º 10
0
 public static function getInstance($adapter = 'Redis', $config = null)
 {
     $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     return CFactory::getInstance($className, $config);
 }
Ejemplo n.º 11
0
 public static function getInstance($adapter = 'Php')
 {
     $className = __NAMESPACE__ . "\\Adapter\\{$adapter}";
     return CFactory::getInstance($className);
 }
Ejemplo n.º 12
0
 /**
  * @param $dao
  * @return \dao\Base
  */
 public static function getDao($dao)
 {
     return Factory::getInstance("dao\\{$dao}");
 }
Ejemplo n.º 13
0
 public function __construct($opt, $config = 'default')
 {
     $this->webPath = $opt['path'];
     if (!empty($opt['config'])) {
         $this->configPath = $opt['config'];
     }
     $ip = empty($opt['ip']) ? '0.0.0.0' : $opt['ip'];
     $port = empty($opt['port']) ? '9501' : $opt['port'];
     $http = new swoole_websocket_server($ip, $port);
     if (isset($opt['d'])) {
         $daemonize = 1;
     } else {
         $daemonize = 0;
     }
     $worker_num = empty($opt['worker']) ? 4 : $opt['worker'];
     $http->set(array('worker_num' => $worker_num, 'daemonize' => $daemonize, 'max_request' => 0));
     $http->setGlobal(HTTP_GLOBAL_ALL, HTTP_GLOBAL_GET | HTTP_GLOBAL_POST);
     $http->on('WorkerStart', array($this, 'onWorkerStart'));
     $http->on('WorkerError', array($this, 'onWorkerError'));
     $http->on('WorkerStop', array($this, 'onWorkerStop'));
     $http->on('close', function () {
         $params = func_get_args();
         $conn = $params[0]->connection_info($params[1]);
         if ($conn['websocket_status'] > 1) {
             $parse = ZFactory::getInstance(ZConfig::getField('socket', 'parse_class', 'WebSocketChatParse'));
             $parse->close($this->zphp, $params[1]);
         }
     });
     $http->on('open', function ($response) {
         $parse = ZFactory::getInstance(ZConfig::getField('socket', 'parse_class', 'WebSocketChatParse'));
         $parse->open($this->zphp, $response->fd);
     });
     $http->on('message', function ($server, $frame) {
         HttpServer::$wsfarme = $frame;
         $parse = ZFactory::getInstance(ZConfig::getField('websocket', 'parse_class', 'WebSocketChatParse'));
         $parse->message($this->zphp, $frame);
     });
     $http->on('request', function ($request, $response) {
         HttpServer::$request = $request;
         HttpServer::$response = $response;
         $_SERVER['PATH_INFO'] = $request->server['path_info'];
         if ($_SERVER['PATH_INFO'] == '/') {
             if (!empty($this->defaultFiles)) {
                 foreach ($this->defaultFiles as $file) {
                     $staticFile = $this->getStaticFile(DIRECTORY_SEPARATOR . $file);
                     if (is_file($staticFile)) {
                         $response->end(file_get_contents($staticFile));
                         return;
                     }
                 }
             }
         }
         if ($_SERVER['PATH_INFO'] == '/favicon.ico') {
             $response->header('Content-Type', $this->mimes['ico']);
             $response->end('');
             return;
         }
         $staticFile = $this->getStaticFile($_SERVER['PATH_INFO']);
         if (\is_dir($staticFile)) {
             //是目录
             foreach ($this->defaultFiles as $file) {
                 if (is_file($staticFile . $file)) {
                     $response->header('Content-Type', 'text/html');
                     $response->end(file_get_contents($staticFile . $file));
                     return;
                 }
             }
         }
         $ext = \pathinfo($_SERVER['PATH_INFO'], PATHINFO_EXTENSION);
         if (isset($this->mimes[$ext])) {
             //非法的扩展名
             if (\is_file($staticFile)) {
                 //读取静态文件
                 $response->header('Content-Type', $this->mimes[$ext]);
                 $response->end(file_get_contents($staticFile));
                 return;
             } else {
                 $response->status(404);
                 $response->end('');
                 return;
             }
         }
         try {
             ob_start();
             $result = $this->zphp->run();
             if (null == $result) {
                 $result = ob_get_contents();
             }
             ob_end_clean();
         } catch (Exception $e) {
             $result = json_encode($e->getTrace());
         }
         $response->status(200);
         $response->end($result);
     });
     self::$http = $http;
     self::$http->start();
 }
Ejemplo n.º 14
0
 public static function getModel($model)
 {
     return Factory::getInstance("model\\{$model}");
 }
Ejemplo n.º 15
0
 /**
  * @return \PHPMailer\PHPMailer
  */
 public static function getPhpMail()
 {
     return Factory::getInstance("PHPMailer\\PHPMailer");
 }