Skip to content

Commit

Permalink
Rework the response context (see #3066)
Browse files Browse the repository at this point in the history
Description
-----------

Alternative approach for response context as discussed in Core team call.
I guess there's no point in explaining it all here - I'll just document the final iteration once everything is done in the Contao dev docs ^^

Commits
-------

205dafc Reworked response context concept
786b50c Merge branch '4.x' into feature/response-context-rework
4d1cf18 CS
d2151bb Fixed tests
aae1091 Implemented removing existing services and added support for interfaces
0a83cbe Added psalm docs
5f27ffc Make HtmlHeadBag final
5e75e38 CS
950e9bb Also consider parent classes as aliases
fbe8206 Going back to a regular array and removed the remove() method
41b1c60 Added support for lazy services
8a35bc9 Fixed tests
0d7f8d9 CS

Co-authored-by: Leo Feyer <github@contao.org>
5b9706c Added failing test to show class issue
d37a223 Merge pull request #4 from aschempp/feature/response-context-rework

Added failing test to show class issue
f0a1f35 Removed irrelevant assertion
1d29c95 Fixed tests
6399cc7 Cleanup
1255632 Fixed phpstan
6a1eebc CS
e90daf4 Added failing test to show alias being overridden
a5c9589 Merge pull request #5 from aschempp/feature/response-context-rework

Added failing test to show alias being overridden
1cb74c7 Fixed tests
84af804 Removed leftover
62cb0af CS
  • Loading branch information
Toflar committed Jun 14, 2021
1 parent bdf6dac commit 65c6994
Show file tree
Hide file tree
Showing 21 changed files with 414 additions and 259 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); // Already stored decoded
$htmlHeadBag->setTitle($objEvent->pageTitle); // Already stored decoded
}
elseif ($objEvent->title)
{
$responseContext->setTitle(StringUtil::inputEncodedToPlainText($objEvent->title));
$htmlHeadBag->setTitle(StringUtil::inputEncodedToPlainText($objEvent->title));
}

if ($objEvent->description)
{
$responseContext->setMetaDescription(StringUtil::inputEncodedToPlainText($objEvent->description));
$htmlHeadBag->setMetaDescription(StringUtil::inputEncodedToPlainText($objEvent->description));
}
elseif ($objEvent->teaser)
{
$responseContext->setMetaDescription(StringUtil::htmlToPlainText($objEvent->teaser));
$htmlHeadBag->setMetaDescription(StringUtil::htmlToPlainText($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] = htmlspecialchars($responseContext->getTitle());
$arrCache[$strTag] = htmlspecialchars($htmlHeadBag->getTitle());
break;

case 'description':
$arrCache[$strTag] = htmlspecialchars($responseContext->getMetaDescription());
$arrCache[$strTag] = htmlspecialchars($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(StringUtil::inputEncodedToPlainText($varValue ?? ''));
$htmlHeadBag->setTitle(StringUtil::inputEncodedToPlainText($varValue ?? ''));
break;

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

case 'robots':
$responseContext->setMetaRobots($varValue);
$htmlHeadBag->setMetaRobots($varValue);
break;
}
}
Expand Down
10 changes: 6 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,15 @@ protected function compile()
{
$responseContext = System::getContainer()->get(ResponseContextAccessor::class)->getResponseContext();

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

if ($this->teaser)
{
$responseContext->setMetaDescription(StringUtil::htmlToPlainText($this->teaser));
$htmlHeadBag->setMetaDescription(StringUtil::htmlToPlainText($this->teaser));
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions core-bundle/src/Resources/contao/pages/PageRegular.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Contao\CoreBundle\Exception\NoLayoutSpecifiedException;
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 +73,7 @@ protected function prepare($objPage)
$container = System::getContainer();
$container->get('request_stack')->getCurrentRequest()->setLocale($locale);
$container->get('translator')->setLocale($locale);

$responseContext = $container->get(CoreResponseContextFactory::class)->createContaoWebpageResponseContext($objPage);

System::loadLanguageFile('default');
Expand Down Expand Up @@ -204,14 +206,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 = htmlspecialchars($responseContext->getTitle());
$this->Template->pageTitle = htmlspecialchars($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 +223,7 @@ protected function prepare($objPage)

// Assign the title and description
$this->Template->title = strip_tags($this->replaceInsertTags($objLayout->titleTag));
$this->Template->description = htmlspecialchars($responseContext->getMetaDescription());
$this->Template->description = htmlspecialchars($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,7 +12,9 @@

namespace Contao\CoreBundle\Routing\ResponseContext;

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

class CoreResponseContextFactory
{
Expand All @@ -26,26 +28,40 @@ 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->addLazy(HtmlHeadBag::class, static function () { return 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();

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

$title = $pageModel->pageTitle ?: StringUtil::inputEncodedToPlainText($pageModel->title ?: '');

$htmlHeadBag
->setTitle($title ?: '')
->setMetaDescription(StringUtil::inputEncodedToPlainText($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
final class HtmlHeadBag
{
/**
* @var string
Expand Down

0 comments on commit 65c6994

Please sign in to comment.