コード例 #1
0
ファイル: Swoole.php プロジェクト: superwmh/swoole
 /**
  * 初始化环境
  * @return unknown_type
  */
 private function __init()
 {
     #记录运行时间和内存占用情况
     $this->env['runtime']['start'] = microtime(true);
     $this->env['runtime']['mem'] = memory_get_usage();
     #捕获错误信息
     if (DEBUG == 'on') {
         set_error_handler('swoole_error_handler');
     }
     #初始化App环境
     //为了兼容老的APPSPATH预定义常量方式
     if (defined('APPSPATH')) {
         self::$app_root = str_replace(WEBPATH, '', APPSPATH);
     } elseif (empty(self::$app_root)) {
         self::$app_root = "/apps";
     }
     self::$app_path = WEBPATH . self::$app_root;
     $this->env['app_root'] = self::$app_root;
 }
コード例 #2
0
ファイル: Swoole.php プロジェクト: jinguanio/swoole_websocket
 private function __construct()
 {
     if (!defined('DEBUG')) {
         define('DEBUG', 'off');
     }
     if (DEBUG == 'off') {
         \error_reporting(0);
     }
     #初始化App环境
     //为了兼容老的APPSPATH预定义常量方式
     if (defined('APPSPATH')) {
         self::$app_root = str_replace(WEBPATH, '', APPSPATH);
     } elseif (empty(self::$app_root)) {
         self::$app_root = "/apps";
     }
     self::$app_path = WEBPATH . self::$app_root;
     $this->env['app_root'] = self::$app_root;
     //        $this->__init();
     $this->load = new Swoole\Loader($this);
     $this->model = new Swoole\ModelLoader($this);
     $this->plugin = new Swoole\PluginLoader($this);
     //路由钩子,URLRewrite
     $this->addHook(Swoole::HOOK_ROUTE, function (&$uri) {
         $rewrite = Swoole::$php->config['rewrite'];
         if (empty($rewrite) or !is_array($rewrite)) {
             return false;
         }
         $match = array();
         foreach ($rewrite as $rule) {
             if (preg_match('#' . $rule['regx'] . '#', $uri['path'], $match)) {
                 //合并到GET中
                 if (isset($rule['get'])) {
                     $p = explode(',', $rule['get']);
                     foreach ($p as $k => $v) {
                         $_GET[$v] = $match[$k + 1];
                     }
                 }
                 return $rule['mvc'];
             }
         }
         return false;
     });
     //mvc
     $this->addHook(Swoole::HOOK_ROUTE, function (&$uri) {
         $array = array('controller' => 'page', 'view' => 'index');
         if (!empty($_GET["c"])) {
             $array['controller'] = $_GET["c"];
         }
         if (!empty($_GET["v"])) {
             $array['view'] = $_GET["v"];
         }
         if (empty($uri['path']) or $uri['path'] == '/' or $uri['path'] == '/index.php') {
             return $array;
         }
         $request = explode('/', trim($uri['path'], '/'), 3);
         if (count($request) < 2) {
             return $array;
         }
         $array['controller'] = $request[0];
         $array['view'] = $request[1];
         if (isset($request[2])) {
             $request[2] = trim($request[2], '/');
             if (is_numeric($request[2])) {
                 $_GET['id'] = $request[2];
             } else {
                 Swoole\Tool::$url_key_join = '-';
                 Swoole\Tool::$url_param_join = '-';
                 Swoole\Tool::$url_add_end = '.html';
                 Swoole\Tool::$url_prefix = WEBROOT . "/{$request[0]}/{$request['1']}/";
                 Swoole\Tool::url_parse_into($request[2], $_GET);
             }
         }
         return $array;
     });
 }