Sets the memcache instance to use.
public setMemcache ( Memcache $memcache ) : void | ||
$memcache | Memcache | |
리턴 | void |
private static function driver() { $cacheType = strtolower(Config::get('cache.type')); if ($cacheType == 'apc') { $cacheDriver = new ApcCache(); return $cacheDriver; } if ($cacheType == 'memcache') { $memcacheConfig = Config::get('cache.memcache'); $memcache = new Memcache(); $memcache->connect($memcacheConfig['host'], $memcacheConfig['port']); $cacheDriver = new MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; } if ($cacheType == 'xcache') { $cacheDriver = new XcacheCache(); return $cacheDriver; } if ($cacheType == 'redis') { $redisConfig = Config::get('cache.redis'); $redis = new Redis(); $redis->connect($redisConfig['host'], $redisConfig['port']); $cacheDriver = new RedisCache(); $cacheDriver->setRedis($redis); return $cacheDriver; } return null; }
private function init($namespace) { $prefix = self::$namespacePrefix; $namespace = "{$prefix}:{$namespace}"; $type = 0; if (static::$TYPE_PREFER_DEFAULT == 0) { if (isset(static::$_TYPE)) { $type = static::$_TYPE; } else { $type = static::$TYPE_DEFAULT; } } else { $type = static::$TYPE_PREFER_DEFAULT; } if ($type == static::$TYPE_APC) { $this->_cacheImplement = new ApcCache(); } else { if ($type == static::$TYPE_MEMCACHE) { $m = new MemcacheCache(); $memcache = new \Memcache(); $memcache->connect('localhost', 11211); $m->setMemcache($memcache); $this->_cacheImplement = $m; } else { if ($type == static::$TYPE_XCACHE) { $this->_cacheImplement = new XcacheCache(); } else { $this->_cacheImplement = new ArrayCache(); } } } $this->_cacheImplement->setNamespace($namespace); }
/** * @test */ public function WithMemcacheType_Build_ReturnMemcache() { $memcacheCache = new MemcacheCache(); $memcacheCache->setMemcache(new MemcacheSpy()); $cache = $this->cacheBuilder->withCacheProvider($memcacheCache)->build(); $this->assertAttributeInstanceOf('Doctrine\\Common\\Cache\\MemcacheCache', 'cache', $cache); }
public function register(Application $app) { $app->setParameter('cache', ['namespace' => null, 'type' => 'array']); $app->singleton('cache', function () use($app) { $cache = null; $type = $app->getParameter('cache.type', 'array'); if ($type == 'array') { $cache = new ArrayCache(); } elseif ($type == 'apc') { $cache = new ApcCache(); } elseif ($type == 'xcache') { $cache = new XcacheCache(); } elseif ($type == 'memcache') { $cache = new MemcacheCache(); $memcache = new \Memcache(); $memcache->addserver($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211)); $cache->setMemcache($memcache); } elseif ($type == 'memcached') { $cache = new MemcachedCache(); $memcached = new \Memcached(); $memcached->addServer($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211)); $cache->setMemcached($memcached); } $cache->setNamespace($app->getParameter('cache.namespace')); return $cache; }); }
/** * @return CacheProvider */ protected function getCacheProvider() { $config = $this->getConfig()->get('orm.cache'); switch ($config['provider']) { case 'array': return new ArrayCache(); case 'filesystem': return new FilesystemCache($config[$config['provider']]['directory'], $config[$config['provider']]['extension']); case 'redis': $redis = new \Redis(); $redis->connect($config[$config['provider']]['host'], $config[$config['provider']]['port']); $redis->select($config[$config['provider']]['dbindex']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; case 'memcached': $memcache = new \Memcache(); $memcache->connect($config[$config['provider']]['host'], $config[$config['provider']]['port'], $config[$config['provider']]['weight']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; default: return null; } }
/** * {@inheritdoc} */ public function getAdapter(array $config) { $memcache = new Memcache(); $memcache->connect($config['host'], $config['port']); $client = new MemcacheCache(); $client->setMemcache($memcache); return new DoctrineCachePool($client); }
/** * Sets up and returns the CacheProvider * @param $config * @return \Doctrine\Common\Cache\CacheProvider */ protected function initialize($config) { $memcache = new \Memcache(); $memcache->connect($config['host'], $config['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }
/** * Return's the new cache instance. * * @param array $configuration The cache configuration * * @return \Doctrine\Common\Cache\CacheProvider The cache instance */ public static function get(array $configuration = array()) { if (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect($configuration[CacheKeys::HOST]); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; } }
public function make($config = null) { if (!extension_loaded('memcache')) { throw new \RuntimeException('Memcache extension was not loaded.'); } $memcache = new Memcache(); $memcache->connect($config['host'], $config['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }
protected function drivers(Container $app) { $app['cache.driver.memcached'] = $app->protect(function ($options) { $servers = $options['servers']; $memcached = new \Memcached(); foreach ($servers as $server) { $memcached->addServer($server['host'], $server['port']); } $cacheDriver = new MemcachedCache(); $cacheDriver->setMemcached($memcached); return $cacheDriver; }); $app['cache.driver.memcache'] = $app->protect(function ($options) { $options = array_merge(['host' => '127.0.0.1', 'port' => 11211], $options); $memcache = new \Memcache(); $memcache->connect($options['host'], $options['port']); $cacheDriver = new MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; }); $app['cache.driver.file'] = $app->protect(function ($options) { if (empty($options['path']) || false === is_dir($options['path'])) { throw new \InvalidArgumentException('You must specify "path" for Filesystem.'); } return new FilesystemCache($options['path']); }); $app['cache.driver.redis'] = $app->protect(function ($options) { $options = array_merge(['host' => '127.0.0.1', 'port' => 6379], $options); $redis = new \Redis(); $redis->connect($options['host'], $options['port']); $cacheDriver = new RedisCache(); $cacheDriver->setRedis($redis); return $cacheDriver; }); $app['cache.driver.mongodb'] = $app->protect(function ($options) { if (empty($options['server']) || empty($options['name']) || empty($options['collection'])) { throw new \InvalidArgumentException('You must specify "server", "name" and "collection" for MongoDB.'); } $client = new \MongoClient($options['server']); $db = new \MongoDB($client, $options['name']); $collection = new \MongoCollection($db, $options['collection']); return new MongoDBCache($collection); }); $app['cache.driver.array'] = $app->protect(function () { return new ArrayCache(); }); $app['cache.driver.apc'] = $app->protect(function () { return new ApcuCache(); }); $app['cache.driver.xcache'] = $app->protect(function () { return new XcacheCache(); }); }
private function buildMemcacheCache() { if (null === $this->host) { throw new HostShouldBeProvidedException(); } if (null === $this->port) { $this->port = Memcache::DEFAULT_PORT; } if (null === $this->timeout) { $this->timeout = Memcache::DEFAULT_TIMEOUT; } $this->server->addserver($this->host, $this->port, null, null, $this->timeout); $this->cacheProvider->setMemcache($this->server); }
public function createService(ServiceLocatorInterface $serviceLocator) { $memcache = $serviceLocator->get('memcache'); $config = $serviceLocator->get('ApplicationConfig'); if (is_array($config) && isset($config['tenant_id'])) { if ($memcache instanceof \Memcache) { $cache = new MemcacheCache(); $cache->setNamespace(sprintf('stokq.doctrine.%s', $config['tenant_id'])); $cache->setMemcache($memcache); return $cache; } } else { throw new \RuntimeException("Unable to get `tenant_id` from config."); } throw new \RuntimeException("Invalid memcache instance returned."); }
/** * Creates cache by name. * * @param string $name Name. * @param array $options Options. * * @return CacheProvider * @throws \InvalidArgumentException When cache provider with given name not found. * @throws \LogicException When no caches provided for "chain" cache. */ public function create($name, array $options = array()) { switch ($name) { case 'chain': $valid_caches = array(); foreach (array_filter($options) as $cache_name) { $valid_caches[] = self::create($cache_name); } if (!$valid_caches) { throw new \LogicException('No valid caches provided for "chain" cache.'); } $cache_driver = new ChainCache($valid_caches); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'array': $cache_driver = new ArrayCache(); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'apc': $cache_driver = new ApcCache(); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'memcache': $memcache = new \Memcache(); $memcache->connect('localhost', 11211); $cache_driver = new MemcacheCache(); $cache_driver->setMemcache($memcache); $cache_driver->setNamespace($this->_namespace); return $cache_driver; case 'memcached': $memcached = new \Memcached(); $memcached->addServer('memcache_host', 11211); $cache_driver = new MemcachedCache(); $cache_driver->setMemcached($memcached); $cache_driver->setNamespace($this->_namespace); return $cache_driver; } throw new \InvalidArgumentException('Cache provider "' . $name . '" not found.'); }
/** * @param Container $container */ public function register(Container $container) { foreach ($this->getMongodbOdmDefaults($container) as $key => $value) { if (!isset($container[$key])) { $container[$key] = $value; } } $container['mongodbodm.dm.default_options'] = array('connection' => 'default', 'database' => null, 'mappings' => array(), 'types' => array()); $container['mongodbodm.dms.options.initializer'] = $container->protect(function () use($container) { static $initialized = false; if ($initialized) { return; } $initialized = true; if (!isset($container['mongodbodm.dms.options'])) { $container['mongodbodm.dms.options'] = array('default' => isset($container['mongodbodm.dm.options']) ? $container['mongodbodm.dm.options'] : array()); } $tmp = $container['mongodbodm.dms.options']; foreach ($tmp as $name => &$options) { $options = array_replace($container['mongodbodm.dm.default_options'], $options); if (!isset($container['mongodbodm.dms.default'])) { $container['mongodbodm.dms.default'] = $name; } } $container['mongodbodm.dms.options'] = $tmp; }); $container['mongodbodm.dm_name_from_param_key'] = $container->protect(function ($paramKey) use($container) { $container['mongodbodm.dms.options.initializer'](); if (isset($container[$paramKey])) { return $container[$paramKey]; } return $container['mongodbodm.dms.default']; }); $container['mongodbodm.dms'] = function () use($container) { $container['mongodbodm.dms.options.initializer'](); $dms = new Container(); foreach ($container['mongodbodm.dms.options'] as $name => $options) { if ($container['mongodbodm.dms.default'] === $name) { // we use shortcuts here in case the default has been overridden $config = $container['mongodbodm.dm.config']; } else { $config = $container['mongodbodm.dms.config'][$name]; } if (isset($options['database'])) { $config->setDefaultDB($options['database']); } $dms[$name] = function () use($container, $options, $config) { return DocumentManager::create($container['mongodbs'][$options['connection']], $config, $container['mongodbs.event_manager'][$options['connection']]); }; } return $dms; }; $container['mongodbodm.dms.config'] = function () use($container) { $container['mongodbodm.dms.options.initializer'](); $configs = new Container(); foreach ($container['mongodbodm.dms.options'] as $name => $options) { $config = new Configuration(); $container['mongodbodm.cache.configurer']($name, $config, $options); $config->setProxyDir($container['mongodbodm.proxies_dir']); $config->setProxyNamespace($container['mongodbodm.proxies_namespace']); $config->setAutoGenerateProxyClasses($container['mongodbodm.auto_generate_proxies']); $config->setHydratorDir($container['mongodbodm.hydrator_dir']); $config->setHydratorNamespace($container['mongodbodm.hydrator_namespace']); $config->setAutoGenerateHydratorClasses($container['mongodbodm.auto_generate_hydrators']); $config->setClassMetadataFactoryName($container['mongodbodm.class_metadata_factory_name']); $config->setDefaultRepositoryClassName($container['mongodbodm.default_repository_class']); $config->setRepositoryFactory($container['mongodbodm.repository_factory']); $chain = $container['mongodbodm.mapping_driver_chain.locator']($name); foreach ((array) $options['mappings'] as $entity) { if (!is_array($entity)) { throw new \InvalidArgumentException("The 'mongodbodm.dm.options' option 'mappings' should be an array of arrays."); } if (isset($entity['alias'])) { $config->addDocumentNamespace($entity['alias'], $entity['namespace']); } switch ($entity['type']) { case 'annotation': $useSimpleAnnotationReader = isset($entity['use_simple_annotation_reader']) ? $entity['use_simple_annotation_reader'] : true; $driver = $config->newDefaultAnnotationDriver((array) $entity['path'], $useSimpleAnnotationReader); $chain->addDriver($driver, $entity['namespace']); break; case 'yml': $driver = new YamlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_yml': $driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_xml': $driver = new SimplifiedXmlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'php': $driver = new StaticPHPDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type'])); break; } } $config->setMetadataDriverImpl($chain); foreach ((array) $options['types'] as $typeName => $typeClass) { if (Type::hasType($typeName)) { Type::overrideType($typeName, $typeClass); } else { Type::addType($typeName, $typeClass); } } $configs[$name] = $config; } return $configs; }; $container['mongodbodm.cache.configurer'] = $container->protect(function ($name, Configuration $config, $options) use($container) { $config->setMetadataCacheImpl($container['mongodbodm.cache.locator']($name, 'metadata', $options)); }); $container['mongodbodm.cache.locator'] = $container->protect(function ($name, $cacheName, $options) use($container) { $cacheNameKey = $cacheName . '_cache'; if (!isset($options[$cacheNameKey])) { $options[$cacheNameKey] = $container['mongodbodm.default_cache']; } if (isset($options[$cacheNameKey]) && !is_array($options[$cacheNameKey])) { $options[$cacheNameKey] = array('driver' => $options[$cacheNameKey]); } if (!isset($options[$cacheNameKey]['driver'])) { throw new \RuntimeException("No driver specified for '{$cacheName}'"); } $driver = $options[$cacheNameKey]['driver']; $cacheInstanceKey = 'mongodbodm.cache.instances.' . $name . '.' . $cacheName; if (isset($container[$cacheInstanceKey])) { return $container[$cacheInstanceKey]; } $cache = $container['mongodbodm.cache.factory']($driver, $options[$cacheNameKey]); if (isset($options['cache_namespace']) && $cache instanceof CacheProvider) { $cache->setNamespace($options['cache_namespace']); } return $container[$cacheInstanceKey] = $cache; }); $container['mongodbodm.cache.factory.backing_memcache'] = $container->protect(function () { return new \Memcache(); }); $container['mongodbodm.cache.factory.memcache'] = $container->protect(function ($cacheOptions) use($container) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcache cache'); } /** @var \Memcache $memcache */ $memcache = $container['mongodbodm.cache.factory.backing_memcache'](); $memcache->connect($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }); $container['mongodbodm.cache.factory.backing_memcached'] = $container->protect(function () { return new \Memcached(); }); $container['mongodbodm.cache.factory.memcached'] = $container->protect(function ($cacheOptions) use($container) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcached cache'); } /** @var \Memcached $memcached */ $memcached = $container['mongodbodm.cache.factory.backing_memcached'](); $memcached->addServer($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }); $container['mongodbodm.cache.factory.backing_redis'] = $container->protect(function () { return new \Redis(); }); $container['mongodbodm.cache.factory.redis'] = $container->protect(function ($cacheOptions) use($container) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for redis cache'); } /** @var \Redis $redis */ $redis = $container['mongodbodm.cache.factory.backing_redis'](); $redis->connect($cacheOptions['host'], $cacheOptions['port']); if (isset($cacheOptions['password'])) { $redis->auth($cacheOptions['password']); } $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }); $container['mongodbodm.cache.factory.array'] = $container->protect(function () { return new ArrayCache(); }); $container['mongodbodm.cache.factory.apc'] = $container->protect(function () { return new ApcCache(); }); $container['mongodbodm.cache.factory.apcu'] = $container->protect(function () { return new ApcuCache(); }); $container['mongodbodm.cache.factory.xcache'] = $container->protect(function () { return new XcacheCache(); }); $container['mongodbodm.cache.factory.filesystem'] = $container->protect(function ($cacheOptions) { if (empty($cacheOptions['path'])) { throw new \RuntimeException('FilesystemCache path not defined'); } $cacheOptions += array('extension' => FilesystemCache::EXTENSION, 'umask' => 02); return new FilesystemCache($cacheOptions['path'], $cacheOptions['extension'], $cacheOptions['umask']); }); $container['mongodbodm.cache.factory.couchbase'] = $container->protect(function ($cacheOptions) { if (empty($cacheOptions['host'])) { $cacheOptions['host'] = '127.0.0.1'; } if (empty($cacheOptions['bucket'])) { $cacheOptions['bucket'] = 'default'; } $couchbase = new \Couchbase($cacheOptions['host'], $cacheOptions['user'], $cacheOptions['password'], $cacheOptions['bucket']); $cache = new CouchbaseCache(); $cache->setCouchbase($couchbase); return $cache; }); $container['mongodbodm.cache.factory'] = $container->protect(function ($driver, $cacheOptions) use($container) { switch ($driver) { case 'array': return $container['mongodbodm.cache.factory.array'](); case 'apc': return $container['mongodbodm.cache.factory.apc'](); case 'apcu': return $container['mongodbodm.cache.factory.apcu'](); case 'xcache': return $container['mongodbodm.cache.factory.xcache'](); case 'memcache': return $container['mongodbodm.cache.factory.memcache']($cacheOptions); case 'memcached': return $container['mongodbodm.cache.factory.memcached']($cacheOptions); case 'filesystem': return $container['mongodbodm.cache.factory.filesystem']($cacheOptions); case 'redis': return $container['mongodbodm.cache.factory.redis']($cacheOptions); case 'couchbase': return $container['mongodbodm.cache.factory.couchbase']($cacheOptions); default: throw new \RuntimeException("Unsupported cache type '{$driver}' specified"); } }); $container['mongodbodm.mapping_driver_chain.locator'] = $container->protect(function ($name = null) use($container) { $container['mongodbodm.dms.options.initializer'](); if (null === $name) { $name = $container['mongodbodm.dms.default']; } $cacheInstanceKey = 'mongodbodm.mapping_driver_chain.instances.' . $name; if (isset($container[$cacheInstanceKey])) { return $container[$cacheInstanceKey]; } return $container[$cacheInstanceKey] = $container['mongodbodm.mapping_driver_chain.factory']($name); }); $container['mongodbodm.mapping_driver_chain.factory'] = $container->protect(function ($name) use($container) { return new MappingDriverChain(); }); $container['mongodbodm.add_mapping_driver'] = $container->protect(function (MappingDriver $mappingDriver, $namespace, $name = null) use($container) { $container['mongodbodm.dms.options.initializer'](); if (null === $name) { $name = $container['mongodbodm.dms.default']; } /** @var MappingDriverChain $driverChain */ $driverChain = $container['mongodbodm.mapping_driver_chain.locator']($name); $driverChain->addDriver($mappingDriver, $namespace); }); $container['mongodbodm.repository_factory'] = function ($container) { return new DefaultRepositoryFactory(); }; $container['mongodbodm.dm'] = function ($container) { $dms = $container['mongodbodm.dms']; return $dms[$container['mongodbodm.dms.default']]; }; $container['mongodbodm.dm.config'] = function ($container) { $configs = $container['mongodbodm.dms.config']; return $configs[$container['mongodbodm.dms.default']]; }; }
/** * 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 DoctrineCache\CacheProvider The cache driver to use */ public function getCacheDriver() { $setting = $this->driver_setting; $driver_name = 'file'; if (!$setting || $setting == 'auto') { if (extension_loaded('apcu')) { $driver_name = 'apcu'; } elseif (extension_loaded('apc')) { $driver_name = 'apc'; } elseif (extension_loaded('wincache')) { $driver_name = 'wincache'; } elseif (extension_loaded('xcache')) { $driver_name = 'xcache'; } } else { $driver_name = $setting; } $this->driver_name = $driver_name; switch ($driver_name) { case 'apc': $driver = new DoctrineCache\ApcCache(); break; case 'apcu': $driver = new DoctrineCache\ApcuCache(); break; case 'wincache': $driver = new DoctrineCache\WinCacheCache(); break; case 'xcache': $driver = new DoctrineCache\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 DoctrineCache\MemcacheCache(); $driver->setMemcache($memcache); break; case 'memcached': $memcached = new \Memcached(); $memcached->addServer($this->config->get('system.cache.memcached.server', 'localhost'), $this->config->get('system.cache.memcached.port', 11211)); $driver = new DoctrineCache\MemcachedCache(); $driver->setMemcached($memcached); break; case 'redis': $redis = new \Redis(); $socket = $this->config->get('system.cache.redis.socket', false); if ($socket) { $redis->connect($socket); } else { $redis->connect($this->config->get('system.cache.redis.server', 'localhost'), $this->config->get('system.cache.redis.port', 6379)); } $driver = new DoctrineCache\RedisCache(); $driver->setRedis($redis); break; default: $driver = new DoctrineCache\FilesystemCache($this->cache_dir); break; } return $driver; }
protected function _getCacheDriver() { $driver = new MemcacheCache(); $driver->setMemcache($this->memcache); return $driver; }
public function register(Application $app) { foreach ($this->getOrmDefaults() as $key => $value) { if (!isset($app[$key])) { $app[$key] = $value; } } $app['orm.em.default_options'] = array('connection' => 'default', 'mappings' => array(), 'types' => array()); $app['orm.ems.options.initializer'] = $app->protect(function () use($app) { static $initialized = false; if ($initialized) { return; } $initialized = true; if (!isset($app['orm.ems.options'])) { $app['orm.ems.options'] = array('default' => isset($app['orm.em.options']) ? $app['orm.em.options'] : array()); } $tmp = $app['orm.ems.options']; foreach ($tmp as $name => &$options) { $options = array_replace($app['orm.em.default_options'], $options); if (!isset($app['orm.ems.default'])) { $app['orm.ems.default'] = $name; } } $app['orm.ems.options'] = $tmp; }); $app['orm.em_name_from_param_key'] = $app->protect(function ($paramKey) use($app) { $app['orm.ems.options.initializer'](); if (isset($app[$paramKey])) { return $app[$paramKey]; } return $app['orm.ems.default']; }); $app['orm.ems'] = $app->share(function ($app) { $app['orm.ems.options.initializer'](); $ems = new \Pimple(); foreach ($app['orm.ems.options'] as $name => $options) { if ($app['orm.ems.default'] === $name) { // we use shortcuts here in case the default has been overridden $config = $app['orm.em.config']; } else { $config = $app['orm.ems.config'][$name]; } $ems[$name] = $app->share(function ($ems) use($app, $options, $config) { return EntityManager::create($app['dbs'][$options['connection']], $config, $app['dbs.event_manager'][$options['connection']]); }); } return $ems; }); $app['orm.ems.config'] = $app->share(function ($app) { $app['orm.ems.options.initializer'](); $configs = new \Pimple(); foreach ($app['orm.ems.options'] as $name => $options) { $config = new Configuration(); $app['orm.cache.configurer']($name, $config, $options); $config->setProxyDir($app['orm.proxies_dir']); $config->setProxyNamespace($app['orm.proxies_namespace']); $config->setAutoGenerateProxyClasses($app['orm.auto_generate_proxies']); $config->setCustomStringFunctions($app['orm.custom.functions.string']); $config->setCustomNumericFunctions($app['orm.custom.functions.numeric']); $config->setCustomDatetimeFunctions($app['orm.custom.functions.datetime']); $config->setCustomHydrationModes($app['orm.custom.hydration_modes']); $config->setClassMetadataFactoryName($app['orm.class_metadata_factory_name']); $config->setDefaultRepositoryClassName($app['orm.default_repository_class']); $config->setEntityListenerResolver($app['orm.entity_listener_resolver']); $config->setRepositoryFactory($app['orm.repository_factory']); $config->setNamingStrategy($app['orm.strategy.naming']); $config->setQuoteStrategy($app['orm.strategy.quote']); $chain = $app['orm.mapping_driver_chain.locator']($name); foreach ((array) $options['mappings'] as $entity) { if (!is_array($entity)) { throw new \InvalidArgumentException("The 'orm.em.options' option 'mappings' should be an array of arrays."); } if (!empty($entity['resources_namespace'])) { $entity['path'] = $app['psr0_resource_locator']->findFirstDirectory($entity['resources_namespace']); } if (isset($entity['alias'])) { $config->addEntityNamespace($entity['alias'], $entity['namespace']); } switch ($entity['type']) { case 'annotation': $config->newDefaultAnnotationDriver((array) $entity['path']); $driver = new MappingDriverChain(); $chain->addDriver(new AnnotationDriver(new AnnotationReader(), $entity['path']), $entity['namespace']); break; case 'yml': $driver = new YamlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_yml': $driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'simple_xml': $driver = new SimplifiedXmlDriver(array($entity['path'] => $entity['namespace'])); $chain->addDriver($driver, $entity['namespace']); break; case 'php': $driver = new StaticPHPDriver($entity['path']); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type'])); break; } } //$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); $config->setMetadataDriverImpl($chain); foreach ((array) $options['types'] as $typeName => $typeClass) { if (Type::hasType($typeName)) { Type::overrideType($typeName, $typeClass); } else { Type::addType($typeName, $typeClass); } } $configs[$name] = $config; } return $configs; }); $app['orm.cache.configurer'] = $app->protect(function ($name, Configuration $config, $options) use($app) { $config->setMetadataCacheImpl($app['orm.cache.locator']($name, 'metadata', $options)); $config->setQueryCacheImpl($app['orm.cache.locator']($name, 'query', $options)); $config->setResultCacheImpl($app['orm.cache.locator']($name, 'result', $options)); $config->setHydrationCacheImpl($app['orm.cache.locator']($name, 'hydration', $options)); }); $app['orm.cache.locator'] = $app->protect(function ($name, $cacheName, $options) use($app) { $cacheNameKey = $cacheName . '_cache'; if (!isset($options[$cacheNameKey])) { $options[$cacheNameKey] = $app['orm.default_cache']; } if (isset($options[$cacheNameKey]) && !is_array($options[$cacheNameKey])) { $options[$cacheNameKey] = array('driver' => $options[$cacheNameKey]); } if (!isset($options[$cacheNameKey]['driver'])) { throw new \RuntimeException("No driver specified for '{$cacheName}'"); } $driver = $options[$cacheNameKey]['driver']; $cacheInstanceKey = 'orm.cache.instances.' . $name . '.' . $cacheName; if (isset($app[$cacheInstanceKey])) { return $app[$cacheInstanceKey]; } $cache = $app['orm.cache.factory']($driver, $options[$cacheNameKey]); if (isset($options['cache_namespace']) && $cache instanceof CacheProvider) { $cache->setNamespace($options['cache_namespace']); } return $app[$cacheInstanceKey] = $cache; }); $app['orm.cache.factory.backing_memcache'] = $app->protect(function () { return new \Memcache(); }); $app['orm.cache.factory.memcache'] = $app->protect(function ($cacheOptions) use($app) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcache cache'); } $memcache = $app['orm.cache.factory.backing_memcache'](); $memcache->connect($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }); $app['orm.cache.factory.backing_memcached'] = $app->protect(function () { return new \Memcached(); }); $app['orm.cache.factory.memcached'] = $app->protect(function ($cacheOptions) use($app) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for memcached cache'); } $memcached = $app['orm.cache.factory.backing_memcached'](); $memcached->addServer($cacheOptions['host'], $cacheOptions['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }); $app['orm.cache.factory.backing_redis'] = $app->protect(function () { return new \Redis(); }); $app['orm.cache.factory.redis'] = $app->protect(function ($cacheOptions) use($app) { if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) { throw new \RuntimeException('Host and port options need to be specified for redis cache'); } $redis = $app['orm.cache.factory.backing_redis'](); $redis->connect($cacheOptions['host'], $cacheOptions['port']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; }); $app['orm.cache.factory.array'] = $app->protect(function () { return new ArrayCache(); }); $app['orm.cache.factory.apc'] = $app->protect(function () { return new ApcCache(); }); $app['orm.cache.factory.xcache'] = $app->protect(function () { return new XcacheCache(); }); $app['orm.cache.factory.filesystem'] = $app->protect(function ($cacheOptions) { if (empty($cacheOptions['path'])) { throw new \RuntimeException('FilesystemCache path not defined'); } return new FilesystemCache($cacheOptions['path']); }); $app['orm.cache.factory'] = $app->protect(function ($driver, $cacheOptions) use($app) { switch ($driver) { case 'array': return $app['orm.cache.factory.array'](); case 'apc': return $app['orm.cache.factory.apc'](); case 'xcache': return $app['orm.cache.factory.xcache'](); case 'memcache': return $app['orm.cache.factory.memcache']($cacheOptions); case 'memcached': return $app['orm.cache.factory.memcached']($cacheOptions); case 'filesystem': return $app['orm.cache.factory.filesystem']($cacheOptions); case 'redis': return $app['orm.cache.factory.redis']($cacheOptions); default: throw new \RuntimeException("Unsupported cache type '{$driver}' specified"); } }); $app['orm.mapping_driver_chain.locator'] = $app->protect(function ($name = null) use($app) { $app['orm.ems.options.initializer'](); if (null === $name) { $name = $app['orm.ems.default']; } $cacheInstanceKey = 'orm.mapping_driver_chain.instances.' . $name; if (isset($app[$cacheInstanceKey])) { return $app[$cacheInstanceKey]; } return $app[$cacheInstanceKey] = $app['orm.mapping_driver_chain.factory']($name); }); $app['orm.mapping_driver_chain.factory'] = $app->protect(function ($name) use($app) { return new MappingDriverChain(); }); $app['orm.add_mapping_driver'] = $app->protect(function (MappingDriver $mappingDriver, $namespace, $name = null) use($app) { $app['orm.ems.options.initializer'](); if (null === $name) { $name = $app['orm.ems.default']; } $driverChain = $app['orm.mapping_driver_chain.locator']($name); $driverChain->addDriver($mappingDriver, $namespace); }); $app['orm.generate_psr0_mapping'] = $app->protect(function ($resourceMapping) use($app) { $mapping = array(); foreach ($resourceMapping as $resourceNamespace => $entityNamespace) { $directory = $app['psr0_resource_locator']->findFirstDirectory($resourceNamespace); if (!$directory) { throw new \InvalidArgumentException("Resources for mapping '{$entityNamespace}' could not be located; Looked for mapping resources at '{$resourceNamespace}'"); } $mapping[$directory] = $entityNamespace; } return $mapping; }); $app['orm.strategy.naming'] = $app->share(function ($app) { return new DefaultNamingStrategy(); }); $app['orm.strategy.quote'] = $app->share(function ($app) { return new DefaultQuoteStrategy(); }); $app['orm.entity_listener_resolver'] = $app->share(function ($app) { return new DefaultEntityListenerResolver(); }); $app['orm.repository_factory'] = $app->share(function ($app) { return new DefaultRepositoryFactory(); }); $app['orm.em'] = $app->share(function ($app) { $ems = $app['orm.ems']; return $ems[$app['orm.ems.default']]; }); $app['orm.em.config'] = $app->share(function ($app) { $configs = $app['orm.ems.config']; return $configs[$app['orm.ems.default']]; }); }
protected function registerMemcacheCache(Config $config) { $config = $config->get('cache.drive.memcache.config'); $memcache = new \Memcache(); $memcache->addserver($config['host'], $config['port'], $config['timeout']); $memcacheCache = new MemcacheCache(); $memcacheCache->setMemcache($memcache); $this->cacheDrive = $memcacheCache; }
*/ use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\MemcacheCache; use Doctrine\Common\Cache\MemcachedCache; use Doctrine\Common\Cache\RedisCache; use Doctrine\Common\Cache\XcacheCache; use Doctrine\Common\Cache\FileCache; $config = $app->config->cache; if ($app->isDevelopment()) { $cache = new ArrayCache(); } else { if ($config['driver'] == 'memcache') { $memcache = new Memcache(); $memcache->connect($config['host'], $config['port']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); } else { if ($config['driver'] == 'memcached') { $memcached = new Memcached(); $memcached->addServer($config['host'], $config['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); } else { if ($config['driver'] == 'redis') { $redis = new Redis(); $redis->connect($config['host'], $config['port']); $cache = new RedisCache(); $cache->setRedis($redis); } else { $class = 'Doctrine\\Common\\Cache\\' . ucfirst($config['driver']) . 'Cache'; if (is_a($class, 'Doctrine\\Common\\Cache\\FileCache', true)) {
public function _memcache_init($options) { $memcache = new Memcache(); @($running = $memcache->connect($options['MEMCACHE_HOSTNAME'], $options['MEMCACHE_PORT'])); if (!$running) { return null; } $cacheDriver = new Cache\MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; }
/** * Create a memcached cache. * * @param array $options The memcached driver options * @param Memcache $memcache A Memcache object to be used by the cache * @return ArrayCache object */ public function createMemcachedCache(Memcache $memcache) { $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; }
/** * @param IConfiguration $applicationConfiguration */ public function init(IConfiguration $applicationConfiguration) { $this->router->parseExpressions(); $this->router->detectCurrentRoute($this->kernel->getRequest()->url(), $this->kernel->getServer()->getRequestMethod()); $this->kernel->getRequest()->assignExpressionValues($this->router); $ds = DIRECTORY_SEPARATOR; $proxyPath = $this->kernel->getServer()->getDocumentRoot() . "var" . $ds . "cache" . $ds . "Proxies"; $entityPath = $this->kernel->getServer()->getDocumentRoot() . "src"; if (!file_exists($proxyPath)) { mkdir($proxyPath, 0770, true); } $isDevMode = $applicationConfiguration->get("devmode") == true; $emConfig = Setup::createAnnotationMetadataConfiguration(array($entityPath), $isDevMode, $proxyPath); /*$emConfig->addCustomStringFunction("GROUP_CONCAT", "DoctrineExtensions\\Query\\Mysql\\GroupConcat"); $emConfig->addCustomStringFunction("DATE_FORMAT", "DoctrineExtensions\\Query\\Mysql\\DateFormat"); $emConfig->addCustomStringFunction("IFNULL", "DoctrineExtensions\\Query\\Mysql\\IfNull"); $emConfig->addCustomStringFunction("STR_TO_DATE", "DoctrineExtensions\\Query\\Mysql\\StrToDate"); $emConfig->addCustomStringFunction("CONCAT_WS", "DoctrineExtensions\\Query\\Mysql\\ConcatWs"); $emConfig->addCustomStringFunction("DATEDIFF", "DoctrineExtensions\\Query\\Mysql\\DateDiff"); $emConfig->addCustomStringFunction("MATCH_AGAINST", "DoctrineExtensions\\Query\\Mysql\\MatchAgainst"); $emConfig->addCustomStringFunction("REGEXP", "DoctrineExtensions\\Query\\Mysql\\Regexp"); $emConfig->addCustomStringFunction("IFELSE", "DoctrineExtensions\\Query\\Mysql\\IfElse");*/ if (!empty($applicationConfiguration->get("repository-factory"))) { $repositoryFactory = $applicationConfiguration->get("repository-factory"); $emConfig->setRepositoryFactory(new $repositoryFactory()); } if (!$isDevMode) { $memcache = new \Memcache(); $memcache->addServer("127.0.0.1", 11211); $cacheDriver = new MemcacheCache(); $cacheDriver->setMemcache($memcache); $cacheDriver->setNamespace($this->kernel->getServer()->getHttpHost()); $emConfig->setResultCacheImpl($cacheDriver); $emConfig->setQueryCacheImpl($cacheDriver); $emConfig->setMetadataCacheImpl($cacheDriver); } $conn = array('dbname' => $applicationConfiguration->get("dbname"), 'user' => $applicationConfiguration->get("dbuser"), 'password' => $applicationConfiguration->get("dbpassword"), 'host' => $applicationConfiguration->get("dbhost"), 'charset' => $applicationConfiguration->get("dbcharset"), 'driver' => $applicationConfiguration->get("dbdriver")); $em = $this->injector->getClassname("doctrine.entitymanager"); $this->kernel->setEntityManager($em::create($conn, $emConfig)); }
/** * @inheritdoc */ public function start() { parent::start(); $connectionConfig = $this->config('connection'); if (!empty($connectionConfig) && is_array($connectionConfig)) { // Setup Doctrine. Largely borrowed from // https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/annotations.md#em-setup // Register Doctrine default annotations. AnnotationRegistry::registerFile($this->getEngine()->getSiteInfo()->getSiteRoot() . '/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); // Setup annotation metadata cache if ($this->getEngine()->getEnvironmentInfo()->isDevMode() || !$this->getEngine()->config('memcache.enabled')) { $cache = new ArrayCache(); } else { $cache = new MemcacheCache(); $cache->setMemcache($this->getEngine()->getMemcache()); } // Setup annotation metadata reader and driver /** @var AnnotationReader $cachedAnnotationReader (for all intents and purposes...) */ $cachedAnnotationReader = new CachedReader(new AnnotationReader(), $cache); $this->driverChain = new MappingDriverChain(); $this->annotationDriver = new AnnotationDriver($cachedAnnotationReader, array($this->getEngine()->getApplicationInfo()->getSitegearRoot())); // Setup Gedmo extension annotations \Gedmo\DoctrineExtensions::registerAnnotations(); $this->driverChain->addDriver($this->annotationDriver, 'Gedmo'); // Setup Sitegear extension annotations // TODO Make model-providing modules declare their own namespaces $this->driverChain->addDriver($this->annotationDriver, 'Sitegear\\Module\\Customer\\Model'); $this->driverChain->addDriver($this->annotationDriver, 'Sitegear\\Module\\News\\Model'); $this->driverChain->addDriver($this->annotationDriver, 'Sitegear\\Module\\Locations\\Model'); $this->driverChain->addDriver($this->annotationDriver, 'Sitegear\\Module\\Products\\Model'); // Create the entity manager configuration, with proxy generation, cached metadata and lowercase-underscore // database naming convention. $entityManagerConfig = new Configuration(); // TODO Make this a temp directory set in the engine config $entityManagerConfig->setProxyDir(sys_get_temp_dir()); // TODO Configurable namespace and naming strategy $entityManagerConfig->setProxyNamespace('Proxy'); $entityManagerConfig->setAutoGenerateProxyClasses($this->getEngine()->getEnvironmentInfo()->isDevMode()); $entityManagerConfig->setMetadataDriverImpl($this->driverChain); $entityManagerConfig->setMetadataCacheImpl($cache); $entityManagerConfig->setQueryCacheImpl($cache); $entityManagerConfig->setNamingStrategy(new UnderscoreNamingStrategy(CASE_LOWER)); // Setup event subscribers. $eventManager = new EventManager(); foreach ($this->config('orm.subscribers') as $subscriberConfig) { /** @var \Doctrine\Common\EventSubscriber $subscriber */ $subscriber = TypeUtilities::buildTypeCheckedObject($subscriberConfig['class'], 'event subscriber', null, array('\\Doctrine\\Common\\EventSubscriber'), isset($subscriberConfig['arguments']) ? $subscriberConfig['arguments'] : array()); if ($subscriber instanceof MappedEventSubscriber) { /** @var MappedEventSubscriber $subscriber */ $subscriber->setAnnotationReader($cachedAnnotationReader); } $eventManager->addEventSubscriber($subscriber); } // Create the entity manager using the configured connection parameters. $this->entityManager = EntityManager::create($this->config('connection'), $entityManagerConfig, $eventManager); // Register the JSON custom data type. This has to be done last, when the entity manager has a connection. foreach ($this->config('dbal.types') as $key => $className) { Type::addType($key, $className); $this->entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping(preg_replace('/^.*\\\\(.*?)$/', '$1', $className), $key); } } else { throw new \DomainException('<h1>Incorrect or Missing Configuration</h1><p>You have attempted to use the Doctrine module in your site, but you have not provided all the required connection parameters in your configuration file.</p><p>Please rectify this by providing connection parameters ("driver", "dbname", plus normally "username" and "password") or disabling the Doctrine module.</p>'); } }
/** * Get memcache driver * * @return MemcacheCache|null */ private function getMemcache() { if (isset($this->configuration['memcache']) && isset($this->configuration['memcache']['host']) && isset($this->configuration['memcache']['port'])) { $memcache = new \Memcache(); $memcache->connect($this->configuration['memcache']['host'], $this->configuration['memcache']['port']); $cacheDriver = new MemcacheCache(); $cacheDriver->setMemcache($memcache); return $cacheDriver; } return null; }
public function loadDoctrineConfiguration(Application $app) { $app['doctrine.configuration'] = $app->share(function () use($app) { if (isset($app['doctrine.orm']) and true === $app['doctrine.orm']) { // Check available cache drivers if (extension_loaded('apc')) { $cache = new ApcCache(); } else { if (extension_loaded('xcache')) { $cache = new XcacheCache(); } else { if (extension_loaded('memcache')) { $memcache = new \Memcache(); $memcache->connect('127.0.0.1'); $cache = new MemcacheCache(); $cache->setMemcache($memcache); } else { $cache = new ArrayCache(); } } } $cache->setNamespace("dc2_"); // to avoid collisions $config = new ORMConfiguration(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $config->setResultCacheImpl($cache); $chain = new DriverChain(); foreach ((array) $app['doctrine.orm.entities'] as $entity) { switch ($entity['type']) { case 'annotation': $reader = new AnnotationReader(); $driver = new AnnotationDriver($reader, (array) $entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'yml': $driver = new YamlDriver((array) $entity['path']); $driver->setFileExtension('.yml'); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver((array) $entity['path'], $entity['namespace']); $driver->setFileExtension('.xml'); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type'])); break; } } $config->setMetadataDriverImpl($chain); $config->setProxyDir($app['doctrine.orm.proxies_dir']); $config->setProxyNamespace($app['doctrine.orm.proxies_namespace']); $config->setAutoGenerateProxyClasses($app['doctrine.orm.auto_generate_proxies']); } else { $config = new DBALConfiguration(); } return $config; }); }