private static function getCheckResult() { $result = empty(self::$error); if (!$result) { $lang = get_lang('VALIDATE_PARAM_FAIL', json_encode(self::$userParams, JSON_UNESCAPED_UNICODE)); LoggerUtil::warn($lang); } return array('result' => $result, 'error' => self::$error, 'data' => self::$data); }
public function beforeException(Event $event, MvcDispatcher $dispatcher, \Exception $exception) { if ($exception instanceof DispatcherException) { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: HttpUtil::redirect('/errors/show404'); return false; } } LoggerUtil::error($exception->getMessage()); HttpUtil::redirect('/errors/show500'); return false; }
/** * @业务系统生成签名 * @param array $params * @param string $secret * @return string */ public static function createAPISign($params, $secret, $ignore = array()) { foreach ($ignore as $field) { unset($params[$field]); } ksort($params); $sortedReq = $secret; foreach ($params as $key => $val) { $sortedReq .= $key . $val; } $sortedReq .= $secret; LoggerUtil::info($sortedReq); return strtolower(md5($sortedReq)); }
public function run($name, $args) { $lastSeparator = strrpos($name, '_'); $rpcArgs = array('service' => substr($name, 0, $lastSeparator), 'method' => substr($name, $lastSeparator + 1), 'params' => $args); try { LoggerUtil::info('RPC CALL: ' . json_encode($rpcArgs, JSON_UNESCAPED_UNICODE)); $serviceName = $this->getServiceName($rpcArgs['service'], false); $service = new $serviceName(); $methodName = $rpcArgs['method']; if (!method_exists($service, $methodName)) { throw new \Exception(get_lang('METHOD_NOT_FOUND', $methodName)); } $result = call_user_func_array(array($service, $methodName), $rpcArgs['params']); LoggerUtil::info('RPC SUCC: ' . json_encode($result, JSON_UNESCAPED_UNICODE)); return $result; } catch (\Exception $e) { LoggerUtil::error('RPC FAIL: ' . $e->getMessage()); return data_pack(get_code('REMOTE_RPC_FAIL'), $e->getMessage()); } }
/** * 执行原生的查询sql * @params string $sql 查询的sql * @params array $params 查询的参数 * @params bool $read 是否是读实例 * @return false or array 执行失败返回false, 成功返回数组 */ public function execSelect($sql, $params = null) { try { $connect = $this->getReadConnection(); $result = $connect->query($sql, $params); if (false === $result) { LoggerUtil::error($connect->getErrorInfo()); return false; } $result = new \Phalcon\Mvc\Model\Resultset\Simple(null, $this, $result); return $result->toArray(); } catch (\Exception $e) { LoggerUtil::error($e->getMessage()); return false; } }
private static function logReqInfo() { $data = array('ip' => HttpUtil::getClientIp(), 'host' => isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '', 'uri' => $_SERVER['REQUEST_URI'], 'query' => $_SERVER['QUERY_STRING'], 'method' => $_SERVER['REQUEST_METHOD'], 'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 'cookie' => $_COOKIE, 'params' => $_REQUEST); LoggerUtil::info(json_encode($data, JSON_UNESCAPED_UNICODE)); }
<?php use App\Config\ServiceConfig; use App\Library\AppInit; use App\Library\HttpUtil; use App\Library\LoggerUtil; //定义项目的根目录 define('IS_CLI_APP', false); define('ROOT_PATH', dirname(__DIR__)); //引入自定义函数 require ROOT_PATH . '/app/library/functions.php'; //常量定义 init_app_constant(); //引入自动加载 init_app_autoload(); //初始化容器 init_app_di(); //注册服务 ServiceConfig::register(); //初始化的一系列操作 AppInit::initContext(); //执行请求 try { run_cgi_application(); } catch (\Exception $e) { LoggerUtil::error($e->getMessage()); HttpUtil::redirect('/errors/show500'); }
private static function registCommonService($di, $config) { $di->setShared('config', $config); $di->setShared('profiler', function () { return new \Phalcon\Db\Profiler(); }); $di->setShared('modelsMetadata', function () use($di, $config) { if ('file' == $config->metaData->saveType) { $savePath = $config->metaData->savePath; if (!file_exists($savePath)) { mkdir($savePath, 0744, true); } $metaData = new \Phalcon\Mvc\Model\Metadata\Files(array('metaDataDir' => $savePath)); return $metaData; } }); $di->setShared('db_myPhalcon_w', function () use($di, $config) { $profiler = $di->getProfiler(); $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('db', function ($event, $connection) use($profiler) { if ($event->getType() == 'beforeQuery') { $profiler->startProfile($connection->getSQLStatement(), $connection->getSqlVariables(), $connection->getSQLBindTypes()); } if ($event->getType() == 'afterQuery') { $profiler->stopProfile(); $profile = $profiler->getLastProfile(); LoggerUtil::info(sprintf('SQL %s , cost time : %s', $profile->getSQLStatement(), $profile->getTotalElapsedSeconds())); } }); $db = new DbAdapter(array('host' => $config->mysql->myPhalcon_w->host, 'username' => $config->mysql->myPhalcon_w->username, 'password' => $config->mysql->myPhalcon_w->password, 'dbname' => $config->mysql->myPhalcon_w->dbname, 'port' => $config->mysql->myPhalcon_w->port)); $db->setEventsManager($eventsManager); return $db; }); $di->setShared('db_myPhalcon_r', function () use($di, $config) { $profiler = $di->getProfiler(); $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('db', function ($event, $connection) use($profiler) { if ($event->getType() == 'beforeQuery') { $profiler->startProfile($connection->getSQLStatement(), $connection->getSqlVariables(), $connection->getSQLBindTypes()); } if ($event->getType() == 'afterQuery') { $profiler->stopProfile(); $profile = $profiler->getLastProfile(); LoggerUtil::info(sprintf('SQL: %s , COST TIME: %s', $profile->getSQLStatement(), $profile->getTotalElapsedSeconds())); } }); $db = new DbAdapter(array('host' => $config->mysql->myPhalcon_r->host, 'username' => $config->mysql->myPhalcon_r->username, 'password' => $config->mysql->myPhalcon_r->password, 'dbname' => $config->mysql->myPhalcon_r->dbname, 'port' => $config->mysql->myPhalcon_r->port)); $db->setEventsManager($eventsManager); return $db; }); $di->setShared('redisCache', function () use($di, $config) { require VENDOR_PATH . '/predis/Autoloader.php'; \Predis\Autoloader::register(); $host = $config->redisCache->host; $port = $config->redisCache->port; return new \Predis\Client("tcp://{$host}:{$port}"); }); $di->setShared('curl', function () use($di, $config) { require VENDOR_PATH . '/Curl/Autoloader.php'; \Curl\Autoloader::register(); return new \Curl\Curl(); }); $di->setShared('image', function () use($di, $config) { require VENDOR_PATH . '/Image/Autoloader.php'; \Image\Autoloader::register(); return new \Image\Image(\Image\Image::IMAGE_IMAGICK); }); }