Skip to content

Commit

Permalink
Use the request object instead of the Environment class
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Nov 4, 2021
1 parent 06ea58a commit 1acca17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions core-bundle/src/Resources/config/services.yml
Expand Up @@ -717,6 +717,7 @@ services:
- '@event_dispatcher'
- '@contao.security.token_checker'
- '@Contao\CoreBundle\String\HtmlDecoder'
- '@request_stack'
- '@contao.framework'
public: true

Expand Down
Expand Up @@ -22,6 +22,7 @@
use Contao\Environment;
use Contao\PageModel;
use Spatie\SchemaOrg\WebPage;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class CoreResponseContextFactory
Expand All @@ -30,14 +31,16 @@ class CoreResponseContextFactory
private EventDispatcherInterface $eventDispatcher;
private TokenChecker $tokenChecker;
private HtmlDecoder $htmlDecoder;
private RequestStack $requestStack;
private ContaoFramework $contaoFramework;

public function __construct(ResponseContextAccessor $responseContextAccessor, EventDispatcherInterface $eventDispatcher, TokenChecker $tokenChecker, HtmlDecoder $htmlDecoder, ContaoFramework $contaoFramework)
public function __construct(ResponseContextAccessor $responseContextAccessor, EventDispatcherInterface $eventDispatcher, TokenChecker $tokenChecker, HtmlDecoder $htmlDecoder, RequestStack $requestStack, ContaoFramework $contaoFramework)
{
$this->responseContextAccessor = $responseContextAccessor;
$this->eventDispatcher = $eventDispatcher;
$this->tokenChecker = $tokenChecker;
$this->htmlDecoder = $htmlDecoder;
$this->requestStack = $requestStack;
$this->contaoFramework = $contaoFramework;
}

Expand Down Expand Up @@ -98,9 +101,11 @@ public function createContaoWebpageResponseContext(PageModel $pageModel): Respon

// Ensure absolute links
if (!preg_match('#^https?://#', $url)) {
/** @var Environment $environment */
$environment = $this->contaoFramework->getAdapter(Environment::class);
$url = $environment->get('base').$url;
if (!$request = $this->requestStack->getMainRequest()) {
throw new \RuntimeException('The request stack did not contain a request');
}

$url = $request->getSchemeAndHttpHost().'/'.$url;
}

$htmlHeadBag->setCanonicalUri($url);
Expand Down
Expand Up @@ -21,11 +21,11 @@
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\CoreBundle\String\HtmlDecoder;
use Contao\Environment;
use Contao\PageModel;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand All @@ -44,6 +44,7 @@ public function testResponseContext(): void
$this->createMock(EventDispatcherInterface::class),
$this->createMock(TokenChecker::class),
new HtmlDecoder(),
$this->createMock(RequestStack::class),
$this->createMock(ContaoFramework::class)
);

Expand All @@ -65,6 +66,7 @@ public function testWebpageResponseContext(): void
$this->createMock(EventDispatcherInterface::class),
$this->createMock(TokenChecker::class),
new HtmlDecoder(),
$this->createMock(RequestStack::class),
$this->createMock(ContaoFramework::class)
);

Expand Down Expand Up @@ -110,19 +112,13 @@ public function testContaoWebpageResponseContext(): void
->willReturn('de/foobar.html')
;

$environmentAdapter = $this->mockAdapter(['get']);
$environmentAdapter
->expects($this->once())
->method('get')
->with('base')
->willReturn('https://example.com/')
;

$contaoFramework = $this->mockContaoFramework([
Controller::class => $controllerAdapter,
Environment::class => $environmentAdapter,
]);

$requestStack = new RequestStack();
$requestStack->push(Request::create('https://example.com/'));

/** @var PageModel $pageModel */
$pageModel = $this->mockClassWithProperties(PageModel::class);
$pageModel->title = 'My title';
Expand All @@ -136,6 +132,7 @@ public function testContaoWebpageResponseContext(): void
$this->createMock(EventDispatcherInterface::class),
$this->createMock(TokenChecker::class),
new HtmlDecoder(),
$requestStack,
$contaoFramework
);

Expand Down Expand Up @@ -182,6 +179,7 @@ public function testDecodingAndCleanupOnContaoResponseContext(): void
$this->createMock(EventDispatcherInterface::class),
$this->createMock(TokenChecker::class),
new HtmlDecoder(),
$this->createMock(RequestStack::class),
$this->createMock(ContaoFramework::class)
);

Expand Down

0 comments on commit 1acca17

Please sign in to comment.