Exemplo n.º 1
0
});
/**
 * Start the session the first time some component request the session service
 */
$di->set('session', function () use($di) {
    $sessionAdapter = $di->get('config')->application->session->adapter;
    $session = new $sessionAdapter($di->get('config')->application->session->options->toArray());
    $session->start();
    return $session;
}, true);
/**
 * This service controls the initialization of models, keeping record of relations
 * between the different models of the application.
 */
$di->setShared('collectionManager', function () use($eventsManager) {
    $collectionManager = new CollectionManager();
    $collectionManager->setEventsManager($eventsManager);
    return $collectionManager;
});
$di->setShared('modelsManager', function () use($eventsManager) {
    $modelsManager = new ModelsManager();
    $modelsManager->setEventsManager($eventsManager);
    return $modelsManager;
});
// Set the views cache service
$di->set('viewCache', function () use($di) {
    $config = $di->get('config');
    if ($config->application->debug) {
        return new MemoryBackend(new FrontendNone());
    } else {
        // Cache data for one day by default
Exemplo n.º 2
0
 protected function _registerServices()
 {
     $config = (include $this->appConfigDirectory . "config.php");
     if (isset($config['mode'])) {
         self::$mode = $config->mode;
     }
     $this->di['config'] = $config;
     $this->di['dispatcher'] = function () {
         $dispatcher = new Dispatcher();
         //            $this->eventManager->attach('dispatch', new HttpResponsePlugin());
         //
         //            $dispatcher->setEventsManager($this->eventManager);
         //注册默认controller目录
         $dispatcher->setDefaultNamespace('myweb\\twoweb\\Controllers\\home');
         return $dispatcher;
     };
     /**
      * 数据库设定, 自动读取配置中的所有数据库
      */
     call_user_func(function () {
         $logger = $this->getLogger('mysql');
         $configs = (include $this->globalConfigDirectory . "mysql.php");
         $configs = $configs[self::$mode];
         foreach ($configs as $name => $_config) {
             $this->di['db.' . "{$name}"] = function () use($logger, $name, $_config) {
                 $this->eventManager->attach('db', function ($event, $connection) use($logger, $name) {
                     if ($event->getType() == 'beforeQuery') {
                         // $logger->log("[$name]: " .$connection->getSQLStatement(), \Phalcon\Logger::INFO);
                     }
                 });
                 $mysql = new MysqlDB($_config);
                 $mysql->setEventsManager($this->eventManager);
                 return $mysql;
             };
         }
     });
     /**
      * mongodb
      */
     call_user_func(function () {
         $configs = (include $this->globalConfigDirectory . "mongo.php");
         $configs = $configs[self::$mode];
         $logger = $this->getLogger('mongo');
         foreach ($configs as $name => $_config) {
             $this->di['mongo' . ".{$name}"] = function () use($name, $_config) {
                 $auth = "";
                 if (!empty($_config['username']) && !empty($_config['password'])) {
                     $auth = $_config['username'] . ":" . $_config['password'] . "@";
                 }
                 $connection = new MongoDB("mongodb://{$auth}{$_config['host']}:{$_config['port']}/{$_config['dbname']}");
                 return $connection->selectDB($_config['dbname']);
             };
         }
     });
     //        collection log
     $this->di['collectionManager'] = function () {
         $modelsManager = new CollectionManager();
         $modelsManager->setEventsManager($this->eventManager);
         return $modelsManager;
     };
     $this->di->set('voltService', function ($view, $di) {
         $volt = new Volt($view, $di);
         $volt->setOptions(array('compiledPath' => APP_CACHE_PATH . "caches_template" . DIRECTORY_SEPARATOR, 'compiledExtension' => '.compiled', 'compileAlways' => false));
         $compiler = $volt->getCompiler();
         $compiler->addFilter('array_slice', 'array_slice');
         return $volt;
     });
     $this->di->set('view', function () {
         $view = new View();
         $view->setViewsDir(APP_PATH . "views");
         $view->registerEngines(array(".html" => 'voltService'));
         return $view;
     }, true);
     $this->di['router'] = function () {
         $router = (include $this->appConfigDirectory . "router.php");
         return $router;
     };
     $this->di['logger'] = function () {
         $logger = $this->getLogger("debug");
         return $logger;
     };
     $this->di['mongolog'] = function () {
         $logger = $this->getLogger("mongo");
         return $logger;
     };
     /**
      * redis 设定
      */
     call_user_func(function () {
         $configs = (include $this->globalConfigDirectory . "redis.php");
         $configs = $configs[self::$mode];
         foreach ($configs as $name => $_config) {
             $this->di->set('redis.' . $name, function () use($_config) {
                 return $this->connectRedis($_config);
             });
         }
     });
     /**
      * 缓存设定
      */
     $this->di['cache'] = function () {
         //1 days
         $frontCache = new FrontCache(array('lifetime' => 86400));
         return new FileCache($frontCache, array('cacheDir' => APP_CACHE_PATH . "caches_data" . DIRECTORY_SEPARATOR));
     };
     /**
      * Model Cache
      */
     $this->di['modelsCache'] = function () {
         return $this->di['cache'];
     };
     $this->di['modelsMetadata'] = function () {
         //1 days
         $metaDataCache = new FileMetaData(array('lifetime' => 86400, 'metaDataDir' => APP_CACHE_PATH . "caches_metadata" . DIRECTORY_SEPARATOR));
         return $metaDataCache;
     };
 }