From d09052cf14dc207429e1af43035cf0df5a2bbf5b Mon Sep 17 00:00:00 2001 From: Cornelius Adams Date: Thu, 4 Jun 2015 13:35:15 +0200 Subject: [PATCH] added debug to esi template added handles to esi url --- config/module.config.php | 6 +++--- src/ConVarnish/Controller/EsiController.php | 11 +++++++++-- .../Controller/EsiControllerFactory.php | 19 +++++++++++++++++++ .../Listener/InjectCacheHeaderListener.php | 11 ++++++++++- src/ConVarnish/View/Helper/EsiUrl.php | 13 +++++++++++-- view/con-varnish/esi.phtml | 19 ++++++++++++++++++- 6 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 src/ConVarnish/Controller/EsiControllerFactory.php diff --git a/config/module.config.php b/config/module.config.php index a8df173..ca51902 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -14,8 +14,8 @@ ] ], 'controllers' => [ - 'invokables' => [ - 'ConVarnish\Controller\Esi' => 'ConVarnish\Controller\EsiController' + 'factories' => [ + 'ConVarnish\Controller\Esi' => 'ConVarnish\Controller\EsiControllerFactory' ] ], 'router' => [ @@ -25,7 +25,7 @@ 'options' => [ 'route' => '/esi/:block', 'constraints' => [ - 'block' => '[A-Za-z0-9.-_]+' + 'block' => '[A-Za-z0-9_.-]+' ], 'defaults' => [ 'controller' => 'ConVarnish\Controller\Esi', diff --git a/src/ConVarnish/Controller/EsiController.php b/src/ConVarnish/Controller/EsiController.php index 12e35ed..68d5665 100644 --- a/src/ConVarnish/Controller/EsiController.php +++ b/src/ConVarnish/Controller/EsiController.php @@ -1,6 +1,7 @@ params()->fromRoute('block'); + $handles = $this->params()->fromQuery('handles', []); + /* @var $layoutManager LayoutManager */ + $layoutManager = $this->layoutManager(); + foreach ($handles as $handle) { + $layoutManager->addHandle($handle); + } if (!$blockId) { return $this->blockNotFound($blockId); } - $this->layoutManager()->load(); - if (!$block = $this->layoutManager()->getBlock($blockId)) { + $layoutManager->load(); + if (!$block = $layoutManager->getBlock($blockId)) { $block = $this->blockNotFound($blockId); } $block->setVariable('__ESI__', true); diff --git a/src/ConVarnish/Controller/EsiControllerFactory.php b/src/ConVarnish/Controller/EsiControllerFactory.php new file mode 100644 index 0000000..9f25caa --- /dev/null +++ b/src/ConVarnish/Controller/EsiControllerFactory.php @@ -0,0 +1,19 @@ + + */ +class EsiControllerFactory implements FactoryInterface +{ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $controller = new EsiController(); + return $controller; + } +} diff --git a/src/ConVarnish/Listener/InjectCacheHeaderListener.php b/src/ConVarnish/Listener/InjectCacheHeaderListener.php index 61aa844..71d3f26 100644 --- a/src/ConVarnish/Listener/InjectCacheHeaderListener.php +++ b/src/ConVarnish/Listener/InjectCacheHeaderListener.php @@ -195,8 +195,17 @@ public function injectEsi(EventInterface $e) } /* @var $block ViewModel */ $block = $e->getParam('block'); - if ($block->getOption('esi')) { + if ($options = $block->getOption('esi')) { $block->setTemplate(self::ESI_TEMPLATE); + $handles = isset($options['handles']) ? (array) $options['handles'] : []; + $block->setVariable('__HANDLES__', $handles); + if ($this->varnishOptions->getDebug()) { + $block->setVariables([ + '__DEBUG__' => true, + '__TTL__' => isset($options['ttl']) ? $options['ttl'] : 'n/a', + '__TAGS__' => $block->getOption('cache_tags', []) + ]); + } $this->injectEsiHeader(); } } diff --git a/src/ConVarnish/View/Helper/EsiUrl.php b/src/ConVarnish/View/Helper/EsiUrl.php index 79f79a0..59722c5 100644 --- a/src/ConVarnish/View/Helper/EsiUrl.php +++ b/src/ConVarnish/View/Helper/EsiUrl.php @@ -15,11 +15,20 @@ class EsiUrl extends AbstractHelper * @param string $blockId * @return array|string */ - public function __invoke($blockId) + public function __invoke($blockId, array $handles = []) { + $options = []; + if (count($handles)) { + $options = [ + 'query' => [ + 'handles' => $handles + ] + ]; + } $url = $this->getView()->url( 'esi', - ['block' => $blockId] + ['block' => $blockId], + $options ); return $url; } diff --git a/view/con-varnish/esi.phtml b/view/con-varnish/esi.phtml index be7924a..cb362ee 100644 --- a/view/con-varnish/esi.phtml +++ b/view/con-varnish/esi.phtml @@ -1,8 +1,25 @@ esiUrl($this->{$blockId}, $this->__HANDLES__); ?> - +__DEBUG__): ?> + + + content ?> +__DEBUG__): ?> + +