Skip to content

Commit

Permalink
Merge branch 'swoolefy-5.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Apr 21, 2024
2 parents 99f4d3e + 3cea5d1 commit 001ff5d
Show file tree
Hide file tree
Showing 64 changed files with 1,293 additions and 518 deletions.
52 changes: 35 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,42 @@ composer create-project bingcool/swoolefy:~5.1 myproject
```
<?php
// 在myproject目录下添加cli.php, 这个是启动项目的入口文件
include './vendor/autoload.php';
define('IS_DAEMON_SERVICE', 0);
define('IS_CRON_SERVICE', 0);
define('IS_CLI_SCRIPT', 0);
// 应用父目录
defined('ROOT_PATH') or define('ROOT_PATH', __DIR__);
include __DIR__.'/vendor/autoload.php';
// 启动目录
defined('START_DIR_ROOT') or define('START_DIR_ROOT', __DIR__);
// 应用父目录
defined('ROOT_PATH') or define('ROOT_PATH',__DIR__);
// 应用目录
defined('APP_PATH') or define('APP_PATH',__DIR__.'/'.ucfirst($_SERVER['argv'][2]));
registerNamespace(APP_PATH);
define('IS_WORKER_SERVICE', 0);
define('IS_CLI_SCRIPT', 0);
define('IS_CRON_SERVICE', 0);
define('PHP_BIN_FILE','/usr/bin/php');
date_default_timezone_set('Asia/Shanghai');
define('APP_NAMES', [
// 你的项目命名为App,对应协议为http协议服务器,支持多个项目的,只需要在这里添加好项目名称与对应的协议即可
'App' => 'http',
'Test' => 'http
// 你的项目命名为App,对应协议为http协议服务器,支持多个项目的,只需要在这里添加好项目名称与对应的协议即可
'Test' => 'http',
'Erp' => 'http',
'UdpService' => 'udp'
]);
// 启动前处理,比如加载.env
$beforeFunc = function () {
};
//$beforeFunc = function () {
// try {
// \Test\LoadEnv::load('192.168.1.101:8848','swoolefy','test','nacos-test','123456');
// }catch (\Throwable $exception) {
//
// }
//};
include __DIR__.'/swoolefy';
include './swoolefy';
```

Expand All @@ -172,9 +184,10 @@ include './swoolefy';
```
// 你定义的项目目录是App, 在myproject目录下执行下面命令行
swoole-cli cli.php create App
或者
php cli.php create App
或者
swoole-cli cli.php create App
// 执行完上面命令行后,将会自动生成App项目目录以及内部子目录
myproject
Expand All @@ -186,7 +199,7 @@ myproject
| | |—— cache.php // 缓存组件,可以继续添加其他组件,命名自由
| │ ├── dc.php //环境配置项
| │ └── constants.php
| | |—— config.php // 应用层配置
| | |—— app.php // 应用层配置
| |
| ├── Controller
| │ └── IndexController.php // 控制器层
Expand Down Expand Up @@ -236,6 +249,11 @@ swooole-cli cli.php stop App --force=1
// 查看进程状态
swooole-cli cli.php status App
// 完全重启服务
php cli.php restart App
或者
swooole-cli cli.php restart App
```

### 五、访问
Expand Down Expand Up @@ -264,7 +282,7 @@ class IndexController extends BController {
### 定义组件

应用层配置文件:
Config/config.php
Config/app.php

```
<?php
Expand Down
3 changes: 3 additions & 0 deletions Test/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Test;




use Common\Library\Db\Facade\Db;
use Swoolefy\Core\BootstrapInterface;
use Swoolefy\Exception\SystemException;
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions Test/Config/constants.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
defined('APP_PATH') or define('APP_PATH', dirname(__DIR__));
defined('LOG_PATH') or define('LOG_PATH', APP_PATH.'/Storage/Logs');
defined('CONFIG_PATH') or define('CONFIG_PATH', APP_PATH.'/Config');
defined('CONFIG_COMPONENT_PATH') or define('CONFIG_COMPONENT_PATH', CONFIG_PATH.'/component');
2 changes: 1 addition & 1 deletion Test/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function onInit() {
// ProcessManager::getInstance()->addProcess('test', \Test\Process\TestProcess\MultiCall::class);

// 创建一个定时器处理进程
// ProcessManager::getInstance()->addProcess('tick', \Test\Process\TickProcess\Tick::class);
ProcessManager::getInstance()->addProcess('tick', \Test\Process\TickProcess\Tick::class);

// 测试cron自定义进程
// ProcessManager::getInstance()->addProcess('cron', \Test\Process\CronProcess\Cron::class);
Expand Down
3 changes: 2 additions & 1 deletion Test/Middleware/Route/RateLimiterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Swoolefy\Http\RequestInput;
use Swoolefy\Http\ResponseOutput;
use Swoolefy\Core\RouteMiddleware;
use Swoolefy\Http\RouteOption;
use Test\Factory;

class RateLimiterMiddleware implements RouteMiddleware
Expand All @@ -14,7 +15,7 @@ public function handle(RequestInput $requestInput, ResponseOutput $responseOutpu
$rateLimit = Factory::getRateLimit();
$rateLimit->setRateKey($uri);
// 每10s内滑动窗口限制2次请求
$rateLimit->setLimitParams(30, 60, 120);
$rateLimit->setLimitParams($requestInput->getValue(RouteOption::API_LIMIT_NUM_KEY), $requestInput->getValue(RouteOption::API_LIMIT_WINDOW_SIZE_TIME_KEY));
if ($rateLimit->isLimit()) {
throw new \Exception("请求过快,请稍后重试!");
}else {
Expand Down
12 changes: 11 additions & 1 deletion Test/Module/Order/Controller/UserOrderController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Test\Module\Order\Controller;

use Common\Library\Db\Query;
use phpseclib3\Math\PrimeField\Integer;
use Swoolefy\Core\Application;
use Swoolefy\Core\Controller\BController;
Expand All @@ -17,7 +18,7 @@ class UserOrderController extends BController
*/
public function userList()
{
// $db = Factory::getDb();
$db = Factory::getDb();
// $query = $db->newQuery()->table('tbl_users')->where('user_id','>', '100')->limit(0,10);
// $count = $query->count();
// if ($count > 0) {
Expand All @@ -26,6 +27,15 @@ public function userList()
//
// var_dump($this->request->get);

$uid = 100;
$sql = $db->newQuery()->table('tbl_users')->when($uid > 90,function (Query $query) {
$query->where('user_id','>', '100')->limit(0,10);
})->fetchSql()->select();


var_dump($sql);


// 列表方式查询
$orderList = new OrderList();
$orderList->setUserId([101,102]);
Expand Down
2 changes: 1 addition & 1 deletion Test/Module/Order/Validation/UserOrderValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function userList(): array
)]

// Get Query array eg: ids[1]=22&ids[2]=333
#[OA\QueryParameter(name: 'product_ids', description: '产品名称', required: true, allowEmptyValue: true, allowReserved: true, schema: new OA\Schema(
#[OA\QueryParameter(name: 'product_ids', description: '产品ids', required: true, allowEmptyValue: false, allowReserved: true, schema: new OA\Schema(
type:'array',
items: new OA\Items(
type:'integer'
Expand Down
4 changes: 1 addition & 3 deletions Test/Process/CronProcess/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

class Cron extends AbstractProcess {

private $appConf;

public function run() {
try {
$this->appConf = Swfy::getAppConf();
$appConf = Swfy::getAppConf();
// 闭包回调模式
// CrontabManager::getInstance()->addRule('cron_test', '*/1 * * * *', function($cron) {
// $cid = \Swoole\Coroutine::getCid();
Expand Down
2 changes: 1 addition & 1 deletion Test/Process/LockProcess/LockOrder.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Test\Process\ListProcess;
namespace Test\Process\LockProcess;

use Swoolefy\Core\Process\AbstractProcess;
use Test\Factory;
Expand Down
42 changes: 0 additions & 42 deletions Test/Process/TickProcess/Tick.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,48 +121,6 @@ public function run() {

}


protected function testOrm($data)
{
$dc = [
'default' => 'mysql',
'connections' => [
'mysql' => [
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'bingcool',
// 用户名
'username' => 'root',
// 密码
'password' => '123456',
// 端口
'hostport' => '3306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
// 数据库表前缀
'prefix' => '',
// fetchType
'fetch_type' => \PDO::FETCH_ASSOC,
// 是否需要断线重连
'break_reconnect' => true,
// 是否支持事务嵌套
'support_savepoint' => false,
// sql执行日志条目设置,不能设置太大,适合调试使用,设置为0,则不使用
'spend_log_limit' => 30,
// 是否开启dubug
'debug' => 1
],
]
];

}

public function onReceive($msg, ...$args)
{

Expand Down
4 changes: 3 additions & 1 deletion Test/Router/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
],
'dispatch_route' => [\Test\Module\Order\Controller\UserOrderController::class, 'userList'],
//GroupTestMiddleware::class => GroupTestMiddleware::class
])->enableDbDebug(true)->withRateLimiterMiddleware(RateLimiterMiddleware::class, GroupTestMiddleware::class);
])
->enableDbDebug(true)
->withRateLimiterMiddleware(RateLimiterMiddleware::class, 60,60,GroupTestMiddleware::class);

Route::post('/user-order/userList', [
// 针对该接口启动sql-debug
Expand Down
10 changes: 9 additions & 1 deletion Test/Scripts/GenerateMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Swoolefy\Script\MainCliScript;

/**
* 执行命令生成表属性: php script.php start Test --r=GenerateMysql/generate --db=db --table=tbl_users
* 执行命令生成表属性: php script.php start Test --c=gen:mysql:schema --db=db --table=tbl_users
*/

/**
Expand All @@ -23,6 +23,14 @@

class GenerateMysql extends MainCliScript
{
/**
* @var string
*/
const command = "gen:mysql:schema";

/**
* @return void
*/
public function generate()
{
$db = getenv('db');
Expand Down
5 changes: 4 additions & 1 deletion Test/Scripts/GeneratePg.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@

class GeneratePg extends GenerateMysql
{

/**
* @var string
*/
const command = "gen:pgsql:schema";
}
55 changes: 55 additions & 0 deletions Test/Scripts/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Test\Scripts;

class Kernel
{
public static $commands = [
GenerateMysql::command => [GenerateMysql::class, 'generate'],
GeneratePg::command => [GeneratePg::class, 'generate'],
User\FixedUser::command => [User\FixedUser::class, 'fixName']
];

/**
* 任务调度配置
*
* @var array[]
*/
public static $schedule = [
// [
// 'command' => User\FixedUser::command,
// //'cron_expression' => 10, // 10s执行一次
// 'cron_expression' => '*/1 * * * *', // 每分钟执行一次
// 'desc' => '',
// ],
[
'command' => User\FixedUser::command,
'cron_expression' => 3600, // 10s执行一次
//'cron_expression' => '*/1 * * * *', // 每分钟执行一次
'desc' => '',
],
];


/**
* 配置化调度
*
* @return array
*/
public static function buildScheduleTaskList()
{
$appName = $_SERVER['argv'][2];
$scheduleList = [];
foreach (self::$schedule as $item) {
$item['cron_name'] = $item['command'].'-'.$item['cron_expression'];
$item['exec_bin_file'] = defined('PHP_BIN_FILE') ? PHP_BIN_FILE : '/usr/bin/php';
$item['fork_type'] = \Swoolefy\Worker\Cron\CronForkProcess::FORK_TYPE_PROC_OPEN;
$item['exec_script'] = "script.php start {$appName} --c={$item['command']} --daemon=1";
$item['params'] = [];
$scheduleList[] = $item;
}
return $scheduleList;
}


}
Loading

0 comments on commit 001ff5d

Please sign in to comment.