Skip to content

Commit

Permalink
Merge pull request #58 from KnpLabs/refactoring/remove-serializable-d…
Browse files Browse the repository at this point in the history
…ictionaries

Dictionaries are not serializable anymore
  • Loading branch information
Antoine Lelaisant authored Oct 5, 2017
2 parents 8f898bc + da4ca09 commit eb977f7
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,43 @@

namespace spec\Knp\DictionaryBundle\DataCollector;

use Knp\DictionaryBundle\Dictionary\DictionaryRegistry;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DictionaryDataCollectorSpec extends ObjectBehavior
{
function let(DictionaryRegistry $registry)
{
$this->beConstructedWith($registry);
}

function it_is_initializable()
{
$this->shouldHaveType('Knp\DictionaryBundle\DataCollector\DictionaryDataCollector');
}

function it_collects_data_from_dictionaries(Request $request, Response $response, $registry)
function it_collects_data_from_dictionaries()
{
$registry->all()->willReturn(['foo', 'bar', 'baz']);
$this->collect($request, $response);
$this->addDictionary('foo', ['key1', 'key2'], ['value1', 'value2']);
$this->addDictionary('foo', ['key1', 'key2'], ['value1', 'value2']);
$this->addDictionary('bar', ['keyA', 'keyB'], ['valueA', 'valueB']);

$this->getDictionaries()->shouldReturn(['foo', 'bar', 'baz']);
$this->getDictionaries()->shouldReturn([
'foo' => [
[
'key' => 'key1',
'value' => 'value1',
],
[
'key' => 'key2',
'value' => 'value2',
],
],
'bar' => [
[
'key' => 'keyA',
'value' => 'valueA',
],
[
'key' => 'keyB',
'value' => 'valueB',
],
],
]);
}

function it_has_a_name()
Expand Down
11 changes: 1 addition & 10 deletions spec/Knp/DictionaryBundle/Dictionary/CallableDictionarySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Knp\DictionaryBundle\Dictionary;

use Knp\DictionaryBundle\Dictionary;
use PhpSpec\ObjectBehavior;

class CallableDictionarySpec extends ObjectBehavior
Expand Down Expand Up @@ -46,16 +47,6 @@ function it_supports_some_array_access_functions()
expect(isset($dictionary['foo']))->toBe(false);
}

function it_can_be_serialized_and_unserialized()
{
$dictionary = $this->getWrappedObject();

$dictionary = unserialize(serialize($dictionary));

expect($dictionary['foo'])->toBe(0);
expect(isset($dictionary['foo']))->toBe(true);
}

function it_provides_a_set_of_values()
{
$this->getValues()->shouldReturn([
Expand Down
84 changes: 84 additions & 0 deletions spec/Knp/DictionaryBundle/Dictionary/TraceableDictionarySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace spec\Knp\DictionaryBundle\Dictionary;

use Knp\DictionaryBundle\DataCollector\DictionaryDataCollector;
use Knp\DictionaryBundle\Dictionary\SimpleDictionary;
use PhpSpec\ObjectBehavior;

class TraceableDictionarySpec extends ObjectBehavior
{
function let(DictionaryDataCollector $collector)
{
$dictionary = new SimpleDictionary('name', [
'foo' => 'bar',
'baz' => null,
]);

$this->beConstructedWith($dictionary, $collector);
}

function it_is_initializable()
{
$this->shouldHaveType('Knp\DictionaryBundle\Dictionary\TraceableDictionary');
}

function it_is_a_dictionary()
{
$this->shouldImplement('Knp\DictionaryBundle\Dictionary');
}

function it_has_a_name()
{
$this->getName()->shouldReturn('name');
}

function it_traces_keys($collector)
{
$collector->addDictionary('name', ['foo', 'baz'], ['bar', null])->shouldbeCalled();

$this->getKeys()->shouldReturn(['foo', 'baz']);
}

function it_traces_values($collector)
{
$collector->addDictionary('name', ['foo', 'baz'], ['bar', null])->shouldbeCalled();

$this->getValues()->shouldReturn(['foo' => 'bar', 'baz' => null]);
}

function it_traces_values_get($collector)
{
$collector->addDictionary('name', ['foo', 'baz'], ['bar', null])->shouldbeCalled();

$this['foo']->shouldReturn('bar');
}

function it_traces_value_set($collector)
{
$collector->addDictionary('name', ['foo', 'baz', 'yo'], ['bar', null, 'lo'])->shouldbeCalled();

$this['yo'] = 'lo';
}

function it_traces_value_unset($collector)
{
$collector->addDictionary('name', ['baz'], [null])->shouldbeCalled();

unset($this['foo']);
}

function it_traces_key_exists($collector)
{
$collector->addDictionary('name', ['foo', 'baz'], ['bar', null])->shouldbeCalled();

expect(isset($this['baz']))->toBe(true);
}

function it_trace_iteration($collector)
{
$collector->addDictionary('name', ['foo', 'baz'], ['bar', null])->shouldbeCalled();

expect(iterator_to_array($this->getIterator()->getWrappedObject()))->toBe(['foo' => 'bar', 'baz' => null]);
}
}
5 changes: 5 additions & 0 deletions spec/Knp/DictionaryBundle/KnpDictionaryBundleSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ function it_registers_compiler_passes(ContainerBuilder $container)
->shouldBeCalled()
;

$container
->addCompilerPass(Argument::type('Knp\DictionaryBundle\DependencyInjection\Compiler\DictionaryTracePass'))
->shouldBeCalled()
;

$this->build($container);
}
}
22 changes: 7 additions & 15 deletions src/Knp/DictionaryBundle/DataCollector/DictionaryDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,28 @@

namespace Knp\DictionaryBundle\DataCollector;

use Knp\DictionaryBundle\Dictionary\DictionaryRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class DictionaryDataCollector extends DataCollector
{
/**
* @var DictionaryRegistry
*/
private $registry;

/**
* @param DictionaryRegistry $registry
* {@inheritdoc}
*/
public function __construct(DictionaryRegistry $registry)
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$this->registry = $registry;
}

/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
public function addDictionary($name, array $keys, array $values)
{
$this->data = $this->registry->all();
$this->data[$name] = array_map(function ($key, $value) {
return ['key' => $key, 'value' => $value];
}, $keys, $values);
}

/**
* @return Dictionary[]
* @return array
*/
public function getDictionaries()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Knp\DictionaryBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class DictionaryTracePass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (false === $container->has('knp_dictionary.data_collector.dictionary_data_collector')) {
return;
}

$collector = new Reference('knp_dictionary.data_collector.dictionary_data_collector');
$services = $container->findTaggedServiceIds(DictionaryRegistrationPass::TAG_DICTIONARY);

foreach ($services as $id => $tags) {
$serviceId = sprintf('%s.%s.traceable', $id, md5($id));

$dictionary = new Reference(sprintf('%s.inner', $serviceId));

$traceable = new Definition('Knp\DictionaryBundle\Dictionary\TraceableDictionary', [$dictionary, $collector]);
$traceable->setDecoratedService($id);

$container->setDefinition($serviceId, $traceable);
}
}
}
5 changes: 4 additions & 1 deletion src/Knp/DictionaryBundle/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Knp\DictionaryBundle;

interface Dictionary extends \ArrayAccess, \IteratorAggregate, \Serializable
use ArrayAccess;
use IteratorAggregate;

interface Dictionary extends ArrayAccess, IteratorAggregate
{
const VALUE = 'value';
const VALUE_AS_KEY = 'value_as_key';
Expand Down
28 changes: 2 additions & 26 deletions src/Knp/DictionaryBundle/Dictionary/CallableDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Knp\DictionaryBundle\Dictionary;

use Knp\DictionaryBundle\Dictionary as DictionaryInterface;
use Knp\DictionaryBundle\Dictionary;

class CallableDictionary implements DictionaryInterface
class CallableDictionary implements Dictionary
{
/**
* @var string
Expand Down Expand Up @@ -111,30 +111,6 @@ public function getIterator()
return new \ArrayIterator($this->values);
}

/**
* {@inheritdoc}
*/
public function serialize()
{
$this->hydrate();

return serialize([
'name' => $this->name,
'values' => $this->values,
]);
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);

$this->name = $data['name'];
$this->values = $data['values'];
}

/**
* Hydrate values from callable.
*/
Expand Down
22 changes: 0 additions & 22 deletions src/Knp/DictionaryBundle/Dictionary/SimpleDictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,4 @@ public function getIterator()
{
return new \ArrayIterator($this->values);
}

/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize([
'name' => $this->name,
'values' => $this->values,
]);
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);

$this->name = $data['name'];
$this->values = $data['values'];
}
}
Loading

0 comments on commit eb977f7

Please sign in to comment.