forked from toxmc/swoole-linux-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
84 changed files
with
4,317 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/** | ||
* 自动加载类 | ||
* @param unknown $class | ||
* @author xmc | ||
*/ | ||
function autoload($class) | ||
{ | ||
$filename = BASEDIR.'/'.str_replace('\\','/',$class).'.php'; | ||
if (file_exists($filename)) { | ||
include $filename; | ||
} else { | ||
echo '文件'.$filename.'不存在'.PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
/** | ||
* web server 不依赖php-fpm | ||
* 处理网页显示 | ||
* @author xmc | ||
*/ | ||
|
||
namespace Bootstrap; | ||
use \Config\Config; | ||
use Core; | ||
|
||
class WebServer | ||
{ | ||
public static $instance; | ||
|
||
/** | ||
* MasterPid命令时格式化输出 | ||
* ManagerPid命令时格式化输出 | ||
* WorkerId命令时格式化输出 | ||
* WorkerPid命令时格式化输出 | ||
* @var int | ||
*/ | ||
protected static $_maxMasterPidLength = 12; | ||
protected static $_maxManagerPidLength = 12; | ||
protected static $_maxWorkerIdLength = 12; | ||
protected static $_maxWorkerPidLength = 12; | ||
|
||
/** | ||
* 初始化 | ||
*/ | ||
private function __construct($ip, $port) | ||
{ | ||
register_shutdown_function(array($this, 'handleFatal')); | ||
$http = new \swoole_http_server($ip, $port); | ||
$http->set (\Config\Server::getWebServerConfig()); | ||
$http->on('WorkerStart', array($this, 'onWorkerStart')); | ||
$http->on('request', array($this, 'onRequest')); | ||
$http->on('start', array($this, 'onStart')); | ||
$http->start(); | ||
} | ||
|
||
/** | ||
* server start的时候调用 | ||
* @param unknown $serv | ||
*/ | ||
public function onStart($serv) | ||
{ | ||
echo "\033[1A\n\033[K-----------------------\033[47;30m SWOOLE \033[0m-----------------------------\n\033[0m"; | ||
echo 'swoole version:' . swoole_version() . " PHP version:".PHP_VERSION."\n"; | ||
echo "------------------------\033[47;30m WORKERS \033[0m---------------------------\n"; | ||
echo "\033[47;30mMasterPid\033[0m", str_pad('', self::$_maxMasterPidLength + 2 - strlen('MasterPid')), "\033[47;30mManagerPid\033[0m", str_pad('', self::$_maxManagerPidLength + 2 - strlen('ManagerPid')), "\033[47;30mWorkerId\033[0m", str_pad('', self::$_maxWorkerIdLength + 2 - strlen('WorkerId')), "\033[47;30mWorkerPid\033[0m\n"; | ||
} | ||
/** | ||
* worker start时调用 | ||
* @param unknown $serv | ||
* @param int $worker_id | ||
*/ | ||
public function onWorkerStart($serv, $worker_id) | ||
{ | ||
global $argv; | ||
$worker_num = isset($serv->setting['worker_num']) ? $serv->setting['worker_num'] : 1; | ||
$task_worker_num = isset($serv->setting['task_worker_num']) ? $serv->setting['task_worker_num'] : 0; | ||
|
||
if($worker_id >= $worker_num) { | ||
swoole_set_process_name("php {$argv[0]}: task"); | ||
} else { | ||
swoole_set_process_name("php {$argv[0]}: worker"); | ||
} | ||
echo str_pad($serv->master_pid, self::$_maxMasterPidLength+2),str_pad($serv->manager_pid, self::$_maxManagerPidLength+2),str_pad($serv->worker_id, self::$_maxWorkerIdLength+2), str_pad($serv->worker_pid, self::$_maxWorkerIdLength), "\n";; | ||
define('APPLICATION_PATH', dirname(__DIR__)); | ||
} | ||
|
||
/** | ||
* 当request时调用 | ||
* @param unknown $request | ||
* @param unknown $response | ||
*/ | ||
public function onRequest($request, $response) | ||
{ | ||
$_GET = $_POST = $_COOKIE = array(); | ||
$resp = \Core\Response::getInstance($response); | ||
$resp->setResponse($response); | ||
if (isset($request->get)) { | ||
$_GET = $request->get; | ||
} | ||
if (isset($request->post)) { | ||
$_POST = $request->post; | ||
} | ||
if (isset($request->cookie)) { | ||
$_COOKIE = $request->cookie; | ||
} | ||
try { | ||
ob_start(); | ||
include APPLICATION_PATH.'/server/swooleindex.php'; | ||
$result = ob_get_contents(); | ||
ob_end_clean(); | ||
$response->header("Content-Type", "text/html;charset=utf-8"); | ||
$result = empty($result) ? 'No message' : $result; | ||
$response->end($result); | ||
unset($result); | ||
} catch (Exception $e) { | ||
var_dump($e); | ||
} | ||
} | ||
|
||
/** | ||
* 致命错误处理 | ||
*/ | ||
public function handleFatal() | ||
{ | ||
$error = error_get_last(); | ||
if (isset($error['type'])) { | ||
switch ($error['type']) { | ||
case E_ERROR : | ||
$severity = 'ERROR:Fatal run-time errors. Errors that can not be recovered from. Execution of the script is halted'; | ||
break; | ||
case E_PARSE : | ||
$severity = 'PARSE:Compile-time parse errors. Parse errors should only be generated by the parser'; | ||
break; | ||
case E_DEPRECATED: | ||
$severity = 'DEPRECATED:Run-time notices. Enable this to receive warnings about code that will not work in future versions'; | ||
break; | ||
case E_CORE_ERROR : | ||
$severity = 'CORE_ERROR :Fatal errors at PHP startup. This is like an E_ERROR in the PHP core'; | ||
break; | ||
case E_COMPILE_ERROR : | ||
$severity = 'COMPILE ERROR:Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine'; | ||
break; | ||
default: | ||
$severity = 'OTHER ERROR'; | ||
break; | ||
} | ||
$message = $error['message']; | ||
$file = $error['file']; | ||
$line = $error['line']; | ||
$log = "$message ($file:$line)\nStack trace:\n"; | ||
$trace = debug_backtrace(); | ||
foreach ($trace as $i => $t) { | ||
if (!isset($t['file'])) { | ||
$t['file'] = 'unknown'; | ||
} | ||
if (!isset($t['line'])) { | ||
$t['line'] = 0; | ||
} | ||
if (!isset($t['function'])) { | ||
$t['function'] = 'unknown'; | ||
} | ||
$log .= "#$i {$t['file']}({$t['line']}): "; | ||
if (isset($t['object']) && is_object($t['object'])) { | ||
$log .= get_class($t['object']) . '->'; | ||
} | ||
$log .= "{$t['function']}()\n"; | ||
} | ||
if (isset($_SERVER['REQUEST_URI'])) { | ||
$log .= '[QUERY] ' . $_SERVER['REQUEST_URI']; | ||
} | ||
file_put_contents('data/web_error.log', $log); | ||
} | ||
} | ||
|
||
public static function getInstance($ip="0.0.0.0", $port=6666) | ||
{ | ||
if (!self::$instance) { | ||
self::$instance = new self($ip, $port); | ||
} | ||
return self::$instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* redis 配置文件 | ||
* @author xmc | ||
*/ | ||
|
||
namespace Config; | ||
class Redis | ||
{ | ||
public static function getConfig() { | ||
$config = array( | ||
'host' => '127.0.0.1', | ||
'port' => '6379', | ||
); | ||
return $config; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* 统计server与web server配置 | ||
* @author xmc | ||
*/ | ||
namespace Config; | ||
|
||
class Server | ||
{ | ||
/** | ||
* 获取web server配置 | ||
* @return multitype:number string boolean | ||
*/ | ||
public static function getWebServerConfig() | ||
{ | ||
$config = array( | ||
'worker_num' => 4, // worker进程数量 | ||
'max_request' => 1000, // 最大请求次数,当请求大于它时,将会自动重启该worker | ||
'dispatch_mode' => 1, | ||
'log_file' => 'data/web.log', | ||
'daemonize' => false, // 守护进程设置成true | ||
); | ||
return $config; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** | ||
* 文件缓存类,提供类似memcache的接口 | ||
* 警告:此类仅用于测试,不作为生产环境的代码,请使用Key-Value缓存系列! | ||
* @author xmc | ||
* @subpackage cache | ||
*/ | ||
|
||
namespace Core\Cache; | ||
|
||
class FileCache | ||
{ | ||
|
||
protected $config; | ||
|
||
/** | ||
* 初始化 | ||
* @param unknown $config | ||
*/ | ||
function __construct($config = array()) | ||
{ | ||
if (! isset($config['cache_dir'])) { | ||
$config['cache_dir'] = BASEDIR.'/data/cache/filecache'; | ||
} | ||
if (! is_dir($config['cache_dir'])) { | ||
mkdir($config['cache_dir'], 0777, true); | ||
} | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* 获取文件名 | ||
* @param unknown $key | ||
* @return string | ||
*/ | ||
protected function getFileName($key) | ||
{ | ||
$file = $this->config['cache_dir'] . '/' . trim(str_replace('_', '/', $key), '/'); | ||
$dir = dirname($file); | ||
if (! is_dir($dir)) { | ||
mkdir($dir, 0755, true); | ||
} | ||
return $file; | ||
} | ||
|
||
/** | ||
* 设置缓存 | ||
* @param unknown $key | ||
* @param unknown $value | ||
* @param number $timeout | ||
* @return number | ||
*/ | ||
function set($key, $value, $timeout = 0) | ||
{ | ||
$file = $this->getFileName($key); | ||
$data["value"] = $value; | ||
$data["timeout"] = $timeout; | ||
$data["mktime"] = time(); | ||
$res = file_put_contents($file, serialize($data)); | ||
return $res; | ||
} | ||
|
||
/** | ||
* 获取缓存数据 | ||
* @param unknown $key | ||
* @return boolean|mixed | ||
*/ | ||
function get($key) | ||
{ | ||
$file = $this->getFileName($key); | ||
if (! is_file($file)) | ||
return false; | ||
$data = unserialize(file_get_contents($file)); | ||
if (empty($data) or ! isset($data['timeout']) or ! isset($data["value"])) { | ||
return false; | ||
} | ||
// 已过期 | ||
if ($data["timeout"] != 0 and ($data["mktime"] + $data["timeout"]) < time()) { | ||
$this->delete($key); | ||
return false; | ||
} | ||
return $data['value']; | ||
} | ||
|
||
/** | ||
* 删除 | ||
* @param unknown $key | ||
* @return boolean | ||
*/ | ||
function delete($key) | ||
{ | ||
$file = $this->getFileName($key); | ||
return unlink($file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Cookie | ||
* @author xmc | ||
*/ | ||
|
||
namespace Core; | ||
|
||
class Cookie | ||
{ | ||
public static $path = '/'; | ||
public static $domain = null; | ||
public static $secure = false; | ||
public static $httponly = false; | ||
private $response = false; | ||
private static $instance; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public static function getInstance() | ||
{ | ||
if (!self::$instance) { | ||
self::$instance = new self(); | ||
} | ||
return self::$instance; | ||
} | ||
|
||
public function setResponse($response) | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
public function get($key, $default = null) | ||
{ | ||
if (! isset($_COOKIE[$key])) { | ||
return $default; | ||
} else { | ||
return $_COOKIE[$key]; | ||
} | ||
} | ||
|
||
public function set($key, $value, $expire = 0) | ||
{ | ||
if ($expire != 0) { | ||
$expire = time() + $expire; | ||
} | ||
$this->response->cookie($key, $value, $expire, self::$path, self::$domain, self::$secure, self::$httponly); | ||
} | ||
|
||
public function delete($key) | ||
{ | ||
unset($_COOKIE[$key]); | ||
$this->set($key, null); | ||
} | ||
} |
Oops, something went wrong.