Skip to content

Commit

Permalink
Added a ViewParameters ResponseTagger
Browse files Browse the repository at this point in the history
Tags the response with any Repository Value Object added to the response.
  • Loading branch information
Bertrand Dunogier authored and andrerom committed Nov 17, 2017
1 parent 0b89356 commit f4702f3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
49 changes: 49 additions & 0 deletions spec/ResponseTagger/Delegator/ViewParametersTaggerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace spec\EzSystems\PlatformHttpCacheBundle\ResponseTagger\Delegator;

use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\Core\MVC\Symfony\View\View;
use EzSystems\PlatformHttpCacheBundle\ResponseConfigurator\ResponseCacheConfigurator;
use EzSystems\PlatformHttpCacheBundle\ResponseTagger\Delegator\ViewParametersTagger;
use EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use stdClass;
use Symfony\Component\HttpFoundation\Response;

class ViewParametersTaggerSpec extends ObjectBehavior
{
function let(ResponseTagger $dispatcherTagger)
{
$this->beConstructedWith($dispatcherTagger);
}

function it_is_initializable()
{
$this->shouldHaveType(ViewParametersTagger::class);
}

function it_delegates_tagging_of_parameters_that_are_value_objects(
ResponseCacheConfigurator $configurator,
Response $response,
ResponseTagger $dispatcherTagger,
View $view,
ValueObject $someValueObject,
stdClass $someObject
) {
$view->getParameters()->willReturn([
'value_object' => $someValueObject,
'object' => $someObject,
'string' => 'some_string',
'array' => ['a', 'b', 'c'],
]);

$this->tag($configurator, $response, $view);

$dispatcherTagger->tag($configurator, $response, $someValueObject)->shouldHaveBeenCalled();
$dispatcherTagger->tag($configurator, $response, $someObject)->shouldNotHaveBeenCalled();
$dispatcherTagger->tag($configurator, $response, 'some_string')->shouldNotHaveBeenCalled();
$dispatcherTagger->tag($configurator, $response, ['a', 'b', 'c'])->shouldNotHaveBeenCalled();
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/view_cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ services:
tags:
- {name: ezplatform.cache_response_tagger}

ezplatform.view_cache.response_tagger.view_parameters:
class: EzSystems\PlatformHttpCacheBundle\ResponseTagger\Delegator\ViewParametersTagger
arguments: ['@ezplatform.view_cache.response_tagger.dispatcher']
tags:
- {name: ezplatform.cache_response_tagger}

ezplatform.view_cache.response_tagger.content_info:
class: EzSystems\PlatformHttpCacheBundle\ResponseTagger\Value\ContentInfoTagger
tags:
Expand Down
42 changes: 42 additions & 0 deletions src/ResponseTagger/Delegator/ViewParametersTagger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\ResponseTagger\Delegator;

use eZ\Publish\API\Repository\Values\ValueObject;
use eZ\Publish\Core\MVC\Symfony\View\View;
use EzSystems\PlatformHttpCacheBundle\ResponseConfigurator\ResponseCacheConfigurator;
use EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger;
use Symfony\Component\HttpFoundation\Response;

class ViewParametersTagger implements ResponseTagger
{
/**
* @var \EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger
*/
private $dispatcherTagger;

public function __construct(ResponseTagger $dispatcherTagger)
{
$this->dispatcherTagger = $dispatcherTagger;
}

public function tag(ResponseCacheConfigurator $configurator, Response $response, $view)
{
if (!$view instanceof View) {
return $this;
}

foreach ($view->getParameters() as $parameter) {
if (!$parameter instanceof ValueObject) {
continue;
}

$this->dispatcherTagger->tag($configurator, $response, $parameter);
}

return $this;
}
}

0 comments on commit f4702f3

Please sign in to comment.