/** * @dataProvider dsnValues * * @param string $dsn * @param string $host * @param int|null $port * @param string|null $password * @param int|null $weight */ public function testDsn($dsn, $host, $port = null, $password = null, $weight = null) { $dsn = new MemcachedDsn($dsn); if (substr($host, -5) === '.sock') { $this->assertSame($host, $dsn->getSocket()); } else { $this->assertSame($host, $dsn->getHost()); } $this->assertSame($port, $dsn->getPort()); $this->assertSame($password, $dsn->getPassword()); $this->assertSame($weight, $dsn->getWeight()); }
/** * Loads a Memcached config. * * @param string $id * @param array $config A config configuration * @param ContainerBuilder $container A ContainerBuilder instance * * @throws \LogicException When Memcached extension is not loaded * * @return Definition */ protected function loadMemcachedDriver($id, array $config, ContainerBuilder $container) { // Check if the Memcached extension is loaded if (!extension_loaded('memcached')) { throw new \LogicException('Memcached extension is not loaded'); } $dsn = new MemcachedDsn($config['dsn']); $client = new Definition($container->getParameter('tree_house_cache.memcached_client.class')); $client->addArgument($id); $client->addMethodCall('addServer', [$dsn->getSocket() ?: $dsn->getHost(), $dsn->getPort(), $dsn->getWeight()]); if ($config['connection']['timeout']) { $client->addMethodCall('setOption', [\Memcached::OPT_CONNECT_TIMEOUT, $config['connection']['timeout']]); } if ($config['prefix']) { $client->addMethodCall('setOption', [\Memcached::OPT_PREFIX_KEY, $config['prefix']]); } $container->setDefinition(sprintf('%s.cache', $id), $client); $driver = new Definition($container->getParameter('tree_house_cache.memcached_driver.class')); $driver->addArgument($client); return $driver; }