diff --git a/src/DSN.php b/src/DSN.php index e733136..a128c62 100644 --- a/src/DSN.php +++ b/src/DSN.php @@ -104,6 +104,11 @@ public function getHosts() return $this->hosts; } + public function getFirstHost() + { + return $this->hosts[0]; + } + /** * @return array */ @@ -177,7 +182,7 @@ private function parseDsn($dsn) $dsn = substr($dsn, $pos + 1); $auth = []; - if (sizeof($temp) === 2) { + if (count($temp) === 2) { $auth['username'] = $temp[0]; $auth['password'] = $temp[1]; } else { @@ -197,7 +202,7 @@ private function parseDsn($dsn) // parse parameters if (preg_match('#^(.*)/(\d+)$#', $params, $matches)) { // parse database - $this->database = (int)$matches[2]; + $this->database = (int) $matches[2]; } } } @@ -228,7 +233,7 @@ protected function parseParameters($matches) switch ($kv[0]) { case 'weight': if ($kv[1]) { - $this->weight = (int)$kv[1]; + $this->weight = (int) $kv[1]; } break; case 'alias': diff --git a/src/Factory/AbstractAdapterFactory.php b/src/Factory/AbstractAdapterFactory.php index 9de4976..8585ea3 100644 --- a/src/Factory/AbstractAdapterFactory.php +++ b/src/Factory/AbstractAdapterFactory.php @@ -40,7 +40,7 @@ public function createAdapter(array $options = []) $this->verifyDependencies(); $resolver = new OptionsResolver(); - $this->configureOptionResolver($resolver); + static::configureOptionResolver($resolver); $config = $resolver->resolve($options); return $this->getAdapter($config); @@ -49,12 +49,12 @@ public function createAdapter(array $options = []) /** * {@inheritdoc} */ - public function validate(array $options, $adapterName) + public static function validate(array $options, $adapterName) { - $this->verifyDependencies(); + static::verifyDependencies(); $resolver = new OptionsResolver(); - $this->configureOptionResolver($resolver); + static::configureOptionResolver($resolver); try { $resolver->resolve($options); @@ -75,7 +75,7 @@ public function validate(array $options, $adapterName) * * @throws \LogicException */ - protected function verifyDependencies() + protected static function verifyDependencies() { foreach (static::$dependencies as $dependency) { if (!class_exists($dependency['requiredClass'])) { @@ -91,12 +91,12 @@ protected function verifyDependencies() } /** - * By default we do not have any options to configure. A factory should override this function and configure + * By default we do not have any options to configure. A factory should override this function and confgure * the options resolver. * * @param OptionsResolver $resolver */ - protected function configureOptionResolver(OptionsResolver $resolver) + protected static function configureOptionResolver(OptionsResolver $resolver) { } } diff --git a/src/Factory/AbstractDsnFactory.php b/src/Factory/AbstractDsnFactory.php index 8e35f0b..38cb8b4 100644 --- a/src/Factory/AbstractDsnFactory.php +++ b/src/Factory/AbstractDsnFactory.php @@ -24,6 +24,9 @@ abstract class AbstractDsnFactory extends AbstractAdapterFactory */ private $DSN; + /** + * @return DSN + */ protected function getDsn() { return $this->DSN; @@ -32,13 +35,16 @@ protected function getDsn() /** * {@inheritdoc} */ - protected function configureOptionResolver(OptionsResolver $resolver) + protected static function configureOptionResolver(OptionsResolver $resolver) { $resolver->setDefaults(['dsn' => '']); $resolver->setAllowedTypes('dsn', ['string']); } - public function validate(array $options, $adapterName) + /** + * {@inheritdoc} + */ + public static function validate(array $options, $adapterName) { parent::validate($options, $adapterName); @@ -50,7 +56,22 @@ public function validate(array $options, $adapterName) if (!$dsn->isValid()) { throw new \InvalidArgumentException('Invalid DSN: '.$options['dsn']); } + } + + /** + * {@inheritdoc} + */ + public function createAdapter(array $options = []) + { + if (!empty($options['dsn'])) { + $dsn = new DSN($options['dsn']); + if (!$dsn->isValid()) { + throw new \InvalidArgumentException('Invalid DSN: '.$options['dsn']); + } + + $this->DSN = $dsn; + } - $this->DSN = $dsn; + return parent::createAdapter($options); } } diff --git a/src/Factory/AdapterFactoryInterface.php b/src/Factory/AdapterFactoryInterface.php index 213e8a0..252b15d 100644 --- a/src/Factory/AdapterFactoryInterface.php +++ b/src/Factory/AdapterFactoryInterface.php @@ -39,5 +39,5 @@ public function createAdapter(array $options = []); * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException If a lazy option reads an unavailable option * @throws \Symfony\Component\OptionsResolver\Exception\AccessException If called from a lazy option or normalizer */ - public function validate(array $options, $adapterName); + public static function validate(array $options, $adapterName); } diff --git a/src/Factory/RedisFactory.php b/src/Factory/RedisFactory.php index d994229..db72064 100644 --- a/src/Factory/RedisFactory.php +++ b/src/Factory/RedisFactory.php @@ -12,7 +12,6 @@ namespace Cache\AdapterBundle\Factory; use Cache\Adapter\Redis\RedisCachePool; -use Cache\AdapterBundle\DSN; use Symfony\Component\OptionsResolver\OptionsResolver; /** @@ -39,7 +38,8 @@ public function getAdapter(array $config) $client->auth($dsn->getPassword()); } - $client->connect($dsn->getHost(), $dsn->getPort()); + $host = $dsn->getFirstHost(); + $client->connect($host['host'], $host['port']); } return new RedisCachePool($client);