-
-
Notifications
You must be signed in to change notification settings - Fork 159
/
CoreResponseContextFactory.php
121 lines (97 loc) · 4.05 KB
/
CoreResponseContextFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\CoreBundle\Routing\ResponseContext;
use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\JsonLd\ContaoPageSchema;
use Contao\CoreBundle\Routing\ResponseContext\JsonLd\JsonLdManager;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\Environment;
use Contao\PageModel;
use Contao\StringUtil;
use Spatie\SchemaOrg\WebPage;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CoreResponseContextFactory
{
private ResponseContextAccessor $responseContextAccessor;
private EventDispatcherInterface $eventDispatcher;
private TokenChecker $tokenChecker;
private ContaoFramework $contaoFramework;
public function __construct(ResponseContextAccessor $responseContextAccessor, EventDispatcherInterface $eventDispatcher, TokenChecker $tokenChecker, ContaoFramework $contaoFramework)
{
$this->responseContextAccessor = $responseContextAccessor;
$this->eventDispatcher = $eventDispatcher;
$this->tokenChecker = $tokenChecker;
$this->contaoFramework = $contaoFramework;
}
public function createResponseContext(): ResponseContext
{
$context = new ResponseContext();
$this->responseContextAccessor->setResponseContext($context);
return $context;
}
public function createWebpageResponseContext(): ResponseContext
{
$context = $this->createResponseContext();
$context->add($this->eventDispatcher);
$context->addLazy(HtmlHeadBag::class);
$context->addLazy(
JsonLdManager::class,
static function () use ($context) {
$manager = new JsonLdManager($context);
$manager->getGraphForSchema(JsonLdManager::SCHEMA_ORG)->add(new WebPage());
return $manager;
}
);
return $context;
}
public function createContaoWebpageResponseContext(PageModel $pageModel): ResponseContext
{
$context = $this->createWebpageResponseContext();
/** @var HtmlHeadBag $htmlHeadBag */
$htmlHeadBag = $context->get(HtmlHeadBag::class);
/** @var JsonLdManager $jsonLdManager */
$jsonLdManager = $context->get(JsonLdManager::class);
$title = StringUtil::inputEncodedToPlainText($pageModel->pageTitle ?: $pageModel->title ?: '');
$htmlHeadBag
->setTitle($title ?: '')
->setMetaDescription(StringUtil::inputEncodedToPlainText($pageModel->description ?: ''))
;
if ($pageModel->robots) {
$htmlHeadBag->setMetaRobots($pageModel->robots);
}
if ($pageModel->enableCanonical && $pageModel->canonicalLink) {
$url = $this->contaoFramework->getAdapter(Controller::class)->replaceInsertTags($pageModel->canonicalLink, false);
// Ensure absolute links (FIXME: Remove once we remove support for relative urls)
if (!preg_match('#^https?://#', $url)) {
$url = $this->contaoFramework->getAdapter(Environment::class)->get('base').$url;
}
$htmlHeadBag->setCanonicalUri($url);
}
if ($pageModel->enableCanonical && $pageModel->canonicalKeepParams) {
$htmlHeadBag->setKeepParamsForCanonical(array_map('trim', explode(',', (string) $pageModel->canonicalKeepParams)));
}
$jsonLdManager
->getGraphForSchema(JsonLdManager::SCHEMA_CONTAO)
->set(
new ContaoPageSchema(
$title ?: '',
(int) $pageModel->id,
(bool) $pageModel->noSearch,
(bool) $pageModel->protected,
array_map('intval', array_filter((array) $pageModel->groups)),
$this->tokenChecker->isPreviewMode()
)
)
;
return $context;
}
}