Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a ViewParameters ResponseTagger #7

Open
wants to merge 2 commits into
base: 2.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions src/ResponseTagger/Delegator/DispatcherTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ public function tag(ResponseCacheConfigurator $configurator, Response $response,
foreach ($this->taggers as $tagger) {
$tagger->tag($configurator, $response, $value);
}

return $this;
}
}
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;
}
}
2 changes: 2 additions & 0 deletions src/ResponseTagger/Value/ContentInfoTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public function tag(ResponseCacheConfigurator $configurator, Response $response,
if ($value->mainLocationId) {
$configurator->addTags($response, ['location-' . $value->mainLocationId]);
}

return $this;
}
}