Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use requestStack to access current request when needed. #3132

Merged
merged 3 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/Canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Canonical
private $urlGenerator;

/** @var Request */
private $request;
private $request = null;

/** @var string */
private $scheme = null;
Expand All @@ -44,27 +44,39 @@ class Canonical
/** @var RouterInterface */
private $router;

/** @var RequestStack */
private $requestStack;

public function __construct(Config $config, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack, RouterInterface $router, string $defaultLocale)
{
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->request = $requestStack->getCurrentRequest() ?? Request::createFromGlobals();
$this->defaultLocale = $defaultLocale;
$this->router = $router;
$this->requestStack = $requestStack;
}

public function getRequest(): Request
{
// always try to use current request
if ($this->request === null) {
$this->request = $this->requestStack->getCurrentRequest() ?? Request::createFromGlobals();
$this->init();
}

$this->init();
return $this->request;
}

public function init(): void
{
// Ensure in request cycle (even for override).
if ($this->request === null || $this->request->getHost() === '') {
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix PHPStan's remark:

Strict comparison using === between Symfony\Component\HttpFoundation\Request and null will always evaluate to false.

Suggested change
if ($this->getRequest() === null || $this->getRequest()->getHost() === '') {
if ($this->getRequest()->getHost() === '') {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with possible infinite loop.
I aimed to keep init() for backwards compatibility.
But maybe we should simply ditch it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See latest changes.
I added a request setter.
Please re-run checks.

My tests passed.

return;
}

$requestUrl = parse_url($this->request->getSchemeAndHttpHost());
$requestUrl = parse_url($this->getRequest()->getSchemeAndHttpHost());

$configCanonical = (string) $this->config->get('general/canonical', $this->request->getSchemeAndHttpHost());
$configCanonical = (string) $this->config->get('general/canonical', $this->getRequest()->getSchemeAndHttpHost());

if (mb_strpos($configCanonical, 'http') !== 0) {
$configCanonical = $requestUrl['scheme'] . '://' . $configCanonical;
Expand All @@ -83,7 +95,7 @@ public function init(): void
public function get(?string $route = null, array $params = [], bool $absolute = true): ?string
{
// Ensure request has been matched
if (! $this->request->attributes->get('_route')) {
if (! $this->getRequest()->attributes->get('_route')) {
return null;
}

Expand Down Expand Up @@ -153,8 +165,8 @@ public function setHost(string $host): void
public function getPath(): string
{
if ($this->path === null) {
$route = $this->request->attributes->get('_route');
$params = $this->request->attributes->get('_route_params');
$route = $this->getRequest()->attributes->get('_route');
$params = $this->getRequest()->attributes->get('_route_params');

$this->path = $this->generateLink($route, $params, false);
}
Expand All @@ -164,10 +176,10 @@ public function getPath(): string

public function setPath(?string $route = null, array $params = []): void
{
if (! $route && ! $this->request->attributes->has('_route')) {
if (! $route && ! $this->getRequest()->attributes->has('_route')) {
return;
} elseif (! $route) {
$route = $this->request->attributes->get('_route');
$route = $this->getRequest()->attributes->get('_route');
}

$this->path = $this->generateLink($route, $params, false);
Expand Down Expand Up @@ -207,7 +219,7 @@ public function generateLink(?string $route, ?array $params, $canonical = false)
);
} catch (InvalidParameterException | MissingMandatoryParametersException | RouteNotFoundException | \TypeError $e) {
// Just use the current URL /shrug
return $canonical ? $this->request->getUri() : $this->request->getPathInfo();
return $canonical ? $this->getRequest()->getUri() : $this->getRequest()->getPathInfo();
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/Event/Subscriber/CanonicalSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Bolt\Event\Subscriber;

use Bolt\Canonical;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class CanonicalSubscriber implements EventSubscriberInterface
{
private $canonical;

public function __construct(Canonical $canonical)
{
$this->canonical = $canonical;
}

public function onKernelRequest(RequestEvent $event)
{
// ensure initialization with real request
$this->canonical->getRequest();
}

public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => [
['onKernelRequest', 0],
],
];
}
}
10 changes: 5 additions & 5 deletions src/Menu/FrontendMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ final class FrontendMenu implements FrontendMenuBuilderInterface
/** @var FrontendMenuBuilder */
private $menuBuilder;

/** @var Request */
private $request;

/** @var Stopwatch */
private $stopwatch;

/** @var Config */
private $config;

/** @var RequestStack */
private $requestStack;

public function __construct(FrontendMenuBuilder $menuBuilder, TagAwareCacheInterface $cache, RequestStack $requestStack, Stopwatch $stopwatch, Config $config)
{
$this->cache = $cache;
$this->menuBuilder = $menuBuilder;
$this->request = $requestStack->getCurrentRequest();
$this->stopwatch = $stopwatch;
$this->config = $config;
$this->requestStack = $requestStack;
}

public function buildMenu(Environment $twig, ?string $name = null): array
{
$this->stopwatch->start('bolt.frontendMenu');

$key = 'bolt.frontendMenu_' . ($name ?: 'main') . '_' . $this->request->getLocale();
$key = 'bolt.frontendMenu_' . ($name ?: 'main') . '_' . $this->requestStack->getCurrentRequest()->getLocale();

$menu = $this->cache->get($key, function (ItemInterface $item) use ($name, $twig) {
$item->expiresAfter($this->config->get('general/caching/frontend_menu'));
Expand Down