public function errorAction(E $exception) { if (Registry::get('config')->environment != 'pro') { echo '<pre>'; print_r($exception); } Yaf\Dispatcher::getInstance()->autoRender(false); switch ($exception->getCode()) { case \YAF\ERR\NOTFOUND\MODULE: case \YAF\ERR\NOTFOUND\CONTROLLER: case \YAF\ERR\NOTFOUND\ACTION: case \YAF\ERR\NOTFOUND\VIEW: case 404: header("Content-type: text/html; charset=utf-8"); header("status: 404 Not Found"); $this->display("404"); break; default: header("Content-type: text/html; charset=utf-8"); header("status: 500 Internal Server Error"); if (Registry::get('config')->environment == 'pro') { $this->display("500"); } else { echo $exception->getMessage(); } break; } }
/** * undocumented function * * @return void * @author hwz **/ public function __construct() { $this->loader = new Twig_Loader_Filesystem(APPLICATION_PATH . '/application/views'); $this->twig = new Twig_Environment($this->loader, array('cache' => Registry::get('config')->twig->cache_dir, 'debug' => Registry::get('config')->twig->debug)); $this->_tplVars = array(); $this->registerFunc(); }
public function routerShutdown(Request_Abstract $request, Response_Abstract $response) { $this->_smarty = Registry::get("Smarty"); $smarty_cfg = Registry::get("config")->get("smarty")->toArray(); $this->_smarty->setScriptPath($smarty_cfg[strtolower($request->module)]['template_dir']); $this->_smarty->setCompilePath($smarty_cfg[strtolower($request->module)]['compile_dir']); }
/** * 取得配置服务地址 */ public static function getServerAddress($userId) { $site = \Yaf\Registry::get('g_config')->site; $port = intval(self::$ports[$userId % count(self::$ports)]); $serverAddress = trim($site['ims'], '/') . ':' . $port; return $serverAddress; }
/** * ServiceApi初始化 */ public function init() { Dispatcher::getInstance()->returnResponse(true); Dispatcher::getInstance()->disableView(); $this->_config = Registry::get('config'); $this->_initRequestData(); }
/** * 路由结束之后触发 此时路由一定正确完成, 否则这个事件不会触发 * * @param Request_Abstract $request * @param Response_Abstract $response * @return mixed|void */ public function routerShutdown(Request_Abstract $request, Response_Abstract $response) { $config = Registry::get("config")->smarty->toArray(); $config['template_dir'] = $config['template_dir'] . $request->module . '/'; $smarty = new Smarty\Adapter(null, $config); Dispatcher::getInstance()->setView($smarty); }
public function init() { $this->_config = Registry::get("config"); $this->_session = \Yaf\Session::getInstance(); $this->_session->start(); $this->_layout = Registry::get('layout'); }
public static function create($which = 'master') { $db_config = \Yaf\Registry::get('config')->db->{$which}; $class_name = '\\Db\\' . ucfirst(strtolower($db_config->type)); $db = $class_name::getInstance($db_config); return $db instanceof DbInterface ? $db : false; }
/** * 获取application * * @return \Yaf\Application */ public function getApplication() { $application = \Yaf\Registry::get('application'); if (!$application) { $this->setApplication(); } return \Yaf\Registry::get('application'); }
public function routerShutdown(Yaf\Request_Abstract $request, Yaf\Response_Abstract $response) { if ($request->controller == 'Api') { require \Yaf\Registry::get('config')->application->phpwind->wekit_bin; Wekit::init('phpwind'); $application = Wind::application('phpwind', Wekit::S()); } }
/** * @param \Yaf\Dispatcher $dispatcher * @return \Yaf\Router */ protected function _initRoute(\Yaf\Dispatcher $dispatcher) { if (\Yaf\Registry::get("Config")->routes) { $router = $dispatcher->getRouter(); $router->addConfig(\Yaf\Registry::get("Config")->routes); return $router; } }
/** * @brief action的路由代理 * @throws \Exception */ public function init() { $strAction = $this->getRequest()->getActionName(); $arrActionPath = explode('_', $strAction); $strSuffix = \Yaf\Registry::get('application')->ext; $strActionPath = 'actions' . DS . implode(DS, $arrActionPath) . ".{$strSuffix}"; $this->actions[$strAction] = $strActionPath; }
/** * 获取数据库配置项 * @param $name 操作的库 */ private static function getDbConf($name) { $conf = \Yaf\Registry::get('config')->db; if (!empty($conf[$name]) && is_array($conf[$name])) { return $conf[$name]; } throw new \PdoException('dbConf[' . $name . '] is not defined!'); }
/** * 获取redis配置项 * @param $name redis节点名称 */ private static function getRedisConf($name) { $conf = \Yaf\Registry::get('config')->redis; if (!empty($conf[$name]) && is_array($conf[$name])) { return $conf[$name]; } throw new \RedisException('reidsConf[' . $name . '] is not defined!'); }
public static function getInstance() { if ($instance = \Yaf\Registry::get('ApplicationInit_Medoo')) { return $instance; } $instance = new self(); \Yaf\Registry::set('ApplicationInit_Medoo', $instance); return $instance; }
function __construct() { $config = \Yaf\Registry::get('config'); $this->production_mode = $config->get('umeng.production_mode'); // $this->production_mode = 'true'; $this->appkey = '573ec8aa67e58edc5c002624'; $this->appMasterSecret = 'l72wzou7glpqv3h4l2vqcsdjfnmmsvhu'; $this->timestamp = strval(time()); }
function __construct($table_name = null) { $this->db = \Yaf\Registry::get('db'); if (empty($table_name)) { $this->table_name = strtolower(explode('Dao', get_class($this))[0]); } else { $this->table_name = $table_name; } }
/** * 构造函数 * */ public function __construct() { // 主数据库 $this->dbMaster = new \Zend\Db\Adapter\Adapter(\Yaf\Registry::get('config')->database->master->toArray()); // 从数据库 $this->dbSlave = new \Zend\Db\Adapter\Adapter(\Yaf\Registry::get('config')->database->slave->toArray()); // 默认使用从数据库 $this->db =& $this->dbSlave; }
/** * smarty 自定义标签 * * @param $config * @param null $smarty * @return null */ public static function Assets($config, $smarty = null) { if ($config['url'] && preg_match('/\\.(png|jpg|bmp|gif|svg)$/', $config['url'])) { echo Registry::get('config')->api['static_image'] . $config['url'] . '?' . Registry::get('config')->api['static_assets_date']; } else { echo Registry::get('config')->api['static_assets'] . $config['url'] . '?' . Registry::get('config')->api['static_assets_date']; } return null; }
/** * 路由结束之后触发 此时路由一定正确完成, 否则这个事件不会触发 * * @param Request_Abstract $request * @param Response_Abstract $response * @return mixed|void */ public function routerShutdown(Request_Abstract $request, Response_Abstract $response) { if ($this->_checkModuleTpl($request->module)) { //只有这些模板才注入 $config = Registry::get("config")->smarty->toArray(); $config['template_dir'] = $config['template_dir'] . $request->module . '/'; $smarty = new TheFairLib\Smarty\Adapter(null, $config); Dispatcher::getInstance()->setView($smarty); } }
/** * 连接缓存 * @access public * @param string $type 缓存类型 * @param array $options 配置数组 * @return object */ public function connect($type = '', $options = array()) { if (empty($type)) { $type = \Yaf\Registry::get("config")->cache['type']; } $class = strpos($type, '\\') ? $type : 'Cache\\Driver\\' . strtolower($type); if (class_exists($class)) { $cache = new $class($options); } else { exit('没有找到:' . $type); } return $cache; }
public function postDispatch(Request_Abstract $request, Response_Abstract $response) { /* get the body of the response */ $body = $response->getBody(); /*clear existing response*/ $response->clearBody(); /* wrap it in the layout */ $layout = Registry::get("Smarty"); $layout->content = $body; $layout->assign('layout', $this->_layoutVars); /* set the response to use the wrapped version of the content */ $response->setBody($layout->render($this->_layoutFile)); }
/** * 初始化session配置 */ private function _init() { $this->config = \Yaf\Registry::get('config')->session; $this->lifeTime = $this->config->expire; $this->sessionName = $this->config->prefix; // 设置cookiedomain ini_set('session.cookie_domain', $this->config->cookie_domain); // 设置最大失效时间 ini_set('session.gc_maxlifetime', $this->config->cookie_lifetime); $hander = self::$instance; session_set_save_handler(array(&$hander, "open"), array(&$hander, "close"), array(&$hander, "read"), array(&$hander, "write"), array(&$hander, "destroy"), array(&$hander, "gc")); // 判断是否默认开启session session_start(); }
/** * Add Comment * * @param Array * @return Boolean , if insert complete,return true */ public function addComment($request) { $comment = self::instance(); // $user = Members::getCurrentUser(); $userState = MemberStateTemp::getCurrentUserForAuth(); if ($request and $userState) { $common = \Yaf\Registry::get('common'); $data = array('post_id' => $request->getPost('bid'), 'type' => $request->getPost('type'), 'uid' => $userState['uid'], 'title' => $request->getPost('title'), 'content' => $request->getPost('content'), 'ip' => $common->ip(), 'published' => $request->getPost('published'), 'parent' => $request->getPost('parent')); if ($comment->insert($data)) { return true; } } return false; }
/** * 获取数据库写实例 */ public static function getWriteInstance() { if (!isset(self::$instance['w']) || !is_object(self::$instance['w'])) { try { $config = \Yaf\Registry::get('config'); $dsn = sprintf("%s:dbname=%s;host=%s;port=%s", $config->db->type, $config->db->name, $config->db->write->host, $config->db->port); self::$_db = new PDO($dsn, $config->db->write->user, $config->db->write->pwd); // echo 'db instance write<br>'; // 捕获异常,后期改成抛出 yaf error } catch (Excepton $e) { E('database error', 404); } } self::$instance['w'] = self::$_db; return self::$_db; }
/** * 获取redis连接 * * @staticvar null $redis * @return \Redis * @throws \Exception */ public function getRedis() { if (!self::$redis) { $conf = \Yaf\Registry::get('config')->get('redis.database.params'); if (!$conf) { throw new \Exception('redis连接必须设置'); } if (isset($conf['prefix'])) { self::$prefix = $conf['prefix']; } self::$redis = new \Redis(); self::$redis->connect($conf['host'], $conf['port']); } self::$redis->select($this->_db); return self::$redis; }
protected function connect($config = array()) { if (empty($config)) { $config = $this->_configName; } $_config = array(); /** @var \Yaf\Config\Ini $sysConfig */ $sysConfig = Registry::get('config'); if ($sysConfig && ($tmp = $sysConfig->get($config))) { $_config = $tmp->toArray(); } elseif (is_array($config)) { $_config = $config; } $_config['adapter'] = 'Redis'; $_config['params'][\Redis::OPT_PREFIX] = $this->_keyPrefix ?: get_class($this); $_config['params'][\Redis::OPT_SERIALIZER] = \Redis::SERIALIZER_PHP; return Cache::instance('__redis_model__', $_config); }
private static function check_service($_name, $_type = 'service') { $_result = FALSE; $_conf = \Yaf\Registry::get('service'); switch ($_type) { case 'method': if (isset($_conf['method'][$_name]) && $_conf['method'][$_name] == '1') { $_result = TRUE; } break; case 'service': default: if (isset($_conf['service'][$_name]) && $_conf['service'][$_name] == '1') { $_result = TRUE; } break; } return $_result; }
/** * [link description] * @param [type] $url [description] * @param string $charset [description] * @param [type] $option [description] * @return [array] [lead_image_url,word_count,title,content] */ public static function link($url, $charset = false, array $option = NULL) { $common = \Yaf\Registry::get('common'); $htmls = $common->curl_request($url); $charset = 'utf-8'; if ($htmls) { preg_match('/=(.+\\b)/', $htmls->info['content_type'], $matchs_one); if (isset($matchs_one[1]) and $matchs_one[1]) { $charset = $matchs_one[1]; } else { if (preg_match('/=(.+\\b)/', self::getMetaEncoding($htmls->response), $matchs_two) and isset($matchs_two[1])) { $charset = $matchs_two[1]; } } $datas = new Readability($htmls->response, $charset); return $datas->getContent(); } return false; }
/** * 取得变量的存储文件名 * @access private * @param string $name 缓存变量名 * @return string */ private function filename($name) { $name = md5($name); $config = \Yaf\Registry::get("config")->cache; if ($config['subdir']) { // 使用子目录 $dir = ''; for ($i = 0; $i < $config['temp_level']; $i++) { $dir .= $name[$i] . '/'; } if (!is_dir($this->options['temp'] . $dir)) { @mkdir($this->options['temp'] . $dir, 0755, true); } $filename = $dir . $this->options['prefix'] . $name . '.php'; } else { $filename = $this->options['prefix'] . $name . '.php'; } return $this->options['temp'] . $filename; }