Commit e22dbc83 authored by Tino Goratsch's avatar Tino Goratsch

refactored the BootstrapCache

parent 32ee1634
......@@ -6,6 +6,7 @@
namespace ACP3\Core\Application;
use ACP3\Core\Application\BootstrapCache\Event\Listener\StaticAssetsListener;
use ACP3\Core\Application\BootstrapCache\Event\Listener\UserContextListener;
use ACP3\Core\Session\SessionHandlerInterface;
use ACP3\Core\View\Renderer\Smarty\Filters\MoveToBottom;
......@@ -15,7 +16,6 @@ use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
use FOS\HttpCache\SymfonyCache\PurgeListener;
use FOS\HttpCache\SymfonyCache\RefreshListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
......@@ -49,6 +49,7 @@ class BootstrapCache extends HttpCache implements CacheInvalidation
]));
$this->addSubscriber(new PurgeListener());
$this->addSubscriber(new RefreshListener());
$this->addSubscriber(new StaticAssetsListener());
if (isset($options['debug']) && $options['debug']) {
$this->addSubscriber(new DebugListener());
}
......@@ -63,55 +64,4 @@ class BootstrapCache extends HttpCache implements CacheInvalidation
{
return parent::fetch($request, $catch);
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
$response = parent::handle($request, $type, $catch);
$this->moveStaticAssetsAround($response);
return $response;
}
/**
* @param Response $response
*/
private function moveStaticAssetsAround(Response $response)
{
$content = $response->getContent();
if (strpos($content, static::PLACEHOLDER) !== false) {
$content = str_replace(
static::PLACEHOLDER,
$this->addElementsFromTemplates($content) . "\n" . static::PLACEHOLDER,
$this->getCleanedUpTemplateOutput($content)
);
$response->setContent($content);
$response->headers->set('Content-Length', strlen($content));
}
}
/**
* @param string $tplOutput
* @return string
*/
private function getCleanedUpTemplateOutput(string $tplOutput): string
{
return preg_replace(static::JAVASCRIPTS_REGEX_PATTERN, '', $tplOutput);
}
/**
* @param string $tplOutput
* @return string
*/
private function addElementsFromTemplates(string $tplOutput): string
{
$matches = [];
preg_match_all(static::JAVASCRIPTS_REGEX_PATTERN, $tplOutput, $matches);
return implode("\n", array_unique($matches[1])) . "\n";
}
}
<?php
/**
* Copyright (c) by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Application\BootstrapCache\Event\Listener;
use ACP3\Core\View\Renderer\Smarty\Filters\MoveToBottom;
use FOS\HttpCache\SymfonyCache\CacheEvent;
use FOS\HttpCache\SymfonyCache\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class StaticAssetsListener implements EventSubscriberInterface
{
const JAVASCRIPTS_REGEX_PATTERN = MoveToBottom::ELEMENT_CATCHER_REGEX_PATTERN;
const PLACEHOLDER = '</body>';
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
Events::POST_HANDLE => 'postHandle'
];
}
public function postHandle(CacheEvent $event)
{
$response = $event->getResponse();
$content = $response->getContent();
if (strpos($content, static::PLACEHOLDER) !== false) {
$content = str_replace(
static::PLACEHOLDER,
$this->addElementsFromTemplates($content) . "\n" . static::PLACEHOLDER,
$this->getCleanedUpTemplateOutput($content)
);
$response->setContent($content);
$response->headers->set('Content-Length', strlen($content));
}
}
/**
* @param string $tplOutput
* @return string
*/
private function getCleanedUpTemplateOutput(string $tplOutput): string
{
return preg_replace(static::JAVASCRIPTS_REGEX_PATTERN, '', $tplOutput);
}
/**
* @param string $tplOutput
* @return string
*/
private function addElementsFromTemplates(string $tplOutput): string
{
$matches = [];
preg_match_all(static::JAVASCRIPTS_REGEX_PATTERN, $tplOutput, $matches);
return implode("\n", array_unique($matches[1])) . "\n";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment