Example #1
0
 /**
  * 自定义异常处理
  *
  * @param mixed $e 异常对象
  */
 public function appException(&$e)
 {
     if (Cml::$debug) {
         $run = new Run();
         $run->pushHandler(Request::isCli() ? new PlainTextHandler() : new PrettyPageHandler());
         $run->handleException($e);
     } else {
         $error = [];
         $error['message'] = $e->getMessage();
         $trace = $e->getTrace();
         $error['files'][0] = $trace[0];
         if (substr($e->getFile(), -20) !== '\\Tools\\functions.php' || $e->getLine() !== 90) {
             array_unshift($error['files'], ['file' => $e->getFile(), 'line' => $e->getLine(), 'type' => 'throw']);
         }
         //正式环境 只显示‘系统错误’并将错误信息记录到日志
         Log::emergency($error['message'], [$error['files'][0]]);
         $error = [];
         $error['message'] = Lang::get('_CML_ERROR_');
         if (Request::isCli()) {
             \Cml\pd($error);
         } else {
             header('HTTP/1.1 500 Internal Server Error');
             View::getEngine('html')->reset()->assign('error', $error);
             Cml::showSystemTemplate(Config::get('html_exception'));
         }
     }
     exit;
 }
Example #2
0
 /**
  * 自定义异常处理
  *
  * @param mixed $e 异常对象
  */
 public function appException(&$e)
 {
     $error = [];
     $exceptionClass = new \ReflectionClass($e);
     $error['exception'] = '\\' . $exceptionClass->name;
     $error['message'] = $e->getMessage();
     $trace = $e->getTrace();
     foreach ($trace as $key => $val) {
         $error['files'][$key] = $val;
     }
     if (substr($e->getFile(), -20) !== '\\Tools\\functions.php' || $e->getLine() !== 90) {
         array_unshift($error['files'], ['file' => $e->getFile(), 'line' => $e->getLine(), 'type' => 'throw']);
     }
     if (!Cml::$debug) {
         //正式环境 只显示‘系统错误’并将错误信息记录到日志
         Log::emergency($error['message'], [$error['files'][0]]);
         $error = [];
         $error['message'] = Lang::get('_CML_ERROR_');
     }
     if (Request::isCli()) {
         pd($error);
     } else {
         header('HTTP/1.1 500 Internal Server Error');
         View::getEngine('html')->reset()->assign('error', $error);
         Cml::showSystemTemplate(Config::get('html_exception'));
     }
 }
Example #3
0
File: Cml.php Project: dlpc/cmlphp
 /**
  * 自定义异常处理
  *
  * @access public
  * @param mixed $e 异常对象
  */
 public static function appException($e)
 {
     Plugin::hook('cml.before_throw_exception', $e);
     $error = array();
     $error['message'] = $e->getMessage();
     $trace = $e->getTrace();
     foreach ($trace as $key => $val) {
         $error['files'][$key] = $val;
     }
     if (!$GLOBALS['debug']) {
         //正式环境 只显示‘系统错误’并将错误信息记录到日志
         Log::emergency($error['message'], array($error['files'][0]));
         $error = array();
         $error['message'] = Lang::get('_CML_ERROR_');
     }
     if (Request::isCli()) {
         pd($error);
     } else {
         header('HTTP/1.1 500 Internal Server Error');
         require Config::get('html_exception');
     }
 }
Example #4
0
 /**
  * 执行预处理语句
  *
  * @param object $stmt PDOStatement
  * @param bool $clearBindParams
  *
  * @return bool
  */
 public function execute($stmt, $clearBindParams = true)
 {
     //empty($param) && $param = $this->bindParams;
     $this->conf['log_slow_sql'] && ($startQueryTimeStamp = microtime(true));
     if (!$stmt->execute()) {
         $error = $stmt->errorInfo();
         throw new \InvalidArgumentException('Pdo execute Sql error!,【Sql : ' . $this->buildDebugSql() . '】,【Error:' . $error[2] . '】');
     }
     $slow = 0;
     if ($this->conf['log_slow_sql']) {
         $queryTime = microtime(true) - $startQueryTimeStamp;
         if ($queryTime > $this->conf['log_slow_sql']) {
             if (Plugin::hook('cml.mysql_query_slow', ['sql' => $this->buildDebugSql(), 'query_time' => $queryTime]) !== false) {
                 Log::notice('slow_sql', ['sql' => $this->buildDebugSql(), 'query_time' => $queryTime]);
             }
             $slow = $queryTime;
         }
     }
     if (Cml::$debug) {
         $this->debugLogSql($slow > 0 ? Debug::SQL_TYPE_SLOW : Debug::SQL_TYPE_NORMAL, $slow);
     }
     $this->currentSql = '';
     $clearBindParams && $this->clearBindParams();
     return true;
 }
Example #5
0
 /**
  * 根据key获取redis实例
  * 这边还是用取模的方式,一致性hash用php实现性能开销过大。取模的方式对只有几台机器的情况足够用了
  * 如果有集群需要,直接使用redis3.0+自带的集群功能就好了。不管是可用性还是性能都比用php自己实现好
  *
  * @param $key
  *
  * @return \Redis
  */
 private function hash($key)
 {
     $serverNum = count($this->conf['server']);
     $success = sprintf('%u', crc32($key)) % $serverNum;
     if (!isset($this->redis[$success]) || !is_object($this->redis[$success])) {
         $instance = new \Redis();
         $connectToRedisFunction = function ($host, $port, $isPersistentConnect) use($instance) {
             if ($isPersistentConnect) {
                 return $instance->pconnect($host, $port, 1.5);
             } else {
                 return $instance->connect($host, $port, 1.5);
             }
         };
         $isPersistentConnect = !(isset($this->conf['server'][$success]['pconnect']) && $this->conf['server'][$success]['pconnect'] === false);
         $connectResult = $connectToRedisFunction($this->conf['server'][$success]['host'], $this->conf['server'][$success]['port'], $isPersistentConnect);
         $failOver = null;
         if (!$connectResult && !empty($this->conf['back'])) {
             $failOver = $this->conf['back'];
             $isPersistentConnect = !(isset($failOver['pconnect']) && $failOver['pconnect'] === false);
             $connectResult = $connectToRedisFunction($failOver['host'], $failOver['port'], $isPersistentConnect);
         }
         if (!$connectResult && $serverNum > 1) {
             $failOver = $success + 1;
             $failOver >= $serverNum && ($failOver = $success - 1);
             $failOver = $this->conf['server'][$failOver];
             $isPersistentConnect = !(isset($failOver['pconnect']) && $failOver['pconnect'] === false);
             $connectResult = $connectToRedisFunction($failOver['host'], $failOver['port'], $isPersistentConnect);
         }
         if (!$connectResult) {
             Plugin::hook('cml.cache_server_down', [$this->conf['server'][$success]]);
             throw new CacheConnectFailException(Lang::get('_CACHE_CONNECT_FAIL_', 'Redis', $this->conf['server'][$success]['host'] . ':' . $this->conf['server'][$success]['port']));
         }
         $password = false;
         if (is_null($failOver)) {
             if (isset($this->conf['server'][$success]['password']) && !empty($this->conf['server'][$success]['password'])) {
                 $password = $this->conf['server'][$success]['password'];
             }
             isset($this->conf['server'][$success]['db']) && $instance->select($this->conf['server'][$success]['db']);
         } else {
             if (isset($failOver['password']) && !empty($failOver['password'])) {
                 $password = $failOver['password'];
             }
             isset($failOver['db']) && $instance->select($failOver['db']);
             Log::emergency('redis server down', ['downServer' => $this->conf['server'][$success], 'failOverTo' => $failOver]);
             Plugin::hook('cml.redis_server_down_fail_over', ['downServer' => $this->conf['server'][$success], 'failOverTo' => $failOver]);
         }
         if ($password && !$instance->auth($password)) {
             throw new \RuntimeException('redis password error!');
         }
         $instance->setOption(\Redis::OPT_PREFIX, $this->conf['prefix']);
         $this->redis[$success] = $instance;
     }
     return $this->redis[$success];
 }
Example #6
0
 /**
  * 使用的缓存配置 默认为使用default_cache配置的参数
  *
  * @param bool|array $conf
  *
  * @throws CacheConnectFailException | PhpExtendNotInstall
  */
 public function __construct($conf = false)
 {
     $this->conf = $conf ? $conf : Config::get('default_cache');
     if (extension_loaded('Memcached')) {
         $this->memcache = new \Memcached('cml_memcache_pool');
         $this->type = 1;
     } elseif (extension_loaded('Memcache')) {
         $this->memcache = new \Memcache();
         $this->type = 2;
     } else {
         throw new PhpExtendNotInstall(Lang::get('_CACHE_EXTEND_NOT_INSTALL_', 'Memcached/Memcache'));
     }
     if (!$this->memcache) {
         throw new PhpExtendNotInstall(Lang::get('_CACHE_NEW_INSTANCE_ERROR_', 'Memcache'));
     }
     $singleNodeDownFunction = function ($host, $port) {
         //这边挂掉调用此回调在几s内只会调用一次。其它情况使用memcache方法均返回flase不报错
         Plugin::hook('cml.cache_server_down', ['host' => $host, 'port' => $port]);
         Log::emergency('memcache server down', ['downServer' => ['host' => $host, 'port' => $port]]);
     };
     $allNodeDownFunction = function ($serverList) {
         Plugin::hook('cml.cache_server_down', $this->conf['server'], $serverList);
         //全挂
         throw new CacheConnectFailException(Lang::get('_CACHE_CONNECT_FAIL_', 'Memcache', json_encode($serverList)));
     };
     $downServer = 0;
     if ($this->type == 2) {
         //memcache
         foreach ($this->conf['server'] as $val) {
             if (!$this->memcache->addServer($val['host'], $val['port'])) {
                 Log::emergency('memcache server down', ['downServer' => $val]);
             }
         }
         $this->memcache->setFailureCallback($singleNodeDownFunction);
         $serverList = $this->memcache->getextendedstats();
         foreach ($serverList as $server => $status) {
             $status || $downServer++;
         }
         if (count($serverList) <= $downServer) {
             $allNodeDownFunction($serverList);
         }
         return;
     }
     if (md5(json_encode($this->conf['server'])) !== md5(json_encode($this->memcache->getServerList()))) {
         $this->memcache->quit();
         $this->memcache->resetServerList();
         $this->memcache->addServers(array_values($this->conf['server']));
         $this->memcache->setOptions([\Memcached::OPT_PREFIX_KEY => $this->conf['prefix'], \Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT, \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, \Memcached::OPT_SERVER_FAILURE_LIMIT => 1, \Memcached::OPT_RETRY_TIMEOUT => 30, \Memcached::OPT_AUTO_EJECT_HOSTS => true, \Memcached::OPT_REMOVE_FAILED_SERVERS => true]);
         \Memcached::HAVE_JSON && $this->memcache->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_JSON_ARRAY);
     }
     $serverStatus = $this->memcache->getStats();
     foreach ($serverStatus as $server => $status) {
         if ($status['pid'] < 1) {
             $downServer++;
             $server = explode(':', $server);
             $singleNodeDownFunction($server[0], $server[1]);
         }
     }
     if (count($serverStatus) <= $downServer) {
         $allNodeDownFunction($serverStatus);
     }
 }