Esempio n. 1
0
 /**
  * 获取路由控制器和动作
  */
 public function getRoute()
 {
     $params = isset($_SERVER['argv']) ? $_SERVER['argv'] : [];
     if (empty($params) || count($params) < 1) {
         throw new \Exception('console params is error!');
     }
     array_shift($params);
     if (empty($params)) {
         //显示帮助信息
         $this->help();
     }
     $pointcnt = substr_count($params[0], '.');
     if ($pointcnt != 1) {
         throw new \Exception('console route is error! for example: main.index');
     }
     $data = \H2O\base\Module::parseRoute($params[0]);
     //返回路由规则URL
     array_shift($params);
     $this->_checkParams($params);
     //参数格式检查
     return $data;
 }
Esempio n. 2
0
 /**
  * 启动
  */
 public function actStart()
 {
     $routep = $this->_getRoutePath();
     //路由规则path
     $logfile = $this->_logpath . $routep . DS . date('Ymd') . '.log';
     //记录日志信息 按天记录
     $module = \H2O::getContainer('module');
     $route = \H2O\base\Module::parseRoute($routep);
     //返回路由规则URL
     //启动时,要删除已产生的停止信号,防止启动时就退出
     $this->_deleteSignal($routep);
     //写入进程信息
     $pid = getmypid();
     $pidfile = $this->_logpath . $routep . '.pid';
     //进程存储文件
     File::write($pidfile, $pid . ':' . date('Y-m-d H:i:s') . PHP_EOL);
     //写入存储信息
     //循环业务处理
     while (true) {
         $signal = $this->_getSignal($routep);
         //获取信号
         if ($signal[0] == $this->_stopsignal) {
             //信号源为停止,并且是当前应用
             //清理进程信息
             if ($signal[1] == $pid) {
                 $apid = file($pidfile);
                 //读取所有进程信息
                 $apid = array_filter($apid);
                 //过滤空格
                 $tmpid = [];
                 foreach ($apid as $ap) {
                     $ap = trim($ap);
                     $opid = substr($ap, 0, strpos($ap, ':'));
                     if ($opid != $pid && !empty($ap)) {
                         $tmpid[] = $ap . PHP_EOL;
                     }
                 }
                 if (empty($tmpid)) {
                     //如果不存进程信息时,直接删除记录进程日志信息
                     File::remove($pidfile);
                     //删除运行时的日志
                 } else {
                     File::write($pidfile, implode('', $tmpid), false);
                     //新写入进程日志
                 }
             } else {
                 File::remove($pidfile);
                 //删除运行时的日志
             }
             exit;
         } else {
             $octr = $module->getController($route['controller']);
             //控制器对象
             $gwmethod = 'Gate' . ucfirst($route['action']);
             //方法网关
             if (method_exists($octr, $gwmethod)) {
                 //增加入口应用关口,可在此函数中处理业务逻辑,可实现定时任务等
                 if ($octr->{$gwmethod}()) {
                     //返回值只有为true时才执行相应的程序
                     $res = $octr->runAction(ucfirst($route['action']));
                     //执行操作
                     $content = 'pid:' . $pid . ' datetime:' . date('Y-m-d H:i:s') . ' response:' . $res . PHP_EOL;
                     File::write($logfile, $content);
                     //写入日志信息
                 }
             } else {
                 $res = $octr->runAction(ucfirst($route['action']));
                 //执行操作
                 $content = 'pid:' . $pid . ' datetime:' . date('Y-m-d H:i:s') . ' response:' . $res . PHP_EOL;
                 File::write($logfile, $content);
                 //写入日志信息
             }
         }
         sleep(1);
         //休眠时间 1秒
     }
 }
Esempio n. 3
0
 /**
  * 守护子进程
  */
 public function actDaemon()
 {
     $routep = $this->_getRoutePath();
     //路由规则path
     $module = \H2O::getContainer('module');
     $route = \H2O\base\Module::parseRoute($routep);
     //返回路由规则URL
     $octr = $module->getController($route['controller']);
     //控制器对象
     $gwmethod = 'Gate' . ucfirst($route['action']);
     //方法网关
     $pid = getmypid();
     //进程ID
     if (method_exists($octr, $gwmethod)) {
         //增加入口应用关口,可在此函数中处理业务逻辑,可实现定时任务等
         $gwm = $octr->{$gwmethod}();
         if ($gwm === true) {
             //返回值只有为true时才执行相应的程序
             $res = $octr->runAction(ucfirst($route['action']));
             //执行操作
             $response = 'pid:' . $pid . ' datetime:' . date('Y-m-d H:i:s') . ' response:' . $res . PHP_EOL;
         }
     } else {
         $res = $octr->runAction(ucfirst($route['action']));
         //执行操作
         $response = 'pid:' . $pid . ' datetime:' . date('Y-m-d H:i:s') . ' response:' . $res . PHP_EOL;
     }
     echo $response;
 }
Esempio n. 4
0
 /**
  * 获取路由控制器和动作
  */
 public function getRoute()
 {
     $routepath = $this->getRoutePath();
     if (empty($routepath)) {
         //默认路由规则
         $data = ['controller' => $this->_defaultController, 'action' => $this->_defaultAction];
     } else {
         //其他路由
         $routepath = $this->getRealPath($routepath);
         $routepath = strpos($routepath, '.') === false ? $routepath . '.' . ucfirst(strtolower($this->getMethod())) : $routepath;
         $data = \H2O\base\Module::parseRoute($routepath);
     }
     return $data;
 }
Esempio n. 5
0
 /**
  * 返回模板渲染后的字符串
  * @param string $tpl 模板文件
  * @param array $vars 需要传入模板的数据参数
  */
 public function render($tpl, $vars = [])
 {
     $ov = \H2O::getContainer('view');
     $ov->setFile($tpl);
     $ov->setController(new static());
     //设置依附的控制器
     $ov->setPath($this->getViewPath());
     $ov->setContent($this->getContent());
     $vars = array_merge(self::$viewglobalvars, $vars);
     //合并全局变量和局部变量
     $content = $ov->render($vars);
     if (empty($this->_layout) || !empty($this->_content)) {
         //非布局 或者已有内容信息,则当前已为布局模块
         return $content;
     } else {
         //有布局
         $route = Module::parseRoute($this->_layout);
         $o = \H2O::createObject($this->_namespace . '\\' . $route['controller']);
         $o->setContent($content);
         //设置主模块缓存
         return call_user_func([$o, 'act' . ucfirst($route['action'])]);
     }
 }