Commit 4884603d authored by tufengqi's avatar tufengqi

删除其他依赖

parent 90a85686
.buildpath
.settings/
.project
*.patch
.idea/
.git/
vendor/
temp/
*.lock
.phpintel/
.DS_Store
\ No newline at end of file
# swoft-yii2
完美滴在 swoft 框架中使用 yii2 的组件。
_此插件依赖于[yii2-swoole](https://github.com/deepziyu/yii2-swoole)实现_
## 已支持的组件
- yii2-db 易框架的 MySQL-Connect 、 ActiveRecord 等
- yii2-log 易框架的日志组件,推荐还是用 swoft-log 吧
- yii2-cache 易框架的缓存组件,
## 安装
#### 环境要求
- swoft-v1.0 以上
#### composer install 插件安装
- 在项目中的 `composer.json` 文件中添加依赖:
```json
{
"require-dev": {
"deepziyu/swoft-yii2": "dev-master"
}
}
```
- 执行 `$ php composer.phar update``$ composer update` 进行安装。
## 配置
在 swoft 项目的 config/app.php 中添加如下配置:
```php
\Swoft\App::setAliases([
'@swoft-yii2' => '@vendor/deepziyu/swoft-yii2/src'
]);
return [
'bootScan' => [
'deepziyu\swoft\yii' => \Swoft\App::getAlias('@swoft-yii2'),
],
'beanScan' => [
'deepziyu\swoft\yii' => \Swoft\App::getAlias('@swoft-yii2'),
],
'yiiConfig' => require __DIR__ . DS . 'yii.php',
];
```
yii.php 文件配置就是常规的 Yii-Config 了:
```php
return [
'env' => 'dev', // YII_ENV 的值
'debug' => true,// YII_DEBUG 的值
'config' => [ // 常规的 Yii-Config
'id' => 'swotf-test',
'basePath' => BASE_PATH,
'language' => 'zh-CN',
'timeZone' => 'Asia/Shanghai',
'bootstrap' => ['log'],
'components' => [
'request' => [
'cookieValidationKey' => 'php is the best!!',
],
'log' => [
'traceLevel' => 3,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => [
'class' => 'deepziyu\yii\swoole\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=testDb',
'username' => 'test',
'password' => 'testP',
'charset' => 'utf8',
'enableReloadSchema' => true,
'attributes' => [
//MysqlPoolPdo::POOL_MAX_SIZE => 50, 连接池的大小
//MysqlPoolPdo::POOL_MAX_SLEEP_Times => 0.1, 连接池满栈时等待的秒
],
],
'cache' => [
'class' => 'yii\caching\ArrayCache',
],
]
],
];
```
## 愉快的使用咯
```php
/**
* 测试 AR 查询
* @RequestMapping("/yii/ar")
*/
public function yiiAR()
{
return [
'query1' => \Yii::$app->getDb()->createCommand("select 1")->queryAll(),
'query2' => (new Query())->from('tag')->where(['tagid' => 1])->one(),
'query3' => (new class extends ActiveRecord
{
public static function tableName()
{
return 'tag2';
}
public function rules()
{
return [[['tagname', 'tagid'], 'safe']];
}
})->find()->asArray()->all(),
];
}
```
## more words 再多说几句
- 此插件会单独维护一份协程的上下文,和数据库连接池,so,会吃点性能咯
- 没有支持的组件最好不要用,嘿,容易出问题滴
- 我们不支持各种 defer 特性
## Chat && Help
Swoft 框架 QQ 交流群: 小紫羽@548173319
\ No newline at end of file
{
"name": "yii2_swoft_connector_33cn",
"require": {
"php": ">=7.0",
"swoft/framework": "^1.0",
"swoft/http-server": "^1.0",
"yii2_swoole_connector_33cn": "@dev"
},
"require-dev": {
"php": ">=7.0",
"swoft/framework": "^1.0",
"swoft/http-server": "^1.0",
"yii2_swoole_connector_33cn": "@dev"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
},
"yii2_swoole_connector_33cn": {
"type": "git",
"url": "git@gitlab.33.cn:_site-res/yii2_swoole_connector_33cn.git"
}
},
"autoload": {
"psr-4": {
"deepziyu\\swoft\\yii\\": "src/"
}
}
}
\ No newline at end of file
<?php
namespace deepziyu\swoft\yii\Bootstrap\Listener;
use Swoft\Bean\Annotation\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Http\Server\Event\HttpServerEvent;
/**
*
* @Listener(HttpServerEvent::AFTER_REQUEST)
*/
class AfterRequest implements EventHandlerInterface
{
/**
* @\Swoft\Bean\Annotation\Inject()
* @var \deepziyu\swoft\yii\YiiWeb
*/
public $instance = null;
public function handle(EventInterface $event)
{
HttpServerEvent::AFTER_REQUEST;
$this->instance->afterRequest();
}
}
\ No newline at end of file
<?php
namespace deepziyu\swoft\yii\Bootstrap\Listener;
use Swoft\Bean\Annotation\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use Swoft\Http\Server\Event\HttpServerEvent;
/**
*
* @Listener(HttpServerEvent::BEFORE_REQUEST)
*/
class BeforeRequest implements EventHandlerInterface
{
/**
* @\Swoft\Bean\Annotation\Inject()
* @var \deepziyu\swoft\yii\YiiWeb
*/
public $instance = null;
public function handle(EventInterface $event)
{
HttpServerEvent::BEFORE_REQUEST;
$this->instance->beforeRequest();
}
}
\ No newline at end of file
<?php
namespace deepziyu\swoft\yii\Bootstrap\Listener;
use deepziyu\swoft\yii\YiiWeb;
use Swoft\App;
use Swoft\Bean\Annotation\Listener;
use Swoft\Bootstrap\Listeners\Interfaces\WorkerStartInterface;
use Swoft\Bootstrap\Listeners\Interfaces\WorkerStopInterface;
use Swoft\Bootstrap\SwooleEvent;
use Swoole\Server;
use Swoft\Bean\Annotation\ServerListener;
/**
*
* @ServerListener(event={
* SwooleEvent::ON_WORKER_START
* })
*/
class YiiStartListener implements WorkerStartInterface, WorkerStopInterface
{
/**
* @\Swoft\Bean\Annotation\Inject()
* @var YiiWeb
*/
public $instance = null;
public function onWorkerStart(Server $server, int $workerId, bool $isWorker)
{
SwooleEvent::ON_WORKER_START;
$this->instance->onWorkerStart($server, $workerId, $isWorker);
}
public function onWorkerStop(Server $server, int $workerId)
{
SwooleEvent::ON_WORKER_STOP;
$this->instance->onWorkerStop($server, $workerId);
}
}
\ No newline at end of file
<?php
namespace deepziyu\swoft\yii;
use deepziyu\yii\swoole\coroutine\Context;
use deepziyu\yii\swoole\di\Container;
use deepziyu\yii\swoole\di\NullContainer;
use deepziyu\yii\swoole\web\Application;
use deepziyu\yii\swoole\web\Request;
use Swoft\App;
use Swoft\Core\RequestContext;
use Swoole\Server;
use Swoole\Http\Server as HttpServer;
use Swoole\Http\Request as SwooleRequest;
/**
* Class YiiWeb
* @Swoft\Bean\Annotation\Bean()
* @package deepziyu\yii\swoft
*/
class YiiWeb
{
public $config = [];
public $debug = true;
public $env = 'prod';
private $enable = true;
public function __construct()
{
}
public function onWorkerStart(Server $server, int $workerId, bool $isWorker)
{
if(!$isWorker){
$this->enable = false;
return false;
}
if(!$server instanceof HttpServer){
$this->enable = false;
return false;
}
$config = App::getProperties();
if(!empty($config['yiiConfig'])){
$yiiConfig = $config['yiiConfig'];
$this->config = $yiiConfig['config'];
$this->debug = $yiiConfig['debug'];
$this->env = $yiiConfig['env'];
}else{
$this->enable = false;
return false;
}
defined('YII_DEBUG') or define('YII_DEBUG', $this->debug);
defined('YII_ENV') or define('YII_ENV', $this->env);
define('YII_ENABLE_ERROR_HANDLER',false); //必须关闭 Yii 的错误处理器
$path = App::getAlias('@vendor/deepziyu/yii2-swoole/Yii.php');
require_once($path);
\Yii::$container = new NullContainer();
\Yii::$context = new Context();
return true;
}
public function onWorkerStop(Server $server, int $workerId)
{
//TODO Swoft is not supported this Event-Type;
}
public function beforeRequest()
{
if(!$this->enable){
return false;
}
\Yii::$context->setContextDataByKey(Context::COROUTINE_CONTAINER, new Container());
\Yii::$context->setContextDataByKey(Context::COROUTINE_APP, $this->getYiiApplication());
/** @var Request $yiiRequest */
$yiiRequest = \Yii::$app->getRequest();
$swooleRequest = RequestContext::getRequest()->getSwooleRequest();
$yiiRequest->setSwooleRequest($swooleRequest);
//\Swoole\Http\Response::end(); 后会释放Http请求的 buffer ,此后不能再使用 \Swoole\Http\Request::getRawBody() 了
$yiiRequest->getRawBody();
$yiiRequest->getCookies();
return true;
}
public function afterRequest()
{
if(!$this->enable){
return false;
}
//\Yii::getLogger()->flush();
\Yii::getLogger()->flush(true);
\Yii::$context->destory();
}
public function getYiiApplication()
{
return new Application($this->config);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment