Skip to content

Commit

Permalink
Get URI from RequestStack in UrlParser (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg authored Nov 18, 2024
1 parent e0dfd99 commit 01912ff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"php": "^8.1",
"contao/core-bundle": "^4.13 || ^5.0",
"symfony/mime": "^5.4 || ^6.0 || ^7.0",
"symfony/deprecation-contracts": "^2.1 || ^3.0",
"symfony/security-core": "^5.4 || ^6.0 || ^7.0"
},
"require-dev": {
Expand Down
22 changes: 19 additions & 3 deletions src/UrlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

namespace Codefog\HasteBundle;

use Contao\Environment;
use Contao\StringUtil;
use Contao\System;
use Symfony\Component\HttpFoundation\RequestStack;

class UrlParser
{
public function __construct(private readonly RequestStack|null $requestStack = null)
{
}

/**
* Add a query string to the given URI string or page ID.
*/
Expand Down Expand Up @@ -97,9 +102,20 @@ public function removeQueryStringCallback(callable $callback, string|null $url =
protected function prepareUrl(string|null $url = null): string
{
if (null === $url) {
$url = Environment::get('requestUri');
if (null !== $this->requestStack) {
$url = $this->requestStack->getCurrentRequest()?->getUri();
} else {
// Fallback for manually created UrlParser instances
trigger_deprecation('codefog/contao-haste', '5.2', 'Instantiating "%s" without the RequestStack has been deprecated and will no longer work in Haste 6. Retrieve the "%s" service from the container instead.', __CLASS__);

$container = System::getContainer();

if ($container->has('request_stack')) {
$url = $container->get('request_stack')->getCurrentRequest()?->getUri();
}
}
}

return StringUtil::ampersand($url, false);
return StringUtil::ampersand((string) $url, false);
}
}

0 comments on commit 01912ff

Please sign in to comment.