/** * @return array The return value should include 'errno' and 'data' */ function dispatch() { $parse_url = $this->parse_url(); if ($parse_url['errno'] != self::ERRNO_OK) { return $parse_url; } $url_piece = $parse_url['data']; $router = call_user_func($this->_router, $url_piece); if (isset($router['errno']) && $router['errno'] === self::ERRNO_OK) { return ['errno' => self::ERRNO_OK, 'data' => $router]; } elseif (isset($router['errno'])) { switch ($router['errno']) { case self::ERRNO_FORBIDDEN: \Lib_Log::info('forbidden page! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_FORBIDDEN, 'data' => 'You cannot visit this page!']; case self::ERRNO_SERVER_ERR: \Lib_Log::error('page err! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_SERVER_ERR, 'data' => 'Something wrong!']; case self::ERRNO_NOT_FOUND: default: \Lib_Log::notice('page not found! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_NOT_FOUND, 'data' => 'PAGE NOT FOUND!']; } } else { \Lib_Log::notice('page not found! url:[%s] case:[%s]', [json_encode($parse_url), json_encode($router)]); return ['errno' => self::ERRNO_FORBIDDEN, 'data' => 'PAGE NOT FOUND!']; } }
/** * @param int $id * @return $this|null */ function set_current_ap($id) { if (isset($this->_ap_list[$id])) { $this->_current_ap_id = $id; $this->_current_ap = $this->_ap_list[$id]; } else { $ap_info = Module_AccessPoint_Ap::get_ap($id); if ($ap_info['errno'] !== Const_Err_Base::ERR_OK) { Lib_Log::notice('Get ap faild! Result: %s', Lib_Helper::format_err_struct($ap_info)); $this->_current_ap_id = null; $this->_current_ap = null; return null; } $ap_info = $ap_info['data']; $this->set_current_ap_info($id, $ap_info); } return $this; }
/** * @param string $hook_type * @param string $hook_name * @return null|array */ function get_hook($hook_type, $hook_name) { if (!$this->hook_exists($hook_type, $hook_name)) { return null; } $hook = $this->_hooks[$hook_type][$hook_name]; if (!isset($hook[1])) { Lib_Log::notice("Hook: [%s.%s] [%s] not has run_once flag", [strval($hook_type), $hook_name, var_export($hook[1], true)]); unset($this->_hooks[$hook_type][$hook_name]); return null; } if (!is_callable($hook[0])) { Lib_Log::notice("Hook: [%s.%s] [%s] not callable", [strval($hook_type), $hook_name, json_encode($hook[0])]); unset($this->_hooks[$hook_type][$hook_name]); return null; } return $hook; }
/** * @param int $hook_type * @param array $args * @return bool */ function run_hooks($hook_type = self::HOOK_TYPE_BEFORE_RUN, $args = []) { if (!isset($this->_hooks[$hook_type])) { return false; } foreach ($this->_hooks[$hook_type] as $_name => $_hook) { if (!isset($_hook[1])) { unset($this->_hooks[$hook_type][$_name]); continue; } if (is_callable($_hook[0])) { Lib_Log::debug("TASK_HOOK: call hook type [%d], name [%s], once: [%s]", [$hook_type, $_name, var_export($_hook[1], true)]); call_user_func_array($_hook[0], $args); } else { Lib_Log::notice("TASK_HOOK: hook [%s] not callable", json_encode($_hook[0])); } // run once, remove if ($_hook[1] == true) { unset($this->_hooks[$hook_type][$_name]); } } return true; }
/** * @param int $task_id * @param int $mid * @param string $class * @return string */ private function _loop_callback_hook($task_id, $mid, $class) { if (!isset($this->flow[Module_FlowManager_Main::FLOW_TYPE_MAIN]) || !isset($this->flow[Module_FlowManager_Main::FLOW_TYPE_STUFF])) { Lib_Log::info("empty task flow main or stuff, will not run callback hook, id: %d", $this->id); return null; } // 安全检查,只有主模块才能注册回调 $main_mids = $this->flow[Module_FlowManager_Main::FLOW_TYPE_MAIN]; if (!in_array($mid, $main_mids)) { Lib_Log::notice("mid [%d] not in flow, flow id: [%d]", [$mid, $this->id]); return null; } // 没有回调方案 $callback_relation_mids = $this->flow[Module_FlowManager_Main::FLOW_TYPE_STUFF]; if (!isset($callback_relation_mids[$mid])) { return null; } Lib_Log::debug(function () use($mid, $callback_relation_mids) { return vsprintf("FLOW_CALL: prepare to gen callback for mid [%d], mids [%s]", [$mid, json_encode($callback_relation_mids[$mid])]); }); // 生成回调 $callback = $this->gen_module_callback_in_loop($task_id, $mid); // 注册 if ($callback['errno'] == Const_Err_Base::ERR_OK) { $callback = $callback['data']; Lib_Log::debug(function () use($task_id, $mid, $callback, $class) { return vsprintf("FLOW_CALL: register callback to class [%s], mid: %d", [get_class($class), $mid]); }); $class->register_callback($callback); } else { Lib_Log::notice("FLOW_CALL: gen callback failed. return %s", Lib_Helper::format_err_struct($callback)); } }
/** * @param int $ap_id * @param int $mode * @param array $options * @param string $php_script * @return array */ static function exec_task_by_cli($ap_id, $mode = Module_FlowManager_Main::RUN_MODE_CALLBACK, $options = ['meta' => true], $php_script = 'tools/access_point.php') { $script = self::get_run_ap_script($ap_id, $mode, $options, $php_script); $result = 0; $output = null; exec($script, $output, $result); if ($result != 0) { Lib_Log::notice("run ap script failed. case: %s", $output); return Lib_Helper::get_err_struct($result, '执行失败'); } return Lib_Helper::get_return_struct('运行成功'); }
#!/usr/bin/env php <?php require_once dirname(dirname(dirname(__FILE__))) . '/app/init.php'; $opt = getopt('H:P:', ['json', 'php', 'gbk', 'utf8', 'debug']); if (!isset($opt['H']) || !isset($opt['P'])) { echo 'CMD -H host -P port [--json | --php] [--debug]' . PHP_EOL; exit; } $f = fopen("php://stdin", 'r'); $a = ''; while (!feof($f)) { $a = fgets($f); if (feof($f)) { break; } isset($opt['debug']) && p('data is:', $a); $x = isset($opt['p']) || isset($opt['php']) ? unserialize($a) : json_decode($a, true); isset($opt['debug']) && p('decode data is:', $x); if ($x) { isset($opt['gbk']) || Module_DataEntry_Main::convert_encoding($x, Module_DataEntry_Main::$chinese_fields); $conf = ['host' => $opt['H'], 'port' => $opt['P']]; $result = Module_DataEntry_Main::send_data($x, $conf); p($result); } else { Lib_Log::notice('decode data failed!!! data: %s', $a); } }