diff --git a/src/Api.php b/src/Api.php index 8e9850a..61b4e0a 100644 --- a/src/Api.php +++ b/src/Api.php @@ -208,10 +208,11 @@ protected function userCheck() { * @return boolean */ protected function isServiceWhitelist() { - $api = DI()->request->getServiceApi(); - $action = DI()->request->getServiceAction(); + $di = DI(); + $api = $di->request->getServiceApi(); + $action = $di->request->getServiceAction(); - $serviceWhitelist = DI()->config->get('app.service_whitelist', array()); + $serviceWhitelist = $di->config->get('app.service_whitelist', array()); foreach ($serviceWhitelist as $item) { $cfgArr = explode('.', $item); if (count($cfgArr) < 2) { diff --git a/src/ApiFactory.php b/src/ApiFactory.php index 9fa6aad..b81e916 100644 --- a/src/ApiFactory.php +++ b/src/ApiFactory.php @@ -39,10 +39,11 @@ class ApiFactory { * @throws BadRequestException 非法请求下返回400 */ static function generateService($isInitialize = TRUE) { - $service = DI()->request->getService(); - $namespace = DI()->request->getNamespace(); - $api = DI()->request->getServiceApi(); - $action = DI()->request->getServiceAction(); + $di = DI(); + $service = $di->request->getService(); + $namespace = $di->request->getNamespace(); + $api = $di->request->getServiceApi(); + $action = $di->request->getServiceAction(); if (empty($api) || empty($action)) { throw new BadRequestException( diff --git a/src/DependenceInjection.php b/src/DependenceInjection.php index 8ba8273..68e69e5 100644 --- a/src/DependenceInjection.php +++ b/src/DependenceInjection.php @@ -113,7 +113,7 @@ public function onInitialize() { * @parms mixed $value service的值,可以是具体的值或实例、类名、匿名函数、数组配置 */ public function set($key, $value) { - $this->resetHit($key); + $this->hitTimes[$key] = 0; $this->data[$key] = $value; @@ -137,9 +137,13 @@ public function get($key, $default = NULL) { $this->data[$key] = $default; } - $this->recordHitTimes($key); + // 内联操作,减少函数调用,提升性能 + if (!isset($this->hitTimes[$key])) { + $this->hitTimes[$key] = 0; + } + $this->hitTimes[$key] ++; - if ($this->isFirstHit($key)) { + if ($this->hitTimes[$key] == 1) { $this->data[$key] = $this->initService($this->data[$key]); } @@ -206,21 +210,5 @@ protected function initService($config) { return $rs; } - - protected function resetHit($key) { - $this->hitTimes[$key] = 0; - } - - protected function isFirstHit($key) { - return $this->hitTimes[$key] == 1; - } - - protected function recordHitTimes($key) { - if (!isset($this->hitTimes[$key])) { - $this->hitTimes[$key] = 0; - } - - $this->hitTimes[$key] ++; - } } diff --git a/src/PhalApi.php b/src/PhalApi.php index 679559e..0853c61 100644 --- a/src/PhalApi.php +++ b/src/PhalApi.php @@ -40,11 +40,12 @@ class PhalApi { ``` */ public function response() { - $rs = DI()->response; + $di = DI(); + $rs = $di->response; try { // 接口调度与响应 $api = ApiFactory::generateService(); - $action = DI()->request->getServiceAction(); + $action = $di->request->getServiceAction(); $data = call_user_func(array($api, $action)); $rs->setData($data); @@ -54,9 +55,9 @@ public function response() { $rs->setMsg($ex->getMessage()); } catch (\Exception $ex) { // 不可控的异常 - DI()->logger->error(DI()->request->getService(), strval($ex)); + $di->logger->error(DI()->request->getService(), strval($ex)); - if (DI()->debug) { + if ($di->debug) { $rs->setRet($ex->getCode()); $rs->setMsg($ex->getMessage()); $rs->setDebug('exception', $ex->getTrace()); @@ -65,8 +66,8 @@ public function response() { } } - $rs->setDebug('stack', DI()->tracer->getStack()); - $rs->setDebug('sqls', DI()->tracer->getSqls()); + $rs->setDebug('stack', $di->tracer->getStack()); + $rs->setDebug('sqls', $di->tracer->getSqls()); $rs->setDebug('version', PHALAPI_VERSION); return $rs; diff --git a/src/Request.php b/src/Request.php index 4955b12..08593c4 100644 --- a/src/Request.php +++ b/src/Request.php @@ -44,7 +44,7 @@ class Request { /** * @var array $headers 备用数据源 请求头部信息 */ - protected $headers = array(); + protected $headers; /** * @var string 接口服务命名空间 @@ -91,7 +91,6 @@ public function __construct($data = NULL) { $this->data = $this->genData($data); // 备用数据源 - $this->headers = $this->getAllHeaders(); $this->get = $_GET; $this->post = $_POST; $this->request = $_REQUEST; @@ -150,6 +149,11 @@ protected function getAllHeaders() { * @return string */ public function getHeader($key, $default = NULL) { + // 延时加载,提升性能 + if ($this->headers === NULL) { + $this->headers = $this->getAllHeaders(); + } + return isset($this->headers[$key]) ? $this->headers[$key] : $default; } @@ -223,6 +227,9 @@ protected function &getDataBySource($source) { case 'COOKIE': return $this->cookie; case 'HEADER': + if ($this->headers === NULL) { + $this->headers = $this->getAllHeaders(); + } return $this->headers; case 'SERVER': return $_SERVER;