diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3d0decc0..3f48e26f 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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; diff --git a/src/DependencyInjection/CsaGuzzleExtension.php b/src/DependencyInjection/CsaGuzzleExtension.php index adfb643b..dbe5c62f 100644 --- a/src/DependencyInjection/CsaGuzzleExtension.php +++ b/src/DependencyInjection/CsaGuzzleExtension.php @@ -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( diff --git a/src/GuzzleHttp/Cache/DoctrineAdapter.php b/src/GuzzleHttp/Cache/DoctrineAdapter.php index 20ec876b..e3a58c39 100644 --- a/src/GuzzleHttp/Cache/DoctrineAdapter.php +++ b/src/GuzzleHttp/Cache/DoctrineAdapter.php @@ -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)) { diff --git a/src/GuzzleHttp/Command/Guzzle/GuzzleClient.php b/src/GuzzleHttp/Command/Guzzle/GuzzleClient.php index c55d9658..c45dc1b7 100644 --- a/src/GuzzleHttp/Command/Guzzle/GuzzleClient.php +++ b/src/GuzzleHttp/Command/Guzzle/GuzzleClient.php @@ -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 { @@ -20,7 +20,7 @@ class GuzzleClient extends AbstractGuzzleClient protected $serializer; /** - * @param SerializerInterface $serializer + * @param \Symfony\Component\Serializer\SerializerInterface $serializer */ public function setSerializer(SerializerInterface $serializer) { diff --git a/src/GuzzleHttp/Command/Guzzle/Subscriber/ProcessResponse.php b/src/GuzzleHttp/Command/Guzzle/Subscriber/ProcessResponse.php index c6f178a1..669caaa6 100644 --- a/src/GuzzleHttp/Command/Guzzle/Subscriber/ProcessResponse.php +++ b/src/GuzzleHttp/Command/Guzzle/Subscriber/ProcessResponse.php @@ -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 @@ -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, diff --git a/src/GuzzleHttp/Subscriber/CacheSubscriber.php b/src/GuzzleHttp/Subscriber/CacheSubscriber.php index 9f4997c2..bac840a8 100644 --- a/src/GuzzleHttp/Subscriber/CacheSubscriber.php +++ b/src/GuzzleHttp/Subscriber/CacheSubscriber.php @@ -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 @@ -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); diff --git a/src/Serializer/JMSAdapter.php b/src/Serializer/JMSAdapter.php new file mode 100644 index 00000000..7dbc8249 --- /dev/null +++ b/src/Serializer/JMSAdapter.php @@ -0,0 +1,62 @@ +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)); + } + + +} \ No newline at end of file