/** * Set Dependency Injector with configuration variables * * * @param string $file full path to configuration file * @throws \Exception */ public function setConfig($file) { // static $config; $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('config', new \Phalcon\Config(require $file)); $config = $di->get('config'); $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . ucfirst($config->dbMaster->adapter); /** @var Pdo $connMaster */ $connMaster = new $adapter(["host" => $config->dbMaster->host, "port" => $config->dbMaster->port, "username" => $config->dbMaster->username, "password" => $config->dbMaster->password, "dbname" => $config->dbMaster->dbname, "prefix" => $config->dbMaster->prefix, 'options' => [\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES '" . $config->dbMaster->charset . "'", \PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_PERSISTENT => true]]); $di->set('dbMaster', $connMaster); $caches['cache'] = $config->cache->toArray(); if (!empty($config->cacheSlave)) { foreach ($config->cacheSlave as $cache) { $caches['cacheSlave'][] = $cache->toArray(); } } $adapter = ucfirst($config->cache->adapter); if ($adapter == "Redis") { $cacheAdapter = '\\Engine\\' . $adapter; } else { $cacheAdapter = '\\Phalcon\\Cache\\Backend\\' . $adapter; } $frontEndOptions = ['lifetime' => $config->cache->lifetime]; $cacheDataAdapter = new $cacheAdapter($frontEndOptions, $caches); $di->set('cacheData', $cacheDataAdapter, true); $di->setShared('transactions', function () { $manager = new \Phalcon\Mvc\Model\Transaction\Manager(); return $manager->setDbService("dbMaster"); }); $this->setDI($di); }
/** * Set Dependency Injector with configuration variables * * @throws Exception * @param string $file full path to configuration file */ public function setConfig($file) { if (!file_exists($file)) { throw new \Exception('Unable to load configuration file'); } $di = new \Phalcon\DI\FactoryDefault\CLI(); $di->set('config', new \Phalcon\Config(require $file)); $di->set('db', function () use($di) { $type = strtolower($di->get('config')->database->adapter); $creds = array('host' => $di->get('config')->database->host, 'username' => $di->get('config')->database->username, 'password' => $di->get('config')->database->password, 'dbname' => $di->get('config')->database->name); if ($type == 'mysql') { $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($creds); } else { if ($type == 'postgres') { $connection = new \Phalcon\Db\Adapter\Pdo\Postgesql($creds); } else { if ($type == 'sqlite') { $connection = new \Phalcon\Db\Adapter\Pdo\Sqlite($creds); } else { throw new Exception('Bad Database Adapter'); } } } $connection->setEventsManager(new \Events\Database\Profile()); return $connection; }); $this->setDI($di); }
*/ $loader = new \Phalcon\Loader(); $loader->registerDirs(array(ROOT . DS . 'library', ROOT . DS . 'models', ROOT . DS . 'plugins', APPLICATION_PATH . DS . 'Library', APPLICATION_PATH . DS . 'Plugins', APPLICATION_PATH . DS . 'Models', APPLICATION_PATH . DS . 'Tasks')); $loader->registerNamespaces(array('Lininliao\\Library' => ROOT . DS . 'library', 'Lininliao\\Models' => ROOT . DS . 'models', 'Lininliao\\Plugins' => ROOT . DS . 'plugins', 'Lininliao\\Backend\\Library' => APPLICATION_PATH . DS . 'Library', 'Lininliao\\Backend\\Plugins' => APPLICATION_PATH . DS . 'Plugins', 'Lininliao\\Backend\\Models' => APPLICATION_PATH . DS . 'Models'))->register(); $di = new \Phalcon\DI\FactoryDefault\CLI(); $config = new \Phalcon\Config\Adapter\Ini(ROOT . DS . 'configs' . DS . 'config.ini'); $di->set('config', $config, true); /** * Database connection is created based in the parameters defined in the configuration file */ $di->set('db', function () use($config) { $db_config = $config->database[ENVIRONMENT]; return new \Phalcon\Db\Adapter\Pdo\Mysql(array('host' => $db_config->dbhost, 'username' => $db_config->dbusername, 'password' => $db_config->dbpassword, 'dbname' => $db_config->dbname)); }, true); $di->set('acl', function () use($di, $config) { return new Phalcon\Acl\Adapter\Database(array('db' => $di->get('db'), 'roles' => 'roles', 'rolesInherits' => 'roles_inherits', 'resources' => 'resources', 'resourcesAccesses' => 'resources_accesses', 'accessList' => 'access_list')); }, true); $di->set('translate', function () { return new Lininliao\Library\Locale\Translate(SITENAME); }, true); $di->set('simpleView', function () use($di, $config) { $view = new \Phalcon\Mvc\View\Simple(); $view->setDI($di); $view->registerEngines(array('.volt' => function ($view, $di) { $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); $compiledPath = ROOT . DS . 'cache' . DS . SITENAME . DS . 'volt' . DS; if (!file_exists($compiledPath)) { mkdir($compiledPath, 0777, true); } $volt->setOptions(array('compiledPath' => $compiledPath, 'compiledExtension' => '.cache', 'compiledSeparator' => '%', 'stat' => true, 'compileAlways' => ENVIRONMENT === 'production' ? false : true)); $compiler = $volt->getCompiler();
continue; } $keyWrite = 'db' . ucfirst($dbKey); $keyRead = $keyWrite . '_read'; $dbWriteConfig = $currentConfig['write']['db']; $di->set($keyWrite, function () use($dbWriteConfig) { return new Phalcon\Db\Adapter\Pdo\Mysql($dbWriteConfig); }); $dbReadConfig = current($currentConfig['reads']); $di->set($keyRead, function () use($dbReadConfig) { return new Phalcon\Db\Adapter\Pdo\Mysql($dbReadConfig); }); } //设置默认数据库连接 $defaultDbKey = 'db' . ucfirst($config->balanceDb->default); $di->set('db', $di->get($defaultDbKey)); //Redis负载均衡 BalanceRedis::config($config->balanceRedis->toArray()); //URL 工具类 TUrl::config($config->url->toArray()); $console = new Phalcon\CLI\Console(); $console->setDI($di); //注册模块 $modules = array(); foreach ($config->modules as $key => $params) { if ($key == 'default') { continue; } $modules[$key] = array('className' => sprintf('Module\\%s\\Module', ucfirst($key)), 'path' => $params['path']); } $console->registerModules($modules);