Skip to content

Commit

Permalink
增加type类型配置,适应yii2basic项目类型
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Dec 26, 2017
1 parent e361a04 commit 662d9ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -66,10 +66,11 @@ yii2-swoole和php-fpm下的FeehiCMS
'swoole' => [
'class' => feehi\console\SwooleController::className(),
'rootDir' => str_replace('console/config', '', __DIR__ ),//yii2项目根路径
'app' => 'frontend',//app目录地址
'type' => 'advanced',//yii2项目类型,默认为advanced。此处还可以为basic
'app' => 'frontend',//app目录地址,如果type为basic,这里一般为空
'host' => '127.0.0.1',//监听地址
'port' => 9999,//监听端口
'web' => 'web',//默认为web。rootDir app web目的是拼接yii2的根目录,如果你的应用为basic,那么frontend为空即可
'web' => 'web',//默认为web。rootDir app web目的是拼接yii2的根目录,如果你的应用为basic,那么app为空即可
'debug' => true,//默认开启debug,上线应置为false
'env' => 'dev',//默认为dev,上线应置为prod
'swooleConfig' => [//标准的swoole配置项都可以再此加入
Expand All @@ -87,7 +88,7 @@ yii2-swoole和php-fpm下的FeehiCMS
'app' => 'backend',
'host' => '127.0.0.1',
'port' => 9998,
'web' => 'web',//默认为web。rootDir app web目的是拼接yii2的根目录,如果你的应用为basic,那么frontend为空即可
'web' => 'web',//默认为web。rootDir app web目的是拼接yii2的根目录,如果你的应用为basic,那么app为空即可
'debug' => true,//默认开启debug,上线应置为false
'env' => 'dev',//默认为dev,上线应置为prod
'swooleConfig' => [
Expand Down
33 changes: 20 additions & 13 deletions src/console/SwooleController.php
Expand Up @@ -38,7 +38,9 @@ class SwooleController extends \yii\console\Controller

public $rootDir = "";

public $app = "frontend";
public $type = "advanced";

public $app = "frontend";//如果type为basic,这里默认为空

public $web = "web";

Expand All @@ -65,23 +67,27 @@ public function actionStart()
$logDir = dirname($this->swooleConfig['log_file']);
if( !file_exists($logDir) ) FileHelper::createDirectory($logDir);

$rootDir = $this->rootDir;//网站根目录,basic为下载下来的yii2目录,advanced为frontend或backend的目录
$rootDir = $this->rootDir;//yii2项目根目录
$web = $rootDir . $this->app . DIRECTORY_SEPARATOR . $this->web;;

defined('YII_DEBUG') or define('YII_DEBUG', $this->debug);
defined('YII_ENV') or define('YII_ENV', $this->env);

require($rootDir . '/vendor/autoload.php');
//require($rootDir . '/vendor/yiisoft/yii2/Yii.php');
require($rootDir . '/common/config/bootstrap.php');
require($rootDir . $this->app . '/config/bootstrap.php');

$config = ArrayHelper::merge(
require($rootDir . '/common/config/main.php'),
require($rootDir . '/common/config/main-local.php'),
require($rootDir . $this->app . '/config/main.php'),
require($rootDir . $this->app . '/config/main-local.php')
);
if( $this->type == 'basic' ){
$config = require($rootDir . '/config/web.php');
}else {
require($rootDir . '/common/config/bootstrap.php');
require($rootDir . $this->app . '/config/bootstrap.php');

$config = ArrayHelper::merge(
require($rootDir . '/common/config/main.php'),
require($rootDir . '/common/config/main-local.php'),
require($rootDir . $this->app . '/config/main.php'),
require($rootDir . $this->app . '/config/main-local.php')
);
}

$this->swooleConfig = array_merge([
'document_root' => $web,
Expand All @@ -95,6 +101,7 @@ public function actionStart()
* @param \swoole_http_response $response
*/
$server->runApp = function ($request, $response) use ($config, $web) {
$yiiBeginAt = microtime(true);
$aliases = [
'@web' => '',
'@webroot' => $web,
Expand All @@ -113,7 +120,7 @@ public function actionStart()
];
$config['components']['response'] = isset($config['components']['response']) ? array_merge($config['components']['response'], $responseComponent) : $responseComponent;

$config['components']['session'] = isset($config['components']['session']) ? array_merge(['savePath'=>$web . '/../session'], $config['components']['session'], ["class" => Session::className()]) : ["class" => Session::className(), 'savePath'=>$web . '/../session'];
$config['components']['session'] = isset($config['components']['session']) ? array_merge(['savePath'=>$web . '/../runtime/session'], $config['components']['session'], ["class" => Session::className()]) : ["class" => Session::className(), 'savePath'=>$web . '/../session'];

$config['components']['errorHandler'] = isset($config['components']['errorHandler']) ? array_merge($config['components']['errorHandler'], ["class" => ErrorHandler::className()]) : ["class" => ErrorHandler::className()];

Expand All @@ -133,7 +140,7 @@ public function actionStart()

try {
$application = new Application($config);
yii::$app->getLog()->yiiBeginAt = microtime(true);
yii::$app->getLog()->yiiBeginAt = $yiiBeginAt;
yii::$app->setAliases($aliases);
try {
$application->state = Application::STATE_BEFORE_REQUEST;
Expand Down

0 comments on commit 662d9ee

Please sign in to comment.