Skip to content

Commit

Permalink
Adding static back in
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptiklemur committed Jan 26, 2016
1 parent 4de23aa commit aa8d4b3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
11 changes: 8 additions & 3 deletions src/DSN.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public function getHosts()
return $this->hosts;
}

public function getFirstHost()
{
return $this->hosts[0];
}

/**
* @return array
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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];
}
}
}
Expand Down Expand Up @@ -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':
Expand Down
14 changes: 7 additions & 7 deletions src/Factory/AbstractAdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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'])) {
Expand All @@ -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)
{
}
}
27 changes: 24 additions & 3 deletions src/Factory/AbstractDsnFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ abstract class AbstractDsnFactory extends AbstractAdapterFactory
*/
private $DSN;

/**
* @return DSN
*/
protected function getDsn()
{
return $this->DSN;
Expand All @@ -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);

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Factory/AdapterFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/Factory/RedisFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Cache\AdapterBundle\Factory;

use Cache\Adapter\Redis\RedisCachePool;
use Cache\AdapterBundle\DSN;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -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);
Expand Down

0 comments on commit aa8d4b3

Please sign in to comment.