Skip to content

Commit

Permalink
add caching headers and sulu twig parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Mar 21, 2017
1 parent f4ade80 commit fcc5e76
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
35 changes: 32 additions & 3 deletions Controller/WebsiteArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Component\HttpCache\HttpCache;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -22,12 +24,15 @@
class WebsiteArticleController extends Controller
{
/**
* @param string $view
* Article index action.
*
* @param Request $request
* @param ArticleDocument $object
* @param string $view
*
* @return Response
*/
public function indexAction($view, ArticleDocument $object)
public function indexAction(Request $request, ArticleDocument $object, $view)
{
$content = $this->get('jms_serializer')->serialize(
$object,
Expand All @@ -40,7 +45,31 @@ public function indexAction($view, ArticleDocument $object)

return $this->render(
$view . '.html.twig',
$content
$this->get('sulu_website.resolver.template_attribute')->resolve($content),
$this->createResponse($request)
);
}

/**
* Create response.
*
* @param Request $request
*
* @return Response
*/
private function createResponse(Request $request)
{
$response = new Response();
$cacheLifetime = $request->attributes->get('_cacheLifetime');

if ($cacheLifetime) {
$response->setPublic();
$response->headers->set(
HttpCache::HEADER_REVERSE_PROXY_TTL,
$cacheLifetime
);
}

return $response;
}
}
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
class="Sulu\Bundle\ArticleBundle\Routing\ArticleRouteDefaultProvider">
<argument type="service" id="sulu_document_manager.document_manager"/>
<argument type="service" id="sulu_content.structure.factory"/>
<argument type="service" id="sulu_http_cache.cache_lifetime.resolver"/>

<tag name="sulu_route.defaults_provider"/>
</service>
Expand Down
41 changes: 39 additions & 2 deletions Routing/ArticleRouteDefaultProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface;
use Sulu\Component\Content\Document\WorkflowStage;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\StructureMetadata;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\HttpCache\CacheLifetimeResolverInterface;

/**
* Provides route-defaults for articles.
Expand All @@ -32,16 +34,23 @@ class ArticleRouteDefaultProvider implements RouteDefaultsProviderInterface
*/
private $structureMetadataFactory;

/**
* @var CacheLifetimeResolverInterface
*/
private $cacheLifetimeResolver;

/**
* @param DocumentManagerInterface $documentManager
* @param StructureMetadataFactoryInterface $structureMetadataFactory
*/
public function __construct(
DocumentManagerInterface $documentManager,
StructureMetadataFactoryInterface $structureMetadataFactory
StructureMetadataFactoryInterface $structureMetadataFactory,
CacheLifetimeResolverInterface $cacheLifetimeResolver
) {
$this->documentManager = $documentManager;
$this->structureMetadataFactory = $structureMetadataFactory;
$this->cacheLifetimeResolver = $cacheLifetimeResolver;
}

/**
Expand All @@ -60,7 +69,7 @@ public function getByEntity($entityClass, $id, $locale, $object = null)
return [
'object' => $object,
'view' => $metadata->view,
'_cacheLifetime' => $metadata->cacheLifetime,
'_cacheLifetime' => $this->getCacheLifetime($metadata),
'_controller' => $metadata->controller,
];
}
Expand All @@ -85,4 +94,32 @@ public function supports($entityClass)
{
return $entityClass === ArticleDocument::class;
}

/**
* Get cache life time.
*
* @param StructureMetadata $metadata
*
* @return int|null
*/
private function getCacheLifetime($metadata)
{
$cacheLifetime = $metadata->cacheLifetime;

if (!$cacheLifetime) {
return null;
}

if (!is_array($cacheLifetime)
|| !isset($cacheLifetime['type'])
|| !isset($cacheLifetime['value'])
|| $this->cacheLifetimeResolver->supports($cacheLifetime['type'], $cacheLifetime['value'])
) {
throw new \InvalidArgumentException(
sprintf('Invalid cachelifetime in article route default provider: %s', var_export($cacheLifetime, true))
);
}

return $this->cacheLifetimeResolver->resolve($cacheLifetime['type'], $cacheLifetime['value']);
}
}
10 changes: 10 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Upgrade

## dev-develop

### Cachelifetime request attribute changed

The `_cacheLifetime` attribute available in the request parameter of a article
controller will return the seconds and don't need longer be resolved manually
with the cachelifetime resolver.

0 comments on commit fcc5e76

Please sign in to comment.