Skip to content

Commit

Permalink
Reworked response context concept
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Jun 8, 2021
1 parent ad86686 commit 205dafc
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 248 deletions.
17 changes: 10 additions & 7 deletions calendar-bundle/src/Resources/contao/modules/ModuleEventReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Exception\RedirectResponseException;
use Contao\CoreBundle\Image\Studio\Studio;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\CoreBundle\Routing\ResponseContext\WebpageResponseContext;
use Patchwork\Utf8;

/**
Expand Down Expand Up @@ -132,29 +132,32 @@ protected function compile()
// Overwrite the page meta data (see #2853, #4955 and #87)
$responseContext = System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();

if ($responseContext instanceof WebpageResponseContext)
if ($responseContext && $responseContext->has(HtmlHeadBag::class))
{
/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $responseContext->get(HtmlHeadBag::class);

if ($objEvent->pageTitle)
{
$responseContext->setTitle($objEvent->pageTitle);
$htmlHeadBag->setTitle($objEvent->pageTitle);
}
elseif ($objEvent->title)
{
$responseContext->setTitle(strip_tags(StringUtil::stripInsertTags($objEvent->title)));
$htmlHeadBag->setTitle(strip_tags(StringUtil::stripInsertTags($objEvent->title)));
}

if ($objEvent->description)
{
$responseContext->setMetaDescription($objEvent->description);
$htmlHeadBag->setMetaDescription($objEvent->description);
}
elseif ($objEvent->teaser)
{
$responseContext->setMetaDescription($this->prepareMetaDescription($objEvent->teaser));
$htmlHeadBag->setMetaDescription($this->prepareMetaDescription($objEvent->teaser));
}

if ($objEvent->robots)
{
$responseContext->setMetaRobots($objEvent->robots);
$htmlHeadBag->setMetaRobots($objEvent->robots);
}
}

Expand Down
11 changes: 7 additions & 4 deletions core-bundle/src/Resources/contao/library/Contao/InsertTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

use Contao\CoreBundle\Controller\InsertTagsController;
use Contao\CoreBundle\Image\Studio\FigureRenderer;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\CoreBundle\Routing\ResponseContext\WebpageResponseContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
Expand Down Expand Up @@ -734,16 +734,19 @@ protected function doReplace($strBuffer, $blnCache)

$responseContext = System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();

if ($responseContext instanceof WebpageResponseContext && \in_array($elements[1], array('pageTitle', 'description'), true))
if ($responseContext && $responseContext->has(HtmlHeadBag::class) && \in_array($elements[1], array('pageTitle', 'description'), true))
{
/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $responseContext->get(HtmlHeadBag::class);

switch ($elements[1])
{
case 'pageTitle':
$arrCache[$strTag] = $responseContext->getTitle();
$arrCache[$strTag] = $htmlHeadBag->getTitle();
break;

case 'description':
$arrCache[$strTag] = $responseContext->getMetaDescription();
$arrCache[$strTag] = $htmlHeadBag->getMetaDescription();
break;
}
}
Expand Down
13 changes: 8 additions & 5 deletions core-bundle/src/Resources/contao/models/PageModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Contao;

use Contao\CoreBundle\Exception\NoRootPageFoundException;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\CoreBundle\Routing\ResponseContext\WebpageResponseContext;
use Contao\Model\Collection;
use Contao\Model\Registry;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
Expand Down Expand Up @@ -309,20 +309,23 @@ public function __set($strKey, $varValue)

$responseContext = System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();

if ($responseContext instanceof WebpageResponseContext)
if ($responseContext && $responseContext->has(HtmlHeadBag::class))
{
/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $responseContext->get(HtmlHeadBag::class);

switch ($strKey)
{
case 'pageTitle':
$responseContext->setTitle($varValue);
$htmlHeadBag->setTitle($varValue);
break;

case 'description':
$responseContext->setMetaDescription($varValue);
$htmlHeadBag->setMetaDescription($varValue);
break;

case 'robots':
$responseContext->setMetaRobots($varValue);
$htmlHeadBag->setMetaRobots($varValue);
break;
}
}
Expand Down
11 changes: 7 additions & 4 deletions core-bundle/src/Resources/contao/modules/ModuleArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace Contao;

use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\CoreBundle\Routing\ResponseContext\WebpageResponseContext;

/**
* Provides methodes to handle articles.
Expand Down Expand Up @@ -169,13 +169,16 @@ protected function compile()
{
$responseContext = System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();

if ($responseContext instanceof WebpageResponseContext)
if ($responseContext && $responseContext->has(HtmlHeadBag::class))
{
$responseContext->setTitle(strip_tags(StringUtil::stripInsertTags($this->title)));
/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $responseContext->get(HtmlHeadBag::class);

$htmlHeadBag->setTitle(strip_tags(StringUtil::stripInsertTags($this->title)));

if ($this->teaser)
{
$responseContext->setMetaDescription($this->prepareMetaDescription($this->teaser));
$htmlHeadBag->setMetaDescription($this->prepareMetaDescription($this->teaser));
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions core-bundle/src/Resources/contao/pages/PageRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace Contao;

use Contao\CoreBundle\Exception\NoLayoutSpecifiedException;
use Contao\CoreBundle\Routing\ResponseContext\ContaoWebpageResponseContext;
use Contao\CoreBundle\Routing\ResponseContext\CoreResponseContextFactory;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -72,6 +74,8 @@ protected function prepare($objPage)
$container = System::getContainer();
$container->get('request_stack')->getCurrentRequest()->setLocale($locale);
$container->get('translator')->setLocale($locale);

/** @var ContaoWebpageResponseContext $responseContext */
$responseContext = $container->get(CoreResponseContextFactory::class)->createContaoWebpageResponseContext($objPage);

System::loadLanguageFile('default');
Expand Down Expand Up @@ -204,14 +208,14 @@ protected function prepare($objPage)

// Set the page title and description AFTER the modules have been generated
$this->Template->mainTitle = $objPage->rootPageTitle;
$this->Template->pageTitle = $responseContext->getTitle();
$this->Template->pageTitle = $responseContext->get(HtmlHeadBag::class)->getTitle();

// Remove shy-entities (see #2709)
$this->Template->mainTitle = str_replace('[-]', '', $this->Template->mainTitle);
$this->Template->pageTitle = str_replace('[-]', '', $this->Template->pageTitle);

// Meta robots tag
$this->Template->robots = $responseContext->getMetaRobots();
$this->Template->robots = $responseContext->get(HtmlHeadBag::class)->getMetaRobots();

// Fall back to the default title tag
if (!$objLayout->titleTag)
Expand All @@ -221,7 +225,7 @@ protected function prepare($objPage)

// Assign the title and description
$this->Template->title = strip_tags($this->replaceInsertTags($objLayout->titleTag));
$this->Template->description = str_replace(array("\n", "\r", '"'), array(' ', '', ''), $responseContext->getMetaDescription());
$this->Template->description = str_replace(array("\n", "\r", '"'), array(' ', '', ''), $responseContext->get(HtmlHeadBag::class)->getMetaDescription());

// Body onload and body classes
$this->Template->onload = trim($objLayout->onload);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Contao\CoreBundle\Routing\ResponseContext;

use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\PageModel;

class CoreResponseContextFactory
Expand All @@ -26,26 +27,44 @@ public function __construct(ResponseContextAccessor $responseContextAccessor)
$this->responseContextAccessor = $responseContextAccessor;
}

public function createWebpageResponseContext(): WebpageResponseContext
public function createResponseContext(): ResponseContext
{
$context = new WebpageResponseContext();
$context = new ResponseContext();

$this->responseContextAccessor->setResponseContext($context);

return $context;
}

public function createResponseContext(): ResponseContext
public function createWebpageResponseContext(): ResponseContext
{
$context = new ResponseContext();
$this->responseContextAccessor->setResponseContext($context);
$context = $this->createResponseContext();
$context->add(new HtmlHeadBag());

return $context;
}

public function createContaoWebpageResponseContext(PageModel $pageModel): ContaoWebpageResponseContext
public function createContaoWebpageResponseContext(PageModel $pageModel): ResponseContext
{
$context = new ContaoWebpageResponseContext($pageModel);
$this->responseContextAccessor->setResponseContext($context);
$context = $this->createWebpageResponseContext();

if (!$context->has(HtmlHeadBag::class)) {
return $context;
}

/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $context->get(HtmlHeadBag::class);

$htmlHeadBag
->setTitle($pageModel->pageTitle ?: $pageModel->title ?: '')
->setMetaDescription(str_replace(["\n", "\r", '"'], [' ', '', ''], $pageModel->description ?: ''))
;

if ($pageModel->robots) {
$htmlHeadBag
->setMetaRobots($pageModel->robots)
;
}

return $context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Routing\ResponseContext;
namespace Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag;

class WebpageResponseContext extends ResponseContext
class HtmlHeadBag
{
/**
* @var string
Expand Down
41 changes: 28 additions & 13 deletions core-bundle/src/Routing/ResponseContext/ResponseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,46 @@

namespace Contao\CoreBundle\Routing\ResponseContext;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

class ResponseContext implements ResponseContextInterface
final class ResponseContext
{
public const REQUEST_ATTRIBUTE_NAME = '_contao_response_context';

/**
* @var array
*/
private $services = [];

/**
* @var PartialResponseHeaderBag|null
*/
private $headerBag;

public function getHeaderBag(): ResponseHeaderBag
public function add(object $service): self
{
if (null === $this->headerBag) {
$this->headerBag = new PartialResponseHeaderBag();
}
$this->services[\get_class($service)] = $service;

return $this->headerBag;
return $this;
}

public function has(string $service): bool
{
return null !== $this->get($service);
}

public function finalize(Response $response): ResponseContextInterface
/**
* @return mixed|null
*/
public function get(string $service)
{
foreach ($this->getHeaderBag()->all() as $name => $values) {
$response->headers->set($name, $values, false); // Do not replace but add
return $this->services[$service] ?? null;
}

public function getHeaderBag(): PartialResponseHeaderBag
{
if (null === $this->headerBag) {
$this->headerBag = new PartialResponseHeaderBag();
}

return $this;
return $this->headerBag;
}
}

0 comments on commit 205dafc

Please sign in to comment.