protected function setUp() { parent::setUp(); if (\extension_loaded('memcache') && @fsockopen('localhost', 11211)) { $memcache = new \Memcache(); $memcache->addServer('localhost'); $memcache->flush(); $cacheDriver = new \Doctrine\Common\Cache\MemcacheCache(); $cacheDriver->setMemcache($memcache); $this->_em->getMetadataFactory()->setCacheDriver($cacheDriver); } else { if (\extension_loaded('apc')) { $this->_em->getMetadataFactory()->setCacheDriver(new \Doctrine\Common\Cache\ApcCache()); } else { $this->markTestSkipped('Test only works with a cache enabled.'); } } try { $this->_schemaTool->createSchema(array($this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC742User'), $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC742Comment'))); } catch (\Exception $e) { } // make sure classes will be deserialized from caches $this->_em->getMetadataFactory()->setMetadataFor(__NAMESPACE__ . '\\DDC742User', null); $this->_em->getMetadataFactory()->setMetadataFor(__NAMESPACE__ . '\\DDC742Comment', null); }
/** * Create service * * @param ServiceLocatorInterface $serviceLocator * @return \Doctrine\Common\Cache\MemcacheCache */ public function createService(ServiceLocatorInterface $serviceLocator) { $cache = new \Doctrine\Common\Cache\MemcacheCache(); $memcache = new \Memcache(); $memcache->connect('localhost', 11211); $cache->setMemcache($memcache); return $cache; }
public function getServiceConfig() { return array('factories' => array('doctrine.cache.memcache' => function ($sm) { $cache = new \Doctrine\Common\Cache\MemcacheCache(); $memcache = new \Memcache(); $memcache->connect('localhost', 11211); $cache->setMemcache($memcache); return $cache; })); }
/** * @param string $host * @param int $port * @throws BadMethodCallException * * @return \Doctrine\Common\Cache\MemcacheCache */ private static function configureMemcacheCache($host = '127.0.0.1', $port = 11211) { if (!extension_loaded('memcache')) { throw new \BadMethodCallException('MemcacheCache configured but module \'memcache\' not loaded.'); } $memcache = new \Memcache(); $memcache->addserver($host, $port); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); return $cache; }
/** * @param array $cacheConfig * @return \Doctrine\Common\Cache\MemcacheCache * @throws IncorrectConfigurationException */ public function configureCache(array $cacheConfig) { if (empty($cacheConfig['host'])) { throw new IncorrectConfigurationException('cache.memcache.host'); } if (empty($cacheConfig['port'])) { throw new IncorrectConfigurationException('cache.memcache.port'); } if (empty($cacheConfig['timeout'])) { $cacheConfig['timeout'] = 1; } $memcache = $this->configureMemcache($cacheConfig['host'], $cacheConfig['port'], $cacheConfig['timeout']); $cacheDriver = new \Doctrine\Common\Cache\MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; }
public function getMemcache($conn = 'default') { if (in_array($conn, self::$memcache_conns)) { return self::$memcache_conns[$conn]; } // connect to memcache $memcache = $this->getMemcacheServer($conn); // check for drivers if ($memcache) { // set doctrine memcache drivers $memcacheConn = new \Doctrine\Common\Cache\MemcacheCache(); $memcacheConn->setMemcache($memcache); return self::$memcache_conns[$conn] = $memcacheConn; } return false; }
/** * Get cache driver according to config.yml entry. * * Logic from Doctrine setup method * https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Setup.php#L122 * * @param array $cacheConfig * @param boolean $isDevMode * @param string|null $proxyDir * @return Cache */ protected function getManuallyDefinedCache(array $cacheConfig, $isDevMode = false, $proxyDir = null) { $proxyDir = $proxyDir ?: sys_get_temp_dir(); if ($isDevMode === false) { if (extension_loaded('apc') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'apc') { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'xcache') { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'memcache') { $memcache = new \Memcache(); $host = !empty($cacheConfig['host']) ? $cacheConfig['host'] : '127.0.0.1'; if (!empty($cacheConfig['port'])) { $memcache->connect($host, $cacheConfig['port']); } else { $memcache->connect($host); } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('memcached') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'memcached') { $memcached = new \Memcached(); $host = !empty($cacheConfig['host']) ? $cacheConfig['host'] : '127.0.0.1'; $port = !empty($cacheConfig['port']) ? $cacheConfig['port'] : 11211; $memcached->addServer($host, $port); $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcached($memcached); } elseif (extension_loaded('redis') && !empty($cacheConfig['type']) && $cacheConfig['type'] == 'redis') { $redis = new \Redis(); $host = !empty($cacheConfig['host']) ? $cacheConfig['host'] : '127.0.0.1'; if (!empty($cacheConfig['port'])) { $redis->connect($host, $cacheConfig['port']); } else { $redis->connect($host); } $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new ArrayCache(); } } else { $cache = new ArrayCache(); } if ($cache instanceof CacheProvider) { $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions } return $cache; }
/** * * 获取缓存信息 * @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; }
/** * @param Config $config * * @return \Doctrine\Common\Cache\CacheProvider * @throws \OutOfBoundsException */ public function getCache($config) { $type = $config->get('type'); switch ($type) { case 'memcache': $memcache = new \Memcache(); $memcache->connect($config->get('config.host'), $config->get('config.port')); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); break; case 'xcache': $cache = new \Doctrine\Common\Cache\XCacheCache(); break; case 'apc': $cache = new \Doctrine\Common\Cache\ApcCache(); break; case 'array': $cache = new \Doctrine\Common\Cache\ArrayCache(); break; default: throw new \OutOfBoundsException(sprintf("Unsupported cache type: '%s'", $type)); break; } return $cache; }
/** * Get cache driver. * * @param array $options * * @return \Doctrine\Common\Cache\Cache */ protected static function getCacheDriver(array $options) { $cache = $options['cache_driver']; if ($cache === null) { if (extension_loaded('apc')) { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache')) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('redis')) { $redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new \Doctrine\Common\Cache\ArrayCache(); } } return $cache; }
/** * Automatically picks the cache mechanism to use. If you pick one manually it will use that * If there is no config option for $driver in the config, or it's set to 'auto', it will * pick the best option based on which cache extensions are installed. * * @return DoctrineCacheDriver The cache driver to use */ public function getCacheDriver() { $setting = $this->config->get('system.cache.driver'); $driver_name = 'file'; if (!$setting || $setting == 'auto') { if (extension_loaded('apc')) { $driver_name = 'apc'; } elseif (extension_loaded('wincache')) { $driver_name = 'wincache'; } elseif (extension_loaded('xcache')) { $driver_name = 'xcache'; } } else { $driver_name = $setting; } switch ($driver_name) { case 'apc': $driver = new \Doctrine\Common\Cache\ApcCache(); break; case 'wincache': $driver = new \Doctrine\Common\Cache\WinCacheCache(); break; case 'xcache': $driver = new \Doctrine\Common\Cache\XcacheCache(); break; case 'memcache': $memcache = new \Memcache(); $memcache->connect($this->config->get('system.cache.memcache.server', 'localhost'), $this->config->get('system.cache.memcache.port', 11211)); $driver = new \Doctrine\Common\Cache\MemcacheCache(); $driver->setMemcache($memcache); break; default: $driver = new \Doctrine\Common\Cache\FilesystemCache($this->cache_dir); break; } return $driver; }
/** * Get Cache Driver * * @access public * @return object */ public static function getCacheDriver() { $driver_name = Config::get('system.cache.driver'); if (!$driver_name || $driver_name == 'auto') { if (extension_loaded('apc')) { $driver_name = 'apc'; } elseif (extension_loaded('wincache')) { $driver_name = 'wincache'; } elseif (extension_loaded('xcache')) { $driver_name = 'xcache'; } } else { $driver_name = 'file'; } switch ($driver_name) { case 'apc': $driver = new \Doctrine\Common\Cache\ApcCache(); break; case 'wincache': $driver = new \Doctrine\Common\Cache\WinCacheCache(); break; case 'xcache': $driver = new \Doctrine\Common\Cache\XcacheCache(); break; case 'memcache': $memcache = new \Memcache(); $memcache->connect(Config::get('system.cache.memcache.server', 'localhost'), Config::get('system.cache.memcache.port', 11211)); $driver = new \Doctrine\Common\Cache\MemcacheCache(); $driver->setMemcache($memcache); break; case 'redis': $redis = new \Redis(); $redis->connect(Config::get('system.cache.redis.server', 'localhost'), Config::get('system.cache.redis.port', 6379)); $driver = new \Doctrine\Common\Cache\RedisCache(); $driver->setRedis($redis); break; default: // Create doctrine cache directory if its not exists !Dir::exists($cache_directory = CACHE_PATH . '/doctrine/') and Dir::create($cache_directory); $driver = new \Doctrine\Common\Cache\FilesystemCache($cache_directory); break; } return $driver; }
protected function getDoctrine_Odm_Mongodb_Cache_MemcacheService() { if (isset($this->shared['doctrine.odm.mongodb.cache.memcache'])) { return $this->shared['doctrine.odm.mongodb.cache.memcache']; } $instance = new Doctrine\Common\Cache\MemcacheCache(); $this->shared['doctrine.odm.mongodb.cache.memcache'] = $instance; $instance->setMemcache($this->getDoctrine_Odm_Mongodb_Cache_MemcacheInstanceService()); return $instance; }
/** * 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; }
public static function initDoctrineCache($config) { $cache = null; if (isset(App::$inst->config['resources']['doctrine']['orm']['cache']['adapter'])) { switch (App::$inst->config['resources']['doctrine']['orm']['cache']['adapter']) { case 'Doctrine\\Common\\Cache\\MemcacheCache': if (is_null(App::$inst->memcache)) { return false; } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache(App::$inst->memcache); break; case 'Doctrine\\Common\\Cache\\RedisCache': if (is_null(App::$inst->redis)) { return false; } $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis(App::$inst->redis); break; case 'Doctrine\\Common\\Cache\\ApcCache': $cache = new \Doctrine\Common\Cache\ApcCache(); break; default: $cache = new \Doctrine\Common\Cache\ArrayCache(); break; } } else { $cache = new \Doctrine\Common\Cache\ArrayCache(); } $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setMetadataCacheImpl($cache); return $cache; }
/** * Initialize Doctrine entity manager in DI container. * * This method can be called from InstallApp after updating * doctrine configuration. * * @param Pimple\Container $container [description] */ public function register(Container $container) { if ($container['config'] !== null && isset($container['config']["doctrine"])) { $container['em.config'] = function ($c) { $config = Setup::createAnnotationMetadataConfiguration($c['entitiesPaths'], (bool) $c['config']['devMode'], ROADIZ_ROOT . '/gen-src/Proxies', null, false); $config->setProxyDir(ROADIZ_ROOT . '/gen-src/Proxies'); $config->setProxyNamespace('Proxies'); return $config; }; $container['em'] = function ($c) { try { $c['stopwatch']->start('initDoctrine'); $em = EntityManager::create($c['config']["doctrine"], $c['em.config']); $evm = $em->getEventManager(); /* * Create dynamic dicriminator map for our Node system */ $evm->addEventListener(Events::loadClassMetadata, new DataInheritanceEvent()); $resultCacheDriver = $em->getConfiguration()->getResultCacheImpl(); if ($resultCacheDriver !== null) { $resultCacheDriver->setNamespace($c['config']["appNamespace"]); } $hydratationCacheDriver = $em->getConfiguration()->getHydrationCacheImpl(); if ($hydratationCacheDriver !== null) { $hydratationCacheDriver->setNamespace($c['config']["appNamespace"]); } $queryCacheDriver = $em->getConfiguration()->getQueryCacheImpl(); if ($queryCacheDriver !== null) { $queryCacheDriver->setNamespace($c['config']["appNamespace"]); } $metadataCacheDriver = $em->getConfiguration()->getMetadataCacheImpl(); if (null !== $metadataCacheDriver) { $metadataCacheDriver->setNamespace($c['config']["appNamespace"]); } $c['stopwatch']->stop('initDoctrine'); return $em; } catch (\PDOException $e) { $c['session']->getFlashBag()->add('error', $e->getMessage()); return null; } }; } /* * logic from Doctrine setup method * * https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Setup.php#L122 */ $container['nodesSourcesUrlCacheProvider'] = function ($c) { if (true === (bool) $c['config']['devMode']) { $cache = new ArrayCache(); } else { if (extension_loaded('apc')) { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache')) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('redis')) { $redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new ArrayCache(); } } if ($cache instanceof CacheProvider) { $cache->setNamespace($c['config']["appNamespace"] . "_nsurls"); // to avoid collisions } return $cache; }; return $container; }
} break; } /** Load caching as specified in configuration */ switch ($config["cache"]["type"]) { case "array": $Cache = new \Doctrine\Common\Cache\ArrayCache(); break; case 'apc': $Cache = new \Doctrine\Common\Cache\ApcCache(); break; case 'memcache': $memcache = new Memcache(); $memcache->connect($settings["host"], $settings["port"]); $Cache = new \Doctrine\Common\Cache\MemcacheCache(); $Cache->setMemcache($memcache); break; case 'xcache': $Cache = \Doctrine\Common\Cache\XcacheCache(); break; case 'redis': if (!class_exists("Redis")) { throw new Exception("Redis cache is not available"); } $redis = new Redis(); $redis->connect($settings["host"], $settings["port"]); $Cache = new \Doctrine\Common\Cache\RedisCache(); $Cache->setRedis($redis); } // Build configuration $Configuration->setMetadataCacheImpl($Cache);
/** * Get cache driver by options list * * @param mixed $options Options from config.ini * * @return \Doctrine\Common\Cache\Cache */ public static function getCacheDriverByOptions($options) { if (!isset($options) || !is_array($options) || !isset($options['type'])) { $options = array('type' => null); } // Auto-detection if ('auto' == $options['type']) { foreach (static::$cacheDriversQuery as $type) { $method = 'detectCacheDriver' . ucfirst($type); // $method assembled from 'detectCacheDriver' + $type if (static::$method()) { $options['type'] = $type; break; } } } if ('apc' == $options['type']) { // APC $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif ('memcache' == $options['type'] && isset($options['servers']) && class_exists('Memcache', false)) { // Memcache $servers = explode(';', $options['servers']); if ($servers) { $memcache = new \Memcache(); foreach ($servers as $row) { $row = trim($row); $tmp = explode(':', $row, 2); if ('unix' == $tmp[0]) { $memcache->addServer($row, 0); } elseif (isset($tmp[1])) { $memcache->addServer($tmp[0], $tmp[1]); } else { $memcache->addServer($tmp[0]); } } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } } elseif ('xcache' == $options['type']) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } else { // Default cache - file system cache $cache = new \XLite\Core\FileCache(LC_DIR_DATACACHE); } if (isset($options['namespace']) && $options['namespace']) { // TODO - namespace temporary is empty - bug into Doctrine\Common\Cache\AbstractCache::deleteByPrefix() //$cache->setNamespace($options['namespace']); } return $cache; }
/** * Initializes the doctrine framework and * sets all required configuration options. * * @param none * @return nothing */ public static function initializeDoctrine() { $config = new Configuration(); $driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__)); $config->setMetadataDriverImpl($driverImpl); $connectionOptions = PartKeepr::createConnectionOptionsFromConfig(); switch (strtolower(PartKeeprConfiguration::getOption("partkeepr.cache.implementation", "default"))) { case "apc": $cache = new \Doctrine\Common\Cache\ApcCache(); break; case "xcache": if (php_sapi_name() !== "cli") { $cache = new \Doctrine\Common\Cache\XcacheCache(); } else { // For CLI SAPIs, revert to the ArrayCache as Xcache spits out strange warnings when running in CLI. $cache = new \Doctrine\Common\Cache\ArrayCache(); } break; case "memcache": $memcache = new \Memcache(); $memcache->connect(PartKeeprConfiguration::getOption("partkeepr.cache.memcache.host", "localhost"), PartKeeprConfiguration::getOption("partkeepr.cache.memcache.port", "11211")); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); break; case "default": case "auto": if (extension_loaded("xcache")) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } else { if (extension_loaded("apc")) { $cache = new \Doctrine\Common\Cache\ApcCache(); } else { $cache = new \Doctrine\Common\Cache\ArrayCache(); } } break; case "none": $cache = new \Doctrine\Common\Cache\ArrayCache(); break; } $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setProxyDir(self::getRootDirectory() . '/data/proxies'); $config->setProxyNamespace('Proxies'); $config->setEntityNamespaces(self::getEntityClasses()); $config->setAutoGenerateProxyClasses(false); if (PartKeeprConfiguration::getOption("partkeepr.database.echo_sql_log", false) === true) { $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger(); $config->setSQLLogger($logger); } self::$entityManager = EntityManager::create($connectionOptions, $config); }
/** * Get Doctrine2Cache * * @return Doctrine\Common\Cache */ public function getDoctrine2cache() { if ($this->_d2cache === null) { // Get Doctrine configuration options from the application.ini file $config = $this->getOptions(); if (!isset($config['autoload_method'])) { $config['autoload_method'] = 'git'; } switch ($config['autoload_method']) { case 'pear': require_once $config['path'] . '/Tools/Setup.php'; Doctrine\ORM\Tools\Setup::registerAutoloadPEAR(); break; case 'dir': require_once $config['path'] . '/Tools/Setup.php'; // FIXME Doctrine\ORM\Tools\Setup::registerAutoloadDirectory(); break; case 'composer': break; default: require_once $config['path'] . '/lib/Doctrine/ORM/Tools/Setup.php'; Doctrine\ORM\Tools\Setup::registerAutoloadGit($config['path']); } if ($config['type'] == 'ApcCache') { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif ($config['type'] == 'MemcacheCache') { $memcache = new Memcache(); for ($cnt = 0; $cnt < count($config['memcache']['servers']); $cnt++) { $server = $config['memcache']['servers'][$cnt]; $memcache->addServer(isset($server['host']) ? $server['host'] : '127.0.0.1', isset($server['port']) ? $server['port'] : 11211, isset($server['persistent']) ? $server['persistent'] : false, isset($server['weight']) ? $server['weight'] : 1, isset($server['timeout']) ? $server['timeout'] : 1, isset($server['retry_int']) ? $server['retry_int'] : 15); } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } else { $cache = new \Doctrine\Common\Cache\ArrayCache(); } if (isset($config['namespace'])) { $cache->setNamespace($config['namespace']); } // stick the cache in the registry Zend_Registry::set('d2cache', $cache); $this->setDoctrine2Cache($cache); } return $this->_d2cache; }
/** * Creates an instance of a doctrine cache provider and assigns it an ID. * * @param string $id - Instance ID. * @param string|CacheProvider $typeOrInstance - Instance type. * @param array $config - Instance config. * @param bool $refresh - Re-create the instance even if it's already created. * * @throws InvalidTypeException * @throws InvalidConfigException * @throws MissingExtensionException * * @return CacheProvider */ public function create($id, $typeOrInstance, array $config = [], $refresh = false) { if (!is_string($id)) { throw new \InvalidArgumentException('Argument $id must be a string.'); } if (!is_string($id) && (!is_object($typeOrInstance) || !$typeOrInstance instanceof CacheProvider)) { throw new \InvalidArgumentException('Argument $typeOrInstance must be a string or an instance of \\Doctrine\\Common\\Cache\\CacheProvider.'); } if (!isset($this->_instances[$id]) || $refresh) { if (is_object($typeOrInstance)) { $this->_instances[$id] = $typeOrInstance; } else { $cache = null; switch ($typeOrInstance) { case self::TYPE_APCU: if (!function_exists('apcu_fetch')) { throw MissingExtensionException::create('apcu', 'ApcuCache'); } $cache = new \Doctrine\Common\Cache\ApcuCache(); break; case self::TYPE_ARRAY: $cache = new \Doctrine\Common\Cache\ArrayCache(); break; case self::TYPE_COUCHBASE: if (!class_exists('\\Couchbase')) { throw MissingExtensionException::create('couchbase', 'CouchbaseCache'); } $config = array_merge(['couchbase' => null], $config); if (!is_object($config['couchbase']) || !$config['couchbase'] instanceof \Couchbase) { throw InvalidConfigException::create('couchbase', 'an instance of \\Couchbase.'); } $cache = new \Doctrine\Common\Cache\CouchbaseCache(); $cache->setCouchbase($config['couchbase']); break; case self::TYPE_FILESYSTEM: $config = array_merge(['directory' => null, 'extension' => FilesystemCache::EXTENSION, 'umask' => 02], $config); $cache = new FilesystemCache($config['directory'], $config['extension'], $config['umask']); break; case self::TYPE_MEMCACHE: if (!class_exists('\\Memcache')) { throw MissingExtensionException::create('memcache', 'MemcacheCache'); } $config = array_merge(['memcache' => null], $config); if (!is_object($config['memcache']) || !$config['memcache'] instanceof \Memcache) { throw InvalidConfigException::create('memcache', 'an instance of \\Memcache.'); } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($config['memcache']); break; case self::TYPE_MEMCACHED: if (!class_exists('\\Memcached')) { throw MissingExtensionException::create('memcached', 'MemcachedCache'); } $config = array_merge(['memcached' => null], $config); if (!is_object($config['memcached']) || !$config['memcached'] instanceof \Memcached) { throw InvalidConfigException::create('memcached', 'an instance of \\Memcached.'); } $cache = new \Doctrine\Common\Cache\MemcachedCache(); $cache->setMemcached($config['memcached']); break; case self::TYPE_MONGODB: if (!class_exists('\\MongoCollection')) { throw MissingExtensionException::create('mongo', 'MongoDBCache'); } $config = array_merge(['mongoCollection' => null], $config); if (!is_object($config['mongoCollection']) || !$config['mongoCollection'] instanceof \MongoCollection) { throw InvalidConfigException::create('mongoCollection', 'an instance of \\MongoCollection.'); } $cache = new \Doctrine\Common\Cache\MongoDBCache($config['mongoCollection']); break; case self::TYPE_PHP_FILE: $config = array_merge(['directory' => null, 'extension' => PhpFileCache::EXTENSION, 'umask' => 02], $config); $cache = new PhpFileCache($config['directory'], $config['extension'], $config['umask']); break; case self::TYPE_PREDIS: if (!class_exists('\\Predis\\ClientInterface')) { throw MissingExtensionException::create('predis', 'PredisCache'); } $config = array_merge(['predisClient' => null], $config); if (!is_object($config['predisClient']) || !$config['predisClient'] instanceof \Predis\ClientInterface) { throw InvalidConfigException::create('predisClient', 'an instance of \\Predis\\ClientInterface.'); } $cache = new \Doctrine\Common\Cache\PredisCache($config['predisClient']); break; case self::TYPE_REDIS: if (!class_exists('\\Redis')) { throw MissingExtensionException::create('redis', 'RedisCache'); } $config = array_merge(['redis' => null], $config); if (!is_object($config['redis']) || !$config['redis'] instanceof \Redis) { throw InvalidConfigException::create('redis', 'an instance of \\Redis.'); } $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($config['redis']); break; case self::TYPE_RIAK: if (!class_exists('\\Riak\\Bucket')) { throw MissingExtensionException::create('riak', 'RiakCache'); } $config = array_merge(['riakBucket' => null], $config); if (!is_object($config['riakBucket']) || !$config['riakBucket'] instanceof \Riak\Bucket) { throw InvalidConfigException::create('riakBucket', 'an instance of \\Riak\\Bucket.'); } $cache = new \Doctrine\Common\Cache\RiakCache($config['riakBucket']); break; case self::TYPE_SQLITE3: if (!class_exists('\\SQLite3')) { throw MissingExtensionException::create('sqlite3', 'SQLite3Cache'); } $config = array_merge(['sqlite3' => null, 'sqlite3Table' => null], $config); if (!is_object($config['sqlite3']) || !$config['sqlite3'] instanceof \SQLite3) { throw InvalidConfigException::create('sqlite3', 'an instance of \\SQLite3.'); } if (!is_string($config['sqlite3Table']) || $config['sqlite3Table'] === '') { throw InvalidConfigException::create('sqlite3Table', 'a non-empty string.'); } $cache = new \Doctrine\Common\Cache\SQLite3Cache($config['sqlite3'], $config['sqlite3Table']); break; case self::TYPE_VOID: $cache = new VoidCache(); break; case self::TYPE_WINCACHE: if (!function_exists('wincache_ucache_get')) { throw MissingExtensionException::create('wincache', 'WinCacheCache'); } $cache = new \Doctrine\Common\Cache\WinCacheCache(); break; case self::TYPE_XCACHE: if (!function_exists('xcache_isset')) { throw MissingExtensionException::create('xcache', 'XcacheCache'); } $cache = new \Doctrine\Common\Cache\XcacheCache(); break; case self::TYPE_ZEND_DATA: if (!function_exists('zend_shm_cache_fetch')) { throw new \RuntimeException('Zend Data component must be installed and available before using this Cache Driver.'); } $cache = new \Doctrine\Common\Cache\ZendDataCache(); break; default: throw InvalidTypeException::create($typeOrInstance); } $this->_instances[$id] = $cache; } } return $this->_instances[$id]; }
protected function initCache() { $cacheConfiguration = $this['config']->getCacheConfiguration(); switch ($cacheConfiguration['driver']['type']) { case 'apc': $cacheDriver = new \Doctrine\Common\Cache\ApcCache(); break; case 'memcached': $memcached = new \Memcached(); $memcached->addServer($cacheConfiguration['driver']['host'], $cacheConfiguration['driver']['port']); $cacheDriver = new \Doctrine\Common\Cache\MemcachedCache(); $cacheDriver->setMemcached($memcached); break; case 'memcache': $memcache = new \Memcache(); $memcache->connect($cacheConfiguration['driver']['host'], $cacheConfiguration['driver']['port']); $cacheDriver = new \Doctrine\Common\Cache\MemcacheCache(); $cacheDriver->setMemcache($memcache); break; case 'file': $cacheDriver = new PhPFileCache(APPLICATION_PATH . '/doctrine-cache', 'txt'); $this->setCacheDriver($cacheDriver); break; default: $cacheDriver = new \Doctrine\Common\Cache\ArrayCache(); break; } if (isset($cacheConfiguration['driver']['prefix'])) { $cacheDriver->setNamespace($cacheConfiguration['driver']['prefix'] . '_'); } $this->setCacheDriver($cacheDriver); }
/** * 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; }
/** * Creates a configuration without a metadata driver. * * @param bool $isDevMode * @param string $proxyDir * @param Cache $cache * * @return Configuration */ public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null) { $proxyDir = $proxyDir ?: sys_get_temp_dir(); if ($isDevMode === false && $cache === null) { if (extension_loaded('apc')) { $cache = new \Doctrine\Common\Cache\ApcCache(); } elseif (extension_loaded('xcache')) { $cache = new \Doctrine\Common\Cache\XcacheCache(); } elseif (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); } elseif (extension_loaded('redis')) { $redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new \Doctrine\Common\Cache\RedisCache(); $cache->setRedis($redis); } else { $cache = new ArrayCache(); } } elseif ($cache === null) { $cache = new ArrayCache(); } if ($cache instanceof CacheProvider) { $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions } $config = new Configuration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setProxyDir($proxyDir); $config->setProxyNamespace('DoctrineProxies'); $config->setAutoGenerateProxyClasses($isDevMode); return $config; }
/** * Build Memcache driver * * @return \Doctrine\Common\Cache\CacheProvider */ protected function buildMemcacheDriver() { $servers = explode(';', $this->options['servers']) ?: array('localhost'); $memcache = new \Memcache(); foreach ($servers as $row) { $row = trim($row); $tmp = explode(':', $row, 2); if ('unix' == $tmp[0]) { $memcache->addServer($row, 0); } elseif (isset($tmp[1])) { $memcache->addServer($tmp[0], $tmp[1]); } else { $memcache->addServer($tmp[0]); } } $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); return $cache; }