/** * * 获取缓存信息 * @return \Doctrine\Common\Cache\MongoDBCache */ protected function getCache() { $namespace = 'PanGuKTD_ORM_'; $cacheConfig = config('doctrine'); $cacheName = $cacheConfig['name']; $cache = null; if ($cacheName == 'array') { $cache = new \Doctrine\Common\Cache\ArrayCache(); } elseif ($cacheName == 'xcache') { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif ($cacheName == 'memcached') { $memcached = new \Memcached(); $memcached->addServers($cacheConfig['memcached']); $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcached($memcached); $cache->setNamespace($namespace); } elseif ($cacheName == 'memcache') { $memcache = new \Memcache(); foreach ($cacheConfig['memcache'] as $key => $value) { $memcache->addServer($value['host'], $value['port'], $value['persistent'], $value['weight']); } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); $cache->setNamespace($namespace); } elseif ($cacheName == 'apc') { $cache = new \Doctrine\Common\Cache\ApcCache(); $cache->setNamespace($namespace); } elseif ($cacheName == 'mongo') { $host = $cacheConfig['mongo']['host']; $port = $cacheConfig['mongo']['port']; $opt = $cacheConfig['mongo']['options']; $mongo = new \MongoClient("mongodb://{$host}:{$port}", $opt); $mongo = new \MongoDB($mongo, 'doctrine_orm_cache'); $conn = new \MongoCollection($mongo, $namespace); $cache = new \Doctrine\Common\Cache\MongoDBCache($conn); $cache->setNamespace($namespace); } elseif ($cacheName == 'redis') { $host = $cacheConfig['redis']['host']; $port = $cacheConfig['redis']['port']; $redis = new \Redis(); $redis->connect($host, $port); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } return $cache; }
/** * Detects the correct doctrine cache driver for the user caching engine in use * @return \Doctrine\Common\Cache\AbstractCache The doctrine cache driver object */ public function getDoctrineCacheDriver() { if ($this->doctrineCacheEngine) { // return cache engine if already set return $this->doctrineCacheEngine; } $userCacheEngine = $this->getUserCacheEngine(); // check if user caching is active if (!$this->getUserCacheActive()) { $userCacheEngine = \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_OFF; } switch ($userCacheEngine) { case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_APC: $cache = new \Doctrine\Common\Cache\ApcCache(); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_MEMCACHE: $memcache = $this->getMemcache(); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_MEMCACHED: $memcached = $this->getMemcached(); $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcached($memcached); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_XCACHE: $cache = new \Doctrine\Common\Cache\XcacheCache(); $cache->setNamespace($this->getCachePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_FILESYSTEM: $cache = new \Cx\Core_Modules\Cache\Controller\Doctrine\CacheDriver\FileSystemCache($this->strCachePath); break; default: $cache = new \Doctrine\Common\Cache\ArrayCache(); break; } // set the doctrine cache engine to avoid getting it a second time $this->doctrineCacheEngine = $cache; return $cache; }
/** * Returns the doctrine entity manager * @return \Doctrine\ORM\EntityManager */ public function getEntityManager() { if ($this->em) { return $this->em; } global $objCache; $config = new \Doctrine\ORM\Configuration(); $userCacheEngine = $objCache->getUserCacheEngine(); if (!$objCache->getUserCacheActive()) { $userCacheEngine = \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_OFF; } $arrayCache = new \Doctrine\Common\Cache\ArrayCache(); switch ($userCacheEngine) { case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_APC: $cache = new \Doctrine\Common\Cache\ApcCache(); $cache->setNamespace($this->db->getName() . '.' . $this->db->getTablePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_MEMCACHE: $memcache = $objCache->getMemcache(); if ($memcache instanceof \Memcache) { $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif ($memcache instanceof \Memcached) { $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcache($memcache); } $cache->setNamespace($this->db->getName() . '.' . $this->db->getTablePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_XCACHE: $cache = new \Doctrine\Common\Cache\XcacheCache(); $cache->setNamespace($this->db->getName() . '.' . $this->db->getTablePrefix()); break; case \Cx\Core_Modules\Cache\Controller\Cache::CACHE_ENGINE_FILESYSTEM: $cache = new \Cx\Core_Modules\Cache\Controller\Doctrine\CacheDriver\FileSystemCache(ASCMS_CACHE_PATH); break; default: $cache = $arrayCache; break; } \Env::set('cache', $cache); //$config->setResultCacheImpl($cache); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setProxyDir(ASCMS_MODEL_PROXIES_PATH); $config->setProxyNamespace('Cx\\Model\\Proxies'); /** * This should be set to true if workbench is present and active. * Just checking for workbench.config is not really a good solution. * Since ConfigurationFactory used by EM caches auto generation * config value, there's no possibility to set this later. */ $config->setAutoGenerateProxyClasses(file_exists(ASCMS_DOCUMENT_ROOT . '/workbench.config')); $connectionOptions = array('pdo' => $this->getPdoConnection(), 'dbname' => $this->db->getName()); $evm = new \Doctrine\Common\EventManager(); $chainDriverImpl = new \Doctrine\ORM\Mapping\Driver\DriverChain(); $driverImpl = new \Cx\Core\Model\Controller\YamlDriver(array(ASCMS_CORE_PATH . '/Core' . '/Model/Yaml')); $chainDriverImpl->addDriver($driverImpl, 'Cx'); //loggable stuff $loggableDriverImpl = $config->newDefaultAnnotationDriver(ASCMS_LIBRARY_PATH . '/doctrine/Gedmo/Loggable/Entity'); $chainDriverImpl->addDriver($loggableDriverImpl, 'Gedmo\\Loggable'); $this->loggableListener = new \Cx\Core\Model\Model\Event\LoggableListener(); $this->loggableListener->setUsername('currently_loggedin_user'); // in real world app the username should be loaded from session, example: // Session::getInstance()->read('user')->getUsername(); $evm->addEventSubscriber($this->loggableListener); //tree stuff $treeListener = new \Gedmo\Tree\TreeListener(); $evm->addEventSubscriber($treeListener); $config->setMetadataDriverImpl($chainDriverImpl); //table prefix $prefixListener = new \DoctrineExtension\TablePrefixListener($this->db->getTablePrefix()); $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $prefixListener); $config->setSqlLogger(new \Cx\Lib\DBG\DoctrineSQLLogger()); $em = \Cx\Core\Model\Controller\EntityManager::create($connectionOptions, $config, $evm); //resolve enum, set errors $conn = $em->getConnection(); $conn->setCharset($this->db->getCharset()); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string'); $this->em = $em; return $this->em; }