<?php /** * @date 2015-9-13 * @author zhengyin <*****@*****.**> * @blog http://izhengyin.com * 执行方式: * /opt/app/php-5.5/bin/php /data/webroot/yaf-demo/cli/cli.php request_uri="/cli/test/run/name/zhengyin" */ ini_set('display_errors', 1); error_reporting(E_ALL); date_default_timezone_set('Asia/Shanghai'); define('APPLICATION_PATH', dirname(__DIR__)); $application = new Yaf\Application(APPLICATION_PATH . "/conf/application.ini"); $application->bootstrap(); $application->getDispatcher()->dispatch(new Yaf\Request\Simple()); $application->run();
<?php /* * 命令行工具入口 用法: * php /path/to/root/bus.php require_uri=/job/index * */ // 设置对路径 define('ROOT_PATH', __DIR__); define('APPLICATION_PATH', ROOT_PATH . '/application'); // 跳过SESSION define("RUN_IN_CLI", true); // 设置请求 // 1 运行模式 2 模块(非多模块请指定Index) 3 控制器名称 4 控制器方法 5 参数(数组) // $request = new Yaf\Request\Simple("CLI", "Index", "Job", "Index", array()); $request = new Yaf\Request\Simple(); // 应用配置 $application = new Yaf\Application(require ROOT_PATH . "/conf/global.php"); // 启动资源 $application->bootstrap(); // 分发请求 $application->getDispatcher()->dispatch($request);
/** * 构造方法,要执行的东西很多 * @author xuebing<*****@*****.**> */ public function __construct() { $config = new Yaf\Config\Ini(CONF_PATH . 'swoole.ini'); $config = $config->get('swoole'); self::$http = new swoole_http_server("0.0.0.0", $config->port); self::$http->set(array('worker_num' => $config->worker_num, 'max_conn' => $config->max_conn, 'max_request' => $config->max_request, 'ipc_mode' => $config->ipc_mode, 'task_worker_num' => $config->task_worker_num, 'task_ipc_mode' => $config->task_ipc_mode, 'task_max_request' => $config->task_max_request, 'dispatch_mode' => $config->dispatch_mode, 'daemonize' => $config->daemonize, 'backlog' => $config->backlog, 'open_tcp_keepalive' => $config->open_tcp_keepalive, 'tcp_defer_accept' => $config->tcp_defer_accept, 'open_tcp_nodelay' => $config->open_tcp_nodelay, 'log_file' => APP_PATH . '/log/swoole.log')); self::$http->on('request', function ($request, $response) { if (isset($request->post)) { self::$post = $request->post; } self::$raw_data = $request->rawContent(); echo "[" . date('Y-m-d H:i:s') . "]" . PHP_EOL . "请求地址==>" . $request->server['request_uri'] . PHP_EOL . "请求内容==>" . self::$raw_data . PHP_EOL; if (self::$raw_data == 'swoole_reload()') { self::$http->reload(); $response->end('reload success'); return; } try { // TODO handle $yaf_response = $this->application->getDispatcher()->dispatch(new Yaf\Request\Http($request->server['request_uri'])); $result = $yaf_response->getBody(); } catch (\Exception $e) { $result = array(); $result['err_code'] = $e->getCode(); $result['err_msg'] = $e->getMessage(); $result = json_encode($result, JSON_UNESCAPED_UNICODE); } echo "返回内容==>" . $result . PHP_EOL . PHP_EOL; $response->end($result); }); self::$http->on('Finish', function ($serv, $task_id, $data) { echo "异步任务完成[{$task_id}],data:" . $data . PHP_EOL; }); self::$http->on('Task', function ($serv, $task_id, $from_id, $data) { echo "新的异步任务[来自进程 {$from_id},当前进程 {$task_id}],data:" . $data . PHP_EOL; $data = unserialize($data); if (is_array($data)) { list($module, $controller, $action, $params) = $data; $request = new Yaf\Request\Simple('CLI', $module, $controller, $action, $params); $this->application->getDispatcher()->dispatch($request); $serv->finish("task -> OK"); } }); self::$http->on('Timer', function ($serv, $interval) { switch ($interval) { case 300000: break; } }); self::$http->on('Start', function ($serv) { cli_set_process_title("swoolehttp:main"); file_put_contents(APP_PATH . '/log/swoole-master.pid', $serv->master_pid); }); self::$http->on('WorkerStart', array($this, 'onWorkerStart')); self::$http->on('ManagerStart', function ($serv) { cli_set_process_title("swoolehttp:manager"); file_put_contents(APP_PATH . '/log/swoole-manager.pid', $serv->master_pid); }); self::$http->on('Shutdown', function ($serv) { echo 'Shutdown' . PHP_EOL; }); self::$http->on('WorkerError', function ($serv, $worker_id, $worker_pid, $exit_code) { }); self::$http->start(); }