Skip to content

Commit

Permalink
第一个可用版本
Browse files Browse the repository at this point in the history
  • Loading branch information
ywisax committed Nov 15, 2016
1 parent ea47802 commit 077dde4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ return [
'server' => [
'count' => 4,
],
'logFile' => __DIR__ . '/../runtime/workerman.log',
],
],
];
Expand Down
11 changes: 9 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,20 @@ public function beforeRun()
$this->getErrorHandler()->setConnection($this->getConnection());
$this->getRequest()->setConnection($this->getConnection());
$this->getRequest()->setHostInfo('http://' . $_SERVER['HTTP_HOST']);
$this->getRequest()->setPathInfo($_SERVER['REQUEST_URI']);
$this->getRequest()->setPathInfo($_SERVER['ORIG_PATH_INFO']);
$this->getResponse()->setConnection($this->getConnection());
foreach ($this->bootstrap as $k => $component)
{
if ( ! is_object($component))
{
$component = $this->get($component);
if ($this->has($component))
{
$component = $this->get($component);
}
elseif ($this->hasModule($component))
{
$component = $this->getModule($component);
}
}
if (in_array(get_class($component), $this->bootstrapRefresh))
{
Expand Down
8 changes: 8 additions & 0 deletions src/commands/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class ServerController extends Controller
*/
public function actionHttp($app)
{
// 重新组装一次 $argv
global $argv;
unset($argv[0]);
unset($argv[2]);
$argv = array_values($argv);
//print_r($argv);
//return;

/** @var HttpServer $server */
$server = new HttpServer;
$server->run($app);
Expand Down
70 changes: 38 additions & 32 deletions src/server/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function run($app)
$this->debug = $this->config['debug'];
}
$this->root = $this->config['root'];

Worker::$logFile = $this->config['logFile'];

$this->server = new Worker("http://{$this->config['host']}:{$this->config['port']}");
foreach ($this->config['server'] as $k => $v)
{
Expand All @@ -97,6 +100,28 @@ public function onWorkerStart($worker)
{
$this->setProcessTitle($this->name . ': worker');

$_SERVER = [
'QUERY_STRING' => '',
'REQUEST_METHOD' => '',
'REQUEST_URI' => '',
'SERVER_PROTOCOL' => '',
'SERVER_NAME' => '',
'HTTP_HOST' => '',
'HTTP_USER_AGENT' => '',
'HTTP_ACCEPT' => '',
'HTTP_ACCEPT_LANGUAGE' => '',
'HTTP_ACCEPT_ENCODING' => '',
'HTTP_COOKIE' => '',
'HTTP_CONNECTION' => '',
'REMOTE_ADDR' => '',
'REMOTE_PORT' => '0',
];

$file = $this->root . '/' . $this->indexFile;
$_SERVER['SCRIPT_FILENAME'] = $file;
$_SERVER['DOCUMENT_ROOT'] = $this->root;
$_SERVER['SCRIPT_NAME'] = $_SERVER['DOCUMENT_URI'] = '/' . $this->indexFile;

// 关闭Yii2自己实现的异常错误
defined('YII_ENABLE_ERROR_HANDLER') || define('YII_ENABLE_ERROR_HANDLER', false);
// 每个worker都创建一个独立的app实例
Expand Down Expand Up @@ -164,27 +189,8 @@ public function onWorkerStop($worker)
*/
public function onMessage($connection, $data)
{
// 测试DI Container性能
// $j = 100000;
// $s1 = microtime(true);
// for ($i=0; $i<$j; $i++)
// {
// $obj = Yii::createObject('yii\web\Request');
// }
// $t1 = microtime(true) - $s1;
// // 更换新的Container
// $s2 = microtime(true);
// Yii::$container = new Container();
// for ($i=0; $i<$j; $i++)
// {
// $obj = Yii::createObject('yii\web\Request');
// }
// $t2 = microtime(true) - $s2;
// $response->end(json_encode(['t1' => $t1, 't2' => $t2]));
// return;

//$id = posix_getpid();
//echo "id: $id\n";
// $id = posix_getpid();
// echo "id: $id\n";
// $t = '<pre>';
// $t .= print_r($_SERVER, true);
// $t .= '</pre>';
Expand All @@ -197,8 +203,12 @@ public function onMessage($connection, $data)
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
}

$uri = $_SERVER['REQUEST_URI'];
$_SERVER['REQUEST_SCHEME'] = 'http';
$urlInfo = parse_url($_SERVER['REQUEST_URI']);
//var_dump($urlInfo);
$uri = $_SERVER['ORIG_PATH_INFO'] = $urlInfo['path'];
$file = $this->root . $uri;

if ($uri != '/' && is_file($file) && pathinfo($file, PATHINFO_EXTENSION) != 'php')
{
// 非php文件, 最好使用nginx来输出
Expand All @@ -209,13 +219,15 @@ public function onMessage($connection, $data)
}
else
{
// 准备环境信息
// 只要进入PHP的处理流程, 都默认转发给Yii来做处理
// 这样意味着, web目录下的PHP文件, 不会直接执行
$file = $this->root . '/' . $this->indexFile;
$_SERVER['SCRIPT_FILENAME'] = $file;
$_SERVER['DOCUMENT_ROOT'] = $this->root;
$_SERVER['SCRIPT_NAME'] = $_SERVER['DOCUMENT_URI'] = '/' . $this->indexFile;
$_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'] = $_SERVER['DOCUMENT_URI'] = '/' . $this->indexFile;
$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true);
$_SERVER['REQUEST_TIME'] = time();

// $connection->send(print_r($_SERVER, true));
// return;

// 使用clone, 原型模式
// 所有请求都clone一个原生$app对象
Expand All @@ -234,12 +246,6 @@ public function onMessage($connection, $data)

try
{
//$t = '<pre>';
//$t .= print_r($_SERVER, true);
//$t .= print_r($request, true);
//$t .= '</pre>';
//$response->end($t);
//return;
$app->run();
$app->afterRun();
}
Expand Down

0 comments on commit 077dde4

Please sign in to comment.