private function __construct() { if (!defined('DEBUG')) { define('DEBUG', 'on'); } if (DEBUG == 'off') { \error_reporting(0); } $this->env['sapi_name'] = php_sapi_name(); if ($this->env['sapi_name'] != 'cli') { Swoole\Error::$echo_html = true; } if (empty(self::$app_path)) { if (defined('WEBPATH')) { self::$app_path = WEBPATH . '/apps'; } else { Swoole\Error::info("core error", __CLASS__ . ": Swoole::\$app_path and WEBPATH empty."); } } define('APPSPATH', self::$app_path); //将此目录作为App命名空间的根目录 Swoole\Loader::setRootNS('App', self::$app_path . '/classes'); $this->load = new Swoole\Loader($this); $this->model = new Swoole\ModelLoader($this); //路由钩子,URLRewrite $this->addHook(Swoole::HOOK_ROUTE, 'swoole_urlrouter_rewrite'); //mvc $this->addHook(Swoole::HOOK_ROUTE, 'swoole_urlrouter_mvc'); //设置路由函数 $this->router(array($this, 'urlRoute')); }
/** * 设置应用程序路径 * @param $dir */ static function setAppPath($dir) { if (is_dir($dir)) { self::$app_path = $dir; } else { \Swoole\Error::info("fatal error", "app_path[{$dir}] is not exists."); } }
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; }); }
/** * 初始化环境 * @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; }
<?php define('DEBUG', 'on'); require __DIR__ . '/../vendor/autoload.php'; Swoole\Loader::vendor_init(); $pkg = array("client_ip" => "127.0.0.1", "user_id" => 350749960, "level" => 1, "time" => 1398345164, "txt" => "ErrorLog Test. File=/home/htf/workspace/duowan/mostats/tests/send_log.php|num=0"); Swoole::$app_path = __DIR__ . '/../server/'; table('module_log')->put($pkg);