Skip to content

Commit

Permalink
Inital version of Twig ez_http_tag_location() function (#67)
Browse files Browse the repository at this point in the history
1. Refactors Response taggers to get rid of (by now) unused configurator and reponse, in order to:
2. Expose Twig extension to have function for tagging locations

Reason for picking location: It has all data needed, we need lcoation in order to be
able to know which subtree to tag for (to make sure cache is cleared on subtree operations).

If needed there could be a helper for contentInfo as well which loads main for you.
  • Loading branch information
andrerom authored Jun 25, 2018
1 parent 37bcd02 commit 6cf5e2d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Resources/config/view_cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ services:
parent: ezplatform.view_cache.response_tagger.abstract_value
tags:
- {name: ezplatform.cache_response_tagger}

# Twig
ezplatform.view_cache.twig_extension:
class: EzSystems\PlatformHttpCacheBundle\Twig\ContentTaggingExtension
arguments: ['@ezplatform.view_cache.response_tagger.dispatcher']
tags:
- {name: twig.extension}
55 changes: 55 additions & 0 deletions src/Twig/ContentTaggingExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* File containing the ContentTaggingExtension class.
*
* @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\Twig;

use eZ\Publish\API\Repository\Values\Content\Location;
use EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger;
use Twig_Extension;
use Twig_SimpleFunction;

/**
* Twig content extension for eZ Publish specific usage.
* Exposes helpers to play with public API objects.
*/
class ContentTaggingExtension extends Twig_Extension
{
/** @var \EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger */
protected $responseTagger;

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

/**
* @return array|\Twig_Function[]
*/
public function getFunctions()
{
return [
new Twig_SimpleFunction(
'ez_http_tag_location',
[$this, 'tagHttpCacheForLocation']
),
];
}

/**
* Adds tags to current response.
*
* @internal Function is only for use within this class (and implicit by Twig).
*
* @param Location $location
*/
public function tagHttpCacheForLocation(Location $location)
{
$this->responseTagger->tag($location);
$this->responseTagger->tag($location->getContentInfo());
}
}

0 comments on commit 6cf5e2d

Please sign in to comment.