Skip to content

Commit

Permalink
Serializer Adapter JMS and default Symfony
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Janssen committed Jun 5, 2015
1 parent 216b227 commit 0d66837
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,20 @@ private function createClientsNode()
->thenInvalid('Class %s is missing. Did you forget to add guzzlehttp/services to your project\'s composer.json?')
->end()
->end()
->arrayNode('serializer')
->children()
->scalarNode('type')->defaultValue('symfony')
->validate()
->ifNotInArray(['symfony', 'jms'])
->thenInvalid('Invalid serializer adapter')
->end()
->end()
->scalarNode('service')->defaultValue('serialzer')->end()
->end()
->end()
->end()
->end()
->end()
;

return $node;
Expand Down
13 changes: 11 additions & 2 deletions src/DependencyInjection/CsaGuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,17 @@ private function processClientsConfiguration(array $config, ContainerBuilder $co
$name
)));
$serviceDefinition->addArgument(['process' => false]);
$serviceDefinition->addMethodCall('setSerializer', [new Reference('serializer')]);
$serviceDefinition->addMethodCall('addProcess');
if (isset($options['serializer']) && is_array($options['serializer'])) {
if ($options['serializer']['type'] === 'jms') {
$jmsAdapter = new Definition('Csa\Bundle\GuzzleBundle\Serializer\JMSAdapter');
$jmsAdapter->addArgument(new Reference($options['serializer']['service']));
$container->setDefinition('csa_guzzle.adapter.jms', $jmsAdapter);
$options['serializer']['service'] = 'csa_guzzle.adapter.jms';

}
$serviceDefinition->addMethodCall('setSerializer', [new Reference($options['serializer']['service'])]);
$serviceDefinition->addMethodCall('addProcess');
}

$container->setDefinition(sprintf('csa_guzzle.service.%s', $name), $serviceDefinition);
$container->getDefinition('csa_guzzle.paramconverter.guzzle')->addMethodCall(
Expand Down
1 change: 1 addition & 0 deletions src/GuzzleHttp/Cache/DoctrineAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(Cache $cache, $ttl = 0)

public function fetch(RequestInterface $request)
{

$key = $this->getKey($request);

if ($this->cache->contains($key)) {
Expand Down
4 changes: 2 additions & 2 deletions src/GuzzleHttp/Command/Guzzle/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Csa\Bundle\GuzzleBundle\GuzzleHttp\Command\Guzzle\Subscriber\ProcessResponse;
use GuzzleHttp\Command\Guzzle\GuzzleClient as AbstractGuzzleClient;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\Serializer\SerializerInterface;

class GuzzleClient extends AbstractGuzzleClient
{
Expand All @@ -20,7 +20,7 @@ class GuzzleClient extends AbstractGuzzleClient
protected $serializer;

/**
* @param SerializerInterface $serializer
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
*/
public function setSerializer(SerializerInterface $serializer)
{
Expand Down
8 changes: 4 additions & 4 deletions src/GuzzleHttp/Command/Guzzle/Subscriber/ProcessResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use GuzzleHttp\Command\Guzzle\ResponseLocation\ReasonPhraseLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\HeaderLocation;
use GuzzleHttp\Command\Guzzle\ResponseLocation\XmlLocation;
use JMS\Serializer\SerializerInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* Class ProcessResponse
Expand All @@ -40,9 +40,9 @@ class ProcessResponse implements SubscriberInterface
private $serializer;

/**
* @param \GuzzleHttp\Command\Guzzle\DescriptionInterface $description
* @param \JMS\Serializer\SerializerInterface $serializer
* @param array $responseLocations
* @param \GuzzleHttp\Command\Guzzle\DescriptionInterface $description
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
* @param array $responseLocations
*/
public function __construct(
DescriptionInterface $description,
Expand Down
5 changes: 5 additions & 0 deletions src/GuzzleHttp/Subscriber/CacheSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use GuzzleHttp\Event\CompleteEvent;
use GuzzleHttp\Event\RequestEvents;
use GuzzleHttp\Event\SubscriberInterface;
use GuzzleHttp\Message\MessageFactory;
use GuzzleHttp\Message\MessageParser;
use GuzzleHttp\Message\Response;

/**
* Csa Guzzle Cache integration
Expand Down Expand Up @@ -48,6 +51,8 @@ public function onBefore(BeforeEvent $event)

return;
}
$messageFactory = new MessageFactory();
$response = $messageFactory->fromMessage($response);

$request->getConfig()->set('cache_lookup', 'HIT');
$request->getConfig()->set('cache_hit', true);
Expand Down
62 changes: 62 additions & 0 deletions src/Serializer/JMSAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Created by PhpStorm.
* User: AJanssen
* Date: 05-06-15
* Time: 15:22
*/

namespace Csa\Bundle\GuzzleBundle\Serializer;


use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializationContext;
use Symfony\Component\Serializer\SerializerInterface;
use \JMS\Serializer\SerializerInterface as JMSSerializerInterface;

class JMSAdapter implements SerializerInterface
{
/**
* @var JMSSerializerInterface
*/
private $serializer;

/**
* @param \JMS\Serializer\SerializerInterface $serializer
*/
public function __construct(JMSSerializerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* Serializes data in the appropriate format.
*
* @param mixed $data any data
* @param string $format format name
* @param array $context options normalizers/encoders have access to
*
* @return string
*/
public function serialize($data, $format, array $context = [])
{
return $this->serializer->serialize($data, $format, SerializationContext::create($context));
}

/**
* Deserializes data into the given type.
*
* @param mixed $data
* @param string $type
* @param string $format
* @param array $context
*
* @return object
*/
public function deserialize($data, $type, $format, array $context = [])
{
return $this->serializer->deserialize($data, $type, $format, DeserializationContext::create($context));
}


}

0 comments on commit 0d66837

Please sign in to comment.