/** * Gets the 'doctrine.orm.default_second_level_cache.cache_configuration' service. * * This service is shared. * This method always returns the same instance of the service. * * @return \Doctrine\ORM\Cache\CacheConfiguration A Doctrine\ORM\Cache\CacheConfiguration instance. */ protected function getDoctrine_Orm_DefaultSecondLevelCache_CacheConfigurationService() { $a = new \Doctrine\ORM\Cache\Logging\CacheLoggerChain(); $a->setLogger('statistics', new \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger()); $this->services['doctrine.orm.default_second_level_cache.cache_configuration'] = $instance = new \Doctrine\ORM\Cache\CacheConfiguration(); $instance->setCacheLogger($a); $instance->setCacheFactory(new \Doctrine\ORM\Cache\DefaultCacheFactory($this->get('doctrine.orm.default_second_level_cache.regions_configuration'), $this->get('doctrine_cache.providers.doctrine.orm.default_second_level_cache.region_cache_driver'))); $instance->setRegionsConfiguration(new \Doctrine\ORM\Cache\RegionsConfiguration()); return $instance; }
$loader = (require $loaderPath); // Set up class loading. $loader->add('Entities', __DIR__); $loader->add('Proxies', __DIR__); $debug = true; $config = new \Doctrine\ORM\Configuration(); // Set up Metadata Drivers $driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__ . "/Entities")); $config->setMetadataDriverImpl($driverImpl); // Set up caches, depending on $debug variable. // You can use another variable to define which one of the cache systems you gonna use. $cache = $debug ? new Cache\ArrayCache() : new Cache\ApcCache(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); // Proxy configuration $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Proxies'); // Database connection information $connectionOptions = array('driver' => 'pdo_sqlite', 'path' => 'database.sqlite'); // Enable second-level cache $cacheConfig = new \Doctrine\ORM\Cache\CacheConfiguration(); $cacheDriver = $debug ? new Cache\ArrayCache() : new Cache\ApcCache(); $cacheLogger = new \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger(); $factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cacheDriver); if ($debug) { $cacheConfig->setCacheLogger($cacheLogger); } $cacheConfig->setCacheFactory($factory); $config->setSecondLevelCacheEnabled(true); $config->setSecondLevelCacheConfiguration($cacheConfig); return EntityManager::create($connectionOptions, $config);
/** * Gets an EntityManager for testing purposes. * * @param \Doctrine\ORM\Configuration $config The Configuration to pass to the EntityManager. * @param \Doctrine\Common\EventManager $eventManager The EventManager to pass to the EntityManager. * * @return \Doctrine\ORM\EntityManager */ protected function _getEntityManager($config = null, $eventManager = null) { // NOTE: Functional tests use their own shared metadata cache, because // the actual database platform used during execution has effect on some // metadata mapping behaviors (like the choice of the ID generation). if (is_null(self::$_metadataCacheImpl)) { if (isset($GLOBALS['DOCTRINE_CACHE_IMPL'])) { self::$_metadataCacheImpl = new $GLOBALS['DOCTRINE_CACHE_IMPL'](); } else { self::$_metadataCacheImpl = new \Doctrine\Common\Cache\ArrayCache(); } } if (is_null(self::$_queryCacheImpl)) { self::$_queryCacheImpl = new \Doctrine\Common\Cache\ArrayCache(); } $this->_sqlLoggerStack = new \Doctrine\DBAL\Logging\DebugStack(); $this->_sqlLoggerStack->enabled = false; //FIXME: two different configs! $conn and the created entity manager have // different configs. $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl(self::$_metadataCacheImpl); $config->setQueryCacheImpl(self::$_queryCacheImpl); $config->setProxyDir(__DIR__ . '/Proxies'); $config->setProxyNamespace('Doctrine\\Tests\\Proxies'); $enableSecondLevelCache = getenv('ENABLE_SECOND_LEVEL_CACHE'); if ($this->isSecondLevelCacheEnabled || $enableSecondLevelCache) { $cacheConfig = new \Doctrine\ORM\Cache\CacheConfiguration(); $cache = $this->getSharedSecondLevelCacheDriverImpl(); $factory = new DefaultCacheFactory($cacheConfig->getRegionsConfiguration(), $cache); $this->secondLevelCacheFactory = $factory; if ($this->isSecondLevelCacheLogEnabled) { $this->secondLevelCacheLogger = new StatisticsCacheLogger(); $cacheConfig->setCacheLogger($this->secondLevelCacheLogger); } $cacheConfig->setCacheFactory($factory); $config->setSecondLevelCacheEnabled(true); $config->setSecondLevelCacheConfiguration($cacheConfig); $this->isSecondLevelCacheEnabled = true; } $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(array(realpath(__DIR__ . '/Models/Cache'), realpath(__DIR__ . '/Models/GeoNames')), true)); $conn = static::$_sharedConn; $conn->getConfiguration()->setSQLLogger($this->_sqlLoggerStack); // get rid of more global state $evm = $conn->getEventManager(); foreach ($evm->getListeners() as $event => $listeners) { foreach ($listeners as $listener) { $evm->removeEventListener(array($event), $listener); } } if ($enableSecondLevelCache) { $evm->addEventListener('loadClassMetadata', new CacheMetadataListener()); } if (isset($GLOBALS['db_event_subscribers'])) { foreach (explode(",", $GLOBALS['db_event_subscribers']) as $subscriberClass) { $subscriberInstance = new $subscriberClass(); $evm->addEventSubscriber($subscriberInstance); } } if (isset($GLOBALS['debug_uow_listener'])) { $evm->addEventListener(array('onFlush'), new \Doctrine\ORM\Tools\DebugUnitOfWorkListener()); } return \Doctrine\ORM\EntityManager::create($conn, $config); }