public function _initDefaultDbAdapter() { //新建对象 $dbAdapter = new Zend\Db\Adapter\Adapter(Yaf_Application::app()->getConfig()->mysql->write->toArray()); //设为全局变量 Yaf_Registry::set("db", $dbAdapter); }
public function _init(Yaf_Dispatcher $dispatcher) { // auto start session Yaf_Session::getInstance()->start(); // auto load config data $this->config = Yaf_Application::app()->getConfig(); Yaf_Registry::set('Config', $this->config); //auto load redis $redis = new Redis(); $redis->connect($this->config->redis->host, $this->config->redis->port, $this->config->redis->timeout, $this->config->redis->reserved, $this->config->redis->interval); Yaf_Registry::set('Redis', $redis); //auto load mysql Yaf_Registry::set('DbRead', new Db('mysql:host=' . $this->config->mysql->read->host . ';dbname=' . $this->config->mysql->read->dbname . ';charset=' . $this->config->mysql->read->charset . ';port=' . $this->config->mysql->read->port . '', $this->config->mysql->read->username, $this->config->mysql->read->password)); Yaf_Registry::set('DbWrite', new Db('mysql:host=' . $this->config->mysql->write->host . ';dbname=' . $this->config->mysql->write->dbname . ';charset=' . $this->config->mysql->write->charset . ';port=' . $this->config->mysql->write->port . '', $this->config->mysql->write->username, $this->config->mysql->write->password)); // auto load model Yaf_Registry::set('I18n', new I18nModel($redis, $this->config->application->name, 'cn')); Yaf_Registry::set('Cache', new CacheModel($redis, $this->config->application->name)); // auto load plugin $dispatcher->registerPlugin(new GlobalPlugin()); // auto save request $request = $dispatcher->getRequest(); // auto set ajax is no render if ($request->isXmlHttpRequest()) { $dispatcher->autoRender(false); } // auto set http protocol to action except http get protocol if (!$request->isGet()) { $dispatcher->setDefaultAction($request->getMethod()); } }
protected function loadDatabase($name) { $database = Yaf_Registry::get('_database'); if (isset($database[$name])) { return $database[$name]; } $config = Yaf_Registry::get('_config'); if (isset($config->database->{$name})) { if (is_null($database)) { $database = array(); } $dbConfig = $config->database->{$name}; $pdoParams = array(); if ($dbConfig->pconnect) { $pdoParams[Db::ATTR_PERSISTENT] = TRUE; } try { $conn = new Db($dbConfig->type . ':host=' . $dbConfig->host . '; dbname=' . $dbConfig->database, $dbConfig->username, $dbConfig->password, $pdoParams); } catch (PDOException $error) { throw new Exception($error->getMessage(), 500); } $database[$name] = $conn; Yaf_Registry::set('_database', $database); return $conn; } else { throw new Exception('Undefined database', 500); } }
/** * Controller的init方法会被自动首先调用 */ public function init() { //调试mysql if (isset($_GET['debug6429360'])) { $this->debug = true; DB_Mysqli::$DEBUG = 1; } $this->platform = isset($_REQUEST['platform']) ? $_REQUEST['platform'] : $this->platform; Yaf_Registry::set("platform", $this->platform); $this->isMobile = $this->platform == 'ios' || $this->platform == 'android'; $this->wap = !$this->isMobile && Common_Mobile::isMobile(); $this->uid = $this->get('uid', $this->post('uid', false)); $this->controllerName = $this->getRequest()->getControllerName(); $this->actionName = $this->getRequest()->getActionName(); $userModel = new UserModel(); /* if (!isset(Common_Config::$NotNeedLogin[$this->controllerName][$this->actionName]) && $this->isMobile && $this->needLogin && !$this->debug) { $token = $this->get("token", $this->post("token", false)); if (!$token) { $this->redirect(NULL, Common_Error::ERROR_TOKEN_NOT_EXISTS); } $api = $this->getRequestApi(); $check = Common_Token::check($token, $this->uid, strtolower($api)); if (!$check) { $this->redirect(NULL, Common_Error::ERROR_TOKEN); } }*/ $this->uid = $this->uid ? $this->uid : $userModel->getUid(); /** * 如果是Ajax请求, 则关闭HTML输出 */ if ($this->getRequest()->isXmlHttpRequest() || 'POST' == $this->getRequest()->getMethod()) { $this->ajax = true; Yaf_Dispatcher::getInstance()->disableView(); } else { //$path = $this->getView()->getScriptPath(); //$path[0] = $path[0] . "/" . strtolower($this->getRequest()->getControllerName()); //$this->getView()->setScriptPath($path[0]); } //print_r($this->uid?$this->uid:"null"); //验证登录 /*if ( !isset(Common_Config::$NotNeedLogin[$this->controllerName][$this->actionName]) && $this->needLogin && !$this->uid) { $this->redirect("/user/login?from=" . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ""), Common_Error::ERROR_USER_NOT_LOGIN); $this->redirect("/user/verify", Common_Error::ERROR_USER_NOT_LOGIN); }*/ //用户信息 /* if($this->uid) { $user = $userModel->getUser($this->uid); $this->assign("uInfo", $user); Yaf_Registry::set("uid", $this->uid); }*/ }
public function _initDatabase() { $servers = array(); $database = $this->config->database; $servers[] = $database->master->toArray(); $slaves = $database->slaves; if (!empty($slaves)) { $slave_servers = explode('|', $slaves->servers); $slave_users = explode('|', $slaves->users); $slave_passwords = explode('|', $slaves->passwords); $slave_databases = explode('|', $slaves->databases); $slaves = array(); foreach ($slave_servers as $key => $slave_server) { if (isset($slave_users[$key]) && isset($slave_passwords[$key]) && isset($slave_databases[$key])) { $slaves[] = array('server' => $slave_server, 'user' => $slave_users[$key], 'password' => $slave_passwords[$key], 'database' => $slave_databases[$key]); } } $servers[] = $slaves[array_rand($slaves)]; } Yaf_Registry::set('database', $servers); if (isset($database->mysql_cache_enable) && $database->mysql_cache_enable && !defined('MYSQL_CACHE_ENABLE')) { define('MYSQL_CACHE_ENABLE', true); } if (isset($database->mysql_log_error) && $database->mysql_log_error && !defined('MYSQL_LOG_ERROR')) { define('MYSQL_LOG_ERROR', true); } Yaf_Loader::import(APPLICATION_PATH . '/library/Db/Db.php'); Yaf_Loader::import(APPLICATION_PATH . '/library/Db/DbQuery.php'); }
public static function init() { $_ENV['APP_NAME'] = pathinfo(realpath(APPLICATION_PATH), PATHINFO_BASENAME); $configurePath = sprintf('%s/../config/config.ini', APPLICATION_PATH); $config = new Yaf_Config_Ini($configurePath, 'production'); Yaf_Registry::set('dbconfig', $config); self::loadEssentials(); }
public function _initZendDb(Yaf_Dispatcher $dispatcher) { $arrConfig = Yaf_Application::app()->getConfig(); $zendDb = new Zend_Db(); $params = array('host' => $arrConfig->mysql->read->host, 'username' => $arrConfig->mysql->read->username, 'password' => $arrConfig->mysql->read->password, 'dbname' => $arrConfig->mysql->read->dbname, 'port' => $arrConfig->mysql->read->port); $db = $zendDb->factory('PDO_MYSQL', $params); Yaf_Registry::set('db', $db); }
public function _initPlugin(Yaf_Dispatcher $dispatcher) { $router = new RouterPlugin(); $dispatcher->registerPlugin($router); $admin = new AdminPlugin(); $dispatcher->registerPlugin($admin); Yaf_Registry::set('adminPlugin', $admin); }
public function _initConfig() { Yaf_Dispatcher::getInstance()->autoRender(FALSE); // 关闭自动加载模板 //把配置保存起来 $arrConfig = Yaf_Application::app()->getConfig(); Yaf_Registry::set('config', $arrConfig); }
/** * Init Redis */ public function _initRedis(Yaf_Dispatcher $dispatcher) { $config = Yaf_Application::app()->getConfig(); $redis = new Redis(); $redis->connect($config['redis_host'], $config['redis_port']); Yaf_Registry::set('redis', $redis); // TODO sms Redis queue //Yaf_Registry::set('redis_queue_sms', $config['redis_queue_sms']); }
public function _initConfig(Yaf_Dispatcher $dispatcher) { $config = Yaf_Application::app()->getConfig(); Yaf_Registry::set("config", $config); $sysConfig = (include APP_PATH . "/conf/config.php"); Yaf_Registry::set("sysConfig", $sysConfig); $dispatcher->autoRender(FALSE); //全局关闭渲染 }
public function __construct() { $this->__application = Yaf_Registry::get('Application'); if ($this->__application == null) { $this->__application = new Yaf_Application(ROOT_PATH . "/conf/application.ini"); Yaf_Registry::set('Application', $this->__application); Yaf_Registry::set('config', $this->__application->getConfig()); } }
public function _initView(Yaf_Dispatcher $dispatcher) { //在这里注册自己的view控制器,例如smarty,firekylin $smarty = new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty")); Yaf_Registry::set("smarty", $smarty); $dispatcher->setView($smarty); //Yaf_Dispatcher::getInstance()->disableView(); //var_dump('sssss'); }
/** * 初始化SESSION * * @param Yaf_Dispatcher $dispatcher */ public function _initSession(Yaf_Dispatcher $dispatcher) { //开启SESSION session_name('GITHUBLOG_SID'); session_start(); //获取用户UID $uid = \Comm\Arg::session('uid', FILTER_VALIDATE_INT); Yaf_Registry::set('current_uid', $uid); }
private function getAPPConfig() { $key = __METHOD__; $config = Yaf_Registry::get($key); if ($config == null) { $config = Yaf_Application::app() == null ? new Yaf_Application(APPLICATION_CONFIG_PATH . '/application.ini') : Yaf_Application::app(); Yaf_Registry::set($key, $config); } return $config; }
public function _initLogger() { require_once APPLICATION_PATH . "/application/library/Log/Logger.php"; Logger::configure(APPLICATION_PATH . "/conf/logconf.php"); $logger = Logger::getRootLogger(); Yaf_Registry::set('logger', $logger); // Logger::configure(APPLICATION_PATH . "/conf/statlogconf.php"); // $statLogger = Logger::getRootLogger(); // Yaf_Registry::set('statLogger', $statLogger); }
public function _initDatabase() { $db_config['hostname'] = $this->arrConfig->db->hostname; $db_config['username'] = $this->arrConfig->db->username; $db_config['password'] = $this->arrConfig->db->password; $db_config['database'] = $this->arrConfig->db->database; // $db_config['log'] = $this->arrConfig->db->log; header("Content-type: text/html; charset=utf-8"); Yaf_Registry::set('db', new Db($db_config)); }
/** *@desc 获取数据操作层 *@param $name 模块名(服务名) .数据操作类名 *@return DAO Object */ protected function getDao($name) { if (Yaf_Registry::has('dao_' . $name)) { return Yaf_Registry::get('dao_' . $name); } $servicePathInfo = explode('.', $name); $serviceName = 'Service_' . $servicePathInfo['0'] . '_Dao_Impl_' . $servicePathInfo[1] . 'Impl'; $serviceInstance = new $serviceName(); Yaf_Registry::set('dao_' . $name, $serviceInstance); return $serviceInstance; }
public function _initDatabase(Yaf_Dispatcher $dispatcher) { //var_dump(__METHOD__); $dbconfigArray = Yaf_Application::app()->getConfig()->database->toArray(); $host = $dbconfigArray['host']; $dbname = $dbconfigArray['dbname']; $username = $dbconfigArray['username']; $password = $dbconfigArray['password']; $dsn = "mysql:host={$host};dbname={$dbname};charset=utf8"; $db = new PDO($dsn, $username, $password); Yaf_Registry::set('db', $db); }
public function _initLayout(Yaf_Dispatcher $dispatcher) { /*layout allows boilerplate HTML to live in /views/layout rather than every script*/ $layout = new LayoutPlugin('layout.phtml'); /* Store a reference in the registry so values can be set later. * This is a hack to make up for the lack of a getPlugin * method in the dispatcher. */ Yaf_Registry::set('layout', $layout); /*add the plugin to the dispatcher*/ $dispatcher->registerPlugin($layout); }
public function testCase004() { $str = "Ageli Platform"; Yaf_Registry::set("name", $str); unset($str); $this->assertEquals('Ageli Platform', Yaf_Registry::get("name")); $this->assertTrue(Yaf_Registry::has("name")); $name = "name"; Yaf_Registry::del($name); $this->assertNull(Yaf_Registry::get("name")); $this->assertFalse(Yaf_Registry::has("name")); }
public function _initConfig() { $this->config = Yaf_Application::app()->getConfig(); //把配置保存起来 $monitor = Ym_Config::getAppItem('monitor'); $actions = Ym_Config::getAppItem('actions'); $alarm = Ym_Config::getAppItem('alarm'); Yaf_Registry::set('monitor_config', $monitor); Yaf_Registry::set('actions_config', $actions); Yaf_Registry::set('alarm_config', $alarm); Yaf_Registry::set('common_config', Ym_Config::getAppItem('common')); }
public function _initMemcached(Yaf_Dispatcher $dispatcher) { //memcached 扩展 $mc_server = $this->_config->memcached; if ($mc_server['isopen'] != 0) { $this->_mc = new memcached(); $this->_mc->addServer($mc_server['host'], $mc_server['port']); Yaf_Registry::set('_mc', $this->_mc); } //eg. //$this->_mc->set("y","yangzhi"); //echo $this->_mc->get("y"); }
/** * 采用布局 * @param Yaf_Dispatcher $dispatcher */ function _initLayout(Yaf_Dispatcher $dispatcher) { define('REDIRECT_URL', empty($_SERVER['REQUEST_URI']) ? '/' : strtolower($_SERVER['REQUEST_URI'])); # 用户后台 if (false !== strpos(REDIRECT_URL, '/user_emailverify')) { return; } if (false !== strpos(REDIRECT_URL, '/user_') || false !== strpos(REDIRECT_URL, '/huodong_') || false !== strpos(REDIRECT_URL, '/loan_')) { $layout = new LayoutPlugin('user/tpl.layout.phtml'); Yaf_Registry::set('layout', $layout); $dispatcher->registerPlugin($layout); } }
public function _initConfig() { // 把配置保存起来 $arrConfig = Yaf_Application::app()->getConfig(); Yaf_Registry::set('default_config', $arrConfig); $file_consts = Yaf_Application::app()->getConfig()->application->directory . '/../conf/consts.php'; if (file_exists($file_consts)) { require_once $file_consts; } if (file_exists(FRAMEWORK_PATH . '/conf/public.php')) { $this->public_config = (require_once FRAMEWORK_PATH . '/conf/public.php'); Yaf_Registry::set('public_config', $this->public_config); } }
/** * 初始化组件 */ public function _initComponent(Yaf_Dispatcher $dispatcher) { $components = $this->__config->component->toArray(); foreach ($components as $_k => $_v) { if (isset($_v['class'])) { $component = new $_v['class'](); //$component = $_v['class']::getInstance(); if (isset($_v['params']) && is_array($_v['params'])) { foreach ($_v['params'] as $__k => $__v) { $component->{$__k} = $__v; } } if (method_exists($component, 'init')) { $component->init(); } Yaf_Registry::set($_k, $component); } } }
/** * db config */ public function _initDb() { //user_db $userDb = $this->_config->database->user; $userMaster = $userDb->master->toArray(); $userSlave = !empty($userDb->slave) ? $userDb->slave->toArray() : null; $userDb = new TZ_Db($userMaster, $userSlave, $userDb->driver); Yaf_Registry::set('user_db', $userDb); $virtualDb = $this->_config->database->virtual; $virtualMaster = $virtualDb->master->toArray(); $virtualSlave = !empty($virtualDb->slave) ? $virtualDb->slave->toArray() : null; $virtualDb = new TZ_Db($virtualMaster, $virtualSlave, $virtualDb->driver); Yaf_Registry::set('virtual_db', $virtualDb); $deviceDb = $this->_config->database->device; $deviceMaster = $deviceDb->master->toArray(); $deviceSlave = !empty($deviceDb->slave) ? $deviceDb->slave->toArray() : null; $deviceDb = new TZ_Db($deviceMaster, $deviceSlave, $deviceDb->driver); Yaf_Registry::set('device_db', $deviceDb); }
public function indexAction() { $theme = Comm\Arg::get('theme', FILTER_VALIDATE_INT, ['min_range' => 0], true); $resource = Comm\Arg::get('resource', FILTER_DEFAULT, null, true); Yaf_Registry::set('tpl_id', $theme); switch ($resource) { //预览首页 case 'home': $pager = new Comm\Pager(1000, 20, 1); $articles = Model\Article::showUserList($pager); Model\Publish::home($articles, $pager, null, false); break; //预览分类下的文章列表 //预览分类下的文章列表 case 'article-list': $pager = new Comm\Pager(1000, 20, 1); $articles = Model\Article::showUserList(new Comm\Pager(1, 1, 1)); if (!$articles) { throw new Exception\Msg(_('至少发表一篇文章才可预览')); } $article = reset($articles['result']); $category = Model\Category::show($article['category_id']); $articles = Model\Article::showUserList($pager, false, $category['id']); $articles = isset($articles['result']) ? $articles['result'] : array(); Model\Publish::categoryArticleList($category, $articles, $pager, null, false); break; //预览文章 //预览文章 case 'article': $articles = Model\Article::showUserList(new Comm\Pager(1, 1, 1)); $article = reset($articles['result']); Model\Publish::article($article, false); break; //预览导航 //预览导航 case 'sidebar': Model\Publish::sidebar(false, false); break; default: throw new \Exception\Msg('本资源不支持预览'); } }
public function loadModule($name, $key = 'master') { // module 不存在 if (!array_key_exists($name, self::$modules)) { throw new \ImReworks\Exception\ModuleNotFound("module [{$module}] not found."); } // module 需要有配置 if (self::$modules[$name]['cfg']) { // 配置存在 if (isset($this->config[$name][YAF_ENVIRON][$key])) { $config = $this->config[$name][YAF_ENVIRON][$key]; \Yaf_Registry::set($name, new self::$modules[$name]['cls']($config)); } else { throw new \ImReworks\Exception\ConfigNotFound("{$name}->" . YAF_ENVIRON . " is not found."); } } else { \Yaf_Registry::set($name, new self::$modules[$name]()); } return \Yaf_Registry::get($name); }
/** * load module file * * @param string $directory * @param string $modelName * @param string $moduleName * @return object */ private static function _loadModuleFile($directory, $fileName, $moduleName = null) { if (null === self::$_moduleName) { self::$_moduleName = Yaf_Dispatcher::getInstance()->getRequest()->getModuleName(); } if (null !== $moduleName) { self::$_moduleName = $moduleName; } $file = APP_PATH . '/application/modules/' . self::$_moduleName . '/' . $directory . '/' . $fileName . '.php'; $regObject = Yaf_Registry::get($file); if (false !== $regObject && null !== $regObject) { return $regObject; } $loadResult = Yaf_Loader::import($file); $fileName .= $directory == 'models' ? 'Model' : 'Service'; if ($loadResult) { $fileObject = new $fileName(); Yaf_Registry::set($file, $fileObject); return $fileObject; } else { throw new Exception($fileName . ' not found.'); } }