Beispiel #1
0
 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']);
 }
Beispiel #2
0
 public function init($yaf_app)
 {
     static $initialized = false;
     if (!$initialized) {
         $initialized = true;
         \Yaf\Loader::import(YK_CORE_ROOT . '/func.php');
         $config = $yaf_app->getConfig()->toArray();
         \Yaf\Registry::set('_config', $config);
         $level = $config['debug'] ? $config['debug'] == 1 ? E_ERROR : E_ALL : 0;
         define('MAIN_DEBUG', $config['debug']);
         if (MAIN_DEBUG) {
             ini_set('display_error', 'On');
         }
         error_reporting($level);
         define('TIMESTAMP', time());
         \Yaf\Registry::set('_clientip', isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'N/A');
         \Yaf\Registry::set('_clientport', isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : 0);
         \Yaf\Registry::set('_gzip', !isset($_SERVER['HTTP_ACCEPT_ENCODING']) || strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false || !function_exists('ob_gzhandler') ? false : true);
         set_timeoffset($config['timeoffset']);
         //重置视图
         $view = new view();
         \Yaf\Registry::set('_view', $view);
         $yaf_app->getDispatcher()->setView($view);
         define('CHARSET', 'utf-8');
         //end
     }
 }
Beispiel #3
0
 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;
     }
 }
Beispiel #4
0
 /**
  * 取得配置服务地址
  */
 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;
 }
Beispiel #5
0
 /**
  * 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();
 }
Beispiel #6
0
 /**
  * 路由结束之后触发       此时路由一定正确完成, 否则这个事件不会触发
  *
  * @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);
 }
Beispiel #7
0
 /**
  * @brief 把配置信息加载进入注册表
  * @param \Yaf\Dispatcher $dispatcher
  */
 public function _initConfig(\Yaf\Dispatcher $dispatcher)
 {
     $config = \Yaf\Application::app()->getConfig();
     foreach ($config as $section => $arrConf) {
         \Yaf\Registry::set($section, $arrConf);
     }
 }
Beispiel #8
0
 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;
 }
Beispiel #9
0
 public function init()
 {
     $this->_config = Registry::get("config");
     $this->_session = \Yaf\Session::getInstance();
     $this->_session->start();
     $this->_layout = Registry::get('layout');
 }
Beispiel #10
0
 /**
  * yaf路由分发之前触发,完成读取模块自定义配置
  *
  * @access public
  * @param \Yaf\Request_Abstract $request
  * @param \Yaf\Response_Abstract $response
  * @return void
  */
 public function preDispatch(Request_Abstract $request, Response_Abstract $response)
 {
     $module_dir = ROOT_PATH . DS . APP_NAME . DS . 'modules' . DS . $request->module . DS;
     do {
         if (!file_exists($module_dir . 'config' . DS . 'Import.php')) {
             break;
         }
         $module_config = (include $module_dir . 'config' . DS . 'Import.php');
         if (!is_array($module_config)) {
             break;
         }
         $mount = MountManager::getInstance();
         $config = array();
         foreach ($module_config as $name => $option) {
             if (!is_string($option) && !is_callable($option) && !is_object($option)) {
                 $config[$name] = $option;
             } else {
                 if (is_string($option)) {
                     $config[$name] = $option;
                 } else {
                     $mount->mount($name, $option);
                 }
             }
         }
         $simple_config = new Simple($config, true);
         Registry::set("config", $simple_config);
         Registry::set("mount", $mount);
     } while (0);
 }
Beispiel #11
0
 /**
  * ServiceApi初始化
  */
 public function init()
 {
     Dispatcher::getInstance()->returnResponse(true);
     Dispatcher::getInstance()->disableView();
     $this->_config = Registry::get('config');
     $this->_initRequestData();
 }
Beispiel #12
0
 /**
  * @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;
 }
Beispiel #13
0
 /**
  * 获取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!');
 }
Beispiel #14
0
 /**
  * 获取数据库配置项
  * @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!');
 }
Beispiel #15
0
 /**
  * 获取application
  * 
  * @return \Yaf\Application
  */
 public function getApplication()
 {
     $application = \Yaf\Registry::get('application');
     if (!$application) {
         $this->setApplication();
     }
     return \Yaf\Registry::get('application');
 }
Beispiel #16
0
 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());
     }
 }
Beispiel #17
0
 /**
  * 构造函数
  *
  */
 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;
 }
 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());
 }
 public static function getInstance()
 {
     if ($instance = \Yaf\Registry::get('ApplicationInit_Medoo')) {
         return $instance;
     }
     $instance = new self();
     \Yaf\Registry::set('ApplicationInit_Medoo', $instance);
     return $instance;
 }
Beispiel #20
0
 /**
  * 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;
 }
Beispiel #21
0
 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;
     }
 }
Beispiel #22
0
 /**
  * 路由结束之后触发       此时路由一定正确完成, 否则这个事件不会触发
  *
  * @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);
     }
 }
Beispiel #23
0
 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));
 }
Beispiel #24
0
 /**
  * 连接缓存
  * @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;
 }
Beispiel #25
0
 /**
  * 初始化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();
 }
Beispiel #26
0
 /**
  * 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;
 }
Beispiel #27
0
 /**
  * 获取数据库写实例
  */
 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;
 }
Beispiel #28
0
 /**
  * 获取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;
 }
Beispiel #29
0
 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);
 }
Beispiel #30
0
 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;
 }