public function __construct($logName, $options = array())
 {
     $logName = trim($logName);
     if (empty($logName)) {
         $logName = 'zc';
     }
     if (stripos($logName, '.log') === false) {
         $logName .= '.log';
     }
     $logName = str_replace(array('/', '\\'), '-', $logName);
     $logDir = !empty($options['logDir']) ? $options['logDir'] : Zc::C(ZcConfigConst::LogDir);
     $logDir = rtrim($logDir, '/') . '/';
     $today = date('Y-m-d', time());
     $yesterday = date('Y-m-d', time() - 24 * 3600);
     $todayLogDir = $logDir . $today . '/';
     $yesterdayLogDir = $logDir . $yesterday . '/';
     $this->logFile = $todayLogDir . $logName;
     $this->mkdir($todayLogDir);
     // 加这个标识,纯粹就是为了让logstash能够得到multiline
     if (!file_exists($this->logFile)) {
         $yesterdayLogFile = $yesterdayLogDir . $logName;
         error_log(date("[c]") . " -- End {$yesterday} Log --\r\n", 3, $yesterdayLogFile);
         error_log(date("[c]") . " -- Start {$today} Log --\r\n", 3, $this->logFile);
     }
 }
Пример #2
0
 public function __construct($hostname = '', $username = '', $password = '', $database = '')
 {
     if (empty($hostname)) {
         $hostname = Zc::C(ZcConfigConst::DbHostname);
         $username = Zc::C(ZcConfigConst::DbUsername);
         $password = Zc::C(ZcConfigConst::DbPassword);
         $database = Zc::C(ZcConfigConst::DbDatabase);
     }
     $this->errorDisplay = Zc::C(ZcConfigConst::MonitorExitOnDbError) == true ? true : false;
     if (!($this->connection = mysql_connect($hostname, $username, $password, true))) {
         if ($this->errorDisplay) {
             exit('Error: Could not make a database connection using ' . $username . '@' . $hostname);
         }
         return false;
     }
     if (!mysql_select_db($database, $this->connection)) {
         if ($this->errorDisplay) {
             exit('Error: Could not connect to database ' . $database);
         }
         return false;
     }
     mysql_query("SET NAMES 'utf8'", $this->connection);
     mysql_query("SET CHARACTER SET utf8", $this->connection);
     mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection);
     mysql_query("SET SQL_MODE = ''", $this->connection);
 }
Пример #3
0
 /**
  * 全局唯一语言对象
  * @return ZcLanguage
  */
 static function getLanguageObject()
 {
     if (empty(self::$objectPool['language'])) {
         self::$objectPool['language'] = new ZcLanguage(Zc::C(ZcConfigConst::LanguageCurrent), Zc::C(ZcConfigConst::LanguageDefault));
     }
     return self::$objectPool['language'];
 }
Пример #4
0
 public function __construct()
 {
     $urlHandlerConfig = ZcFactory::getConfig()->get(ZcConfigConst::UrlHandler);
     if (!empty($urlHandlerConfig['file'])) {
         require_once Zc::C(ZcConfigConst::DirFsApp) . $urlHandlerConfig['file'];
     }
     $this->urlHandler = new $urlHandlerConfig['class']();
 }
Пример #5
0
 /**
  +----------------------------------------------------------
  * 渲染模板输出 供render方法内部调用
  +----------------------------------------------------------
  * @access public
  +----------------------------------------------------------
  * @param string $templateFile  模板文件
  * @param mixed $var  模板变量
  +----------------------------------------------------------
  * @return string
  +----------------------------------------------------------
 */
 protected function renderFile($templateFile = '', $renderData = '')
 {
     ob_start();
     ob_implicit_flush(0);
     if (!empty($renderData)) {
         extract($renderData, EXTR_OVERWRITE);
     }
     $widgetFile = Zc::C(ZcConfigConst::DirFsViewsWidget) . $templateFile . '.php';
     if (file_exists($widgetFile)) {
         include $widgetFile;
     }
     $content = ob_get_clean();
     return $content;
 }
Пример #6
0
 public function __construct($totalNum, $pageSize, $currPageNo, $otherConfig = array())
 {
     if ($totalNum) {
         $otherConfig['totalNum'] = $totalNum;
     }
     if ($pageSize) {
         $otherConfig['pageSize'] = $pageSize;
     }
     if ($currPageNo) {
         $otherConfig['currPageNo'] = $currPageNo;
     }
     //conf.php的配置,会成为整个应用级别的覆盖
     $config = array_merge(Zc::C(ZcConfigConst::Pagination), $otherConfig);
     $this->setDefaultValue();
     $this->config($config);
 }
 function __construct($logName, $options = array())
 {
     $logName = trim($logName);
     if (empty($logName)) {
         $logName = 'zc';
     }
     if (stripos($logName, '.log') === false) {
         $logName .= '.log';
     }
     if ($logName[0] != '/' && $logName[1] != ':') {
         $logDir = !empty($options['logDir']) ? $options['logDir'] : Zc::C(ZcConfigConst::LogDir);
         $logName = rtrim($logDir, '/') . '/' . $logName;
     }
     $logFile = $logName . '.' . date('Y-m-d', time());
     $this->logFile = $logFile;
     $logDir = dirname($logFile);
     $this->mkdir($logDir);
 }
 /**
  * 
  * 记录Log信息及其日志信息
  * 
  * @param string $message
  */
 public function log($message)
 {
     parent::log($message);
     if (!self::$isEnable) {
         return;
     }
     if (self::$isSupportedRedis && !self::$redis) {
         self::$redis = new Redis();
         try {
             self::$isSupportedRedis = self::$redis->connect(Zc::C(ZcConfigConst::LogHandlerLogstashredisHost), Zc::C(ZcConfigConst::LogHandlerLogstashredisPort), 1);
         } catch (Exception $ex) {
             self::$isSupportedRedis = false;
             parent::log(print_r($ex, true));
         }
     }
     if (self::$isSupportedRedis) {
         self::$redis->rPush($this->redisKey, '[' . $this->logName . '] ' . $message);
     }
 }
Пример #9
0
 /**
  * 寻找Layout的方法
  */
 private function getLayoutFiles($view)
 {
     if (empty($view)) {
         $view = $this->route;
     }
     $dirViewsLayout = Zc::C(ZcConfigConst::DirFsViewsLayout);
     $layoutFiles = array();
     $layoutFiles[] = $dirViewsLayout . 'default.php';
     $path = '';
     $parts = explode('/', $view);
     array_pop($parts);
     foreach ($parts as $part) {
         $path .= $part . '/';
         $layoutFiles[] = $dirViewsLayout . $path . 'default.php';
     }
     $layoutFiles[] = $dirViewsLayout . $view . '.php';
     $layoutFiles = array_reverse($layoutFiles);
     //想知道是按照什么顺序来寻找Layout的,把下面这行代码注释掉就行了
     //dump($layoutFiles);exit;
     return $layoutFiles;
 }
Пример #10
0
 public function __construct($route, $args = array())
 {
     $dirController = Zc::C(ZcConfigConst::DirFsLibsController);
     $this->route = $route;
     $path = '';
     $parts = explode('/', $route);
     foreach ($parts as $part) {
         $tryDir = $path . $part . '/';
         if (is_dir($dirController . $tryDir)) {
             $path = $tryDir;
             array_shift($parts);
             continue;
         }
         $tryClass = str_replace(' ', '', ucwords(strtolower(str_replace(array('-', '_'), ' ', $part)))) . 'Controller';
         $tryFile = $path . 'class.' . $tryClass . '.php';
         if (is_file($dirController . $tryFile)) {
             $this->file = $dirController . $tryFile;
             $this->class = $tryClass;
             array_shift($parts);
             break;
         }
     }
     if ($args) {
         $this->args = $args;
     }
     $method = array_shift($parts);
     if ($method) {
         //$method = preg_replace ( '/(?:^|_)(.?)/e', "strtoupper('$1')", $method );
         $method = preg_replace_callback('/(?:^|_)(.?)/', function ($matches) {
             return strtolower($matches[1]);
         }, $method);
         $method[0] = strtolower($method[0]);
         $this->method = $method;
     } else {
         $this->method = 'index';
         $this->route = $this->route . (substr($this->route, -1) == '/' ? 'index' : '/index');
     }
 }
Пример #11
0
 /**
  * Constructor: Single parameter allows setting of global encoding for use by
  * the current object. If PHP 5.4 is detected, additional ENT_SUBSTITUTE flag
  * is set for htmlspecialchars() calls.
  *
  * @param string $encoding
  * @throws Exception\InvalidArgumentException
  */
 public function __construct($encoding = null)
 {
     if ($encoding !== null) {
         $encoding = (string) $encoding;
         if ($encoding === '') {
             throw new Exception(get_class($this) . ' constructor parameter does not allow a blank value');
         }
         $encoding = strtolower($encoding);
         if (!in_array($encoding, $this->supportedEncodings)) {
             throw new Exception('Value of \'' . $encoding . '\' passed to ' . get_class($this) . ' constructor parameter is invalid. Provide an encoding supported by htmlspecialchars()');
         }
         $this->encoding = $encoding;
     } else {
         $this->encoding = Zc::C(ZcConfigConst::DefaultOutputEncoding);
     }
     if (defined('ENT_SUBSTITUTE')) {
         $this->htmlSpecialCharsFlags |= ENT_SUBSTITUTE;
     }
     // set matcher callbacks
     $this->htmlAttrMatcher = array($this, 'htmlAttrMatcher');
     $this->jsMatcher = array($this, 'jsMatcher');
     $this->cssMatcher = array($this, 'cssMatcher');
 }
Пример #12
0
 public function __construct($config = '')
 {
     if (empty($config)) {
         $config = Zc::C(ZcConfigConst::DbConfig);
     }
     $this->config = $config;
     // 初始化DB缓存
     if (isset($this->config['db_cache'])) {
         $cc = $this->config['db_cache'];
         $this->cache = Zc::getCache($cc['biz_name'], $cc['cache_type'], $cc['timestamp'], $cc['options']);
     }
     // 重新计算分布式DB的配置和权重
     foreach ($this->config['connections'] as $groupName => &$groupConfig) {
         $this->groupMapping[$groupName]['master'] = array('role' => 'master', 'db_id' => $groupConfig['master']['db_id'], 'read_weight' => isset($groupConfig['master']['read_weight']) ? $groupConfig['master']['read_weight'] : false);
         $this->dbIdMapping[$groupConfig['master']['db_id']] =& $groupConfig['master'];
         $this->dbIdMapping[$groupConfig['master']['db_id']]['role'] = 'master';
         if (!empty($groupConfig['slaves'])) {
             foreach ($groupConfig['slaves'] as &$slaveConfig) {
                 $this->dbIdMapping[$slaveConfig['db_id']] =& $slaveConfig;
                 $this->dbIdMapping[$slaveConfig['db_id']]['role'] = 'slave';
                 $this->groupMapping[$groupName][] = array('role' => 'slave', 'db_id' => $slaveConfig['db_id'], 'read_weight' => isset($slaveConfig['read_weight']) ? $slaveConfig['read_weight'] : false);
             }
         }
     }
     //重新计算下每台机器要分配的读权重
     foreach ($this->groupMapping as $groupName => &$group) {
         $countWeight = 0;
         $setCount = 0;
         foreach ($group as &$host) {
             if ($host['read_weight'] !== false) {
                 $setCount++;
             }
             $countWeight += $host['read_weight'];
         }
         if ($setCount === 0) {
             $setCount = count($group);
             $countWeight = 100;
         }
         $unsetWeight = (int) ($countWeight / $setCount);
         $currValve = 0;
         foreach ($group as &$host) {
             if ($host['read_weight'] === false) {
                 $host['read_weight'] = $unsetWeight;
             }
             $currValve += $host['read_weight'];
             $host['read_valve'] = $currValve;
         }
         $this->groupMaxValve[$groupName] = $currValve;
     }
 }
Пример #13
0
 /**
  * Zc框架执行Web MVC的入口函数。
  *
  * 我考虑是否把init方法作为私有,而然runMVC来得到rootdir和appdir,调用init来完成初始化框架和应用工作。
  * 这样其实隐含的一个逻辑,整个Zc框架,可以随着runMVC的参数不同,可以去跑不同的app应用。这个时候还需要把所有的Factory的对象池都清空掉。
  * 总之,需要把Zc的对象池都清空。
  */
 public static function runMVC($route = '')
 {
     //Zc::dump(ZcFactory::getConfig());
     //确保关闭魔术引号
     self::cleanQuotes();
     //URL rewrite
     $zcUrl = ZcFactory::getUrl();
     $zcUrl->parse();
     if (empty($route)) {
         $route = isset($_GET['route']) ? $_GET['route'] : Zc::C(ZcConfigConst::DefaultRoute);
     }
     $action = new ZcAction($route);
     $dispatcher = new ZcDispatcher();
     $dispatcher->dispatch($action);
 }