Skip to content

Commit

Permalink
Add Serialization option (#411)
Browse files Browse the repository at this point in the history
* Add Serialization option

Serialization option is necessary for setting the type of serialization
to use for redis.

This is simply a new commit with all the code taken from PR#199 using
the latest code of SncRedisBundle on branch 2.1

* Minor code style update. Remove HHVM support

* Switch to using Redis Class Constants instead of hard-coded values

* Fix IGBINARY Dependency

* Remove ?? to support php5

(cherry picked from commit 5fca27f)
  • Loading branch information
yellow1912 authored and curry684 committed Apr 17, 2018
1 parent 553ab06 commit afaa0c4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ private function addClientsSection(ArrayNodeDefinition $rootNode)
->scalarNode('read_write_timeout')->defaultNull()->end()
->booleanNode('iterable_multibulk')->defaultFalse()->end()
->booleanNode('throw_errors')->defaultTrue()->end()
->scalarNode('serialization')->defaultValue('default')->end()
->scalarNode('profile')->defaultValue('default')
->end()
->scalarNode('cluster')->defaultNull()->end()
Expand Down
42 changes: 41 additions & 1 deletion DependencyInjection/SncRedisExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ protected function loadPhpredisClient(array $client, ContainerBuilder $container
if (null !== $dsn->getDatabase()) {
$phpredisDef->addMethodCall('select', array($dsn->getDatabase()));
}
if ($client['options']['serialization']) {
$phpredisDef->addMethodCall(
'setOption',
array(\Redis::OPT_SERIALIZER, $this->loadSerializationType($client['options']['serialization']))
);
}
$container->setDefinition($phpredisId, $phpredisDef);

$container->setAlias(sprintf('snc_redis.%s', $client['alias']), $phpredisId);
Expand Down Expand Up @@ -431,7 +437,41 @@ protected function loadSwiftMailer(array $config, ContainerBuilder $container)
}

/**
* Loads the profiler storage configuration.
* Load the correct serializer for Redis
*
* @param string $type
*
* @return string
* @throws InvalidConfigurationException
*/
public function loadSerializationType($type)
{
$types = array(
'none' => \Redis::SERIALIZER_NONE,
'php' => \Redis::SERIALIZER_PHP
);

if (defined('Redis::SERIALIZER_IGBINARY')) {
$types['igbinary'] = \Redis::SERIALIZER_IGBINARY;
}

// allow user to pass in default serialization in which case we should automatically decide for them
if ('default' == $type) {
return isset($types['igbinary']) ? $types['igbinary'] : $types['php'];
} elseif (array_key_exists($type, $types)) {
return $types[$type];
}

throw new InvalidConfigurationException(
sprintf(
'%s in not a valid serializer. Valid serializers: %s',
$type,
implode(", ", array_keys($types))
)
);
}

/* Loads the profiler storage configuration.
*
* @param array $config A configuration array
* @param ContainerBuilder $container A ContainerBuilder instance
Expand Down
10 changes: 10 additions & 0 deletions Resources/config/schema/redis-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,22 @@
<xsd:attribute name="read-write-timeout" type="xsd:int" />
<xsd:attribute name="iterable-multibulk" type="xsd:boolean" />
<xsd:attribute name="throw-errors" type="xsd:boolean" />
<xsd:attribute name="serialization" type="phpredis-serialization-type" />
<xsd:attribute name="profile" type="client-profile" />
<xsd:attribute name="cluster" type="xsd:string" />
<xsd:attribute name="prefix" type="xsd:string" />
<xsd:attribute name="replication" type="xsd:boolean" />
</xsd:complexType>

<xsd:simpleType name="phpredis-serialization-type">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="default" />
<xsd:enumeration value="none" />
<xsd:enumeration value="php" />
<xsd:enumeration value="igbinary" />
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="client-profile">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="1.2" />
Expand Down
46 changes: 46 additions & 0 deletions Tests/DependencyInjection/SncRedisExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,37 @@ public function testClientReplicationOption()
$this->assertEquals(array('snc_redis.default' => array(array('alias' => 'default'))), $container->findTaggedServiceIds('snc_redis.client'));
}

/**
* Test valid config of the serialization option
*/
public function testClientSerializationOption()
{
$extension = new SncRedisExtension();
$config = $this->parseYaml($this->getSerializationYamlConfig());
$extension->load(array($config), $container = $this->getContainer());
$options = $container->getDefinition('snc_redis.client.default_options')->getArgument(0);
$parameters = $container->getDefinition('snc_redis.default')->getArgument(0);
$masterParameters = $container->getDefinition((string) $parameters[0])->getArgument(0);
$this->assertSame($options['serialization'], $masterParameters['serialization']);
}

/**
* Test validity of serialization type
*/
public function testLoadSerializationType()
{
$extension = new SncRedisExtension();
$config = $this->parseYaml($this->getSerializationYamlConfig());
$extension->load(array($config), $container = $this->getContainer());
$options = $container->getDefinition('snc_redis.client.default_options')->getArgument(0);
$serializationType = $extension->loadSerializationType($options['serialization']);
$this->assertTrue(is_integer($serializationType));

$defaultType = defined('Redis::SERIALIZER_IGBINARY') ? 2 : 1;

$this->assertEquals($defaultType, $serializationType);
}

/**
* Test valid config of the sentinel replication option
*/
Expand Down Expand Up @@ -372,6 +403,21 @@ private function parseYaml($yaml)
return $parser->parse($yaml);
}

private function getSerializationYamlConfig()
{
return <<<'EOF'
clients:
default:
type: predis
alias: default
dsn:
- redis://localhost?alias=master
- redis://otherhost
options:
serialization: "default"
EOF;
}

private function getMinimalYamlConfig()
{
return <<<'EOF'
Expand Down

0 comments on commit afaa0c4

Please sign in to comment.