8000 Remove hard dependency of RequestContext in AssetsExtension · symfony/symfony@5ad4d8a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ad4d8a

Browse files
committed
Remove hard dependency of RequestContext in AssetsExtension
1 parent aa58bb8 commit 5ad4d8a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/Symfony/Bundle/TwigBundle/Extension/AssetsExtension.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AssetsExtension extends \Twig_Extension
2424
private $container;
2525
private $context;
2626

27-
public function __construct(ContainerInterface $container, RequestContext $requestContext)
27+
public function __construct(ContainerInterface $container, RequestContext $requestContext = null)
2828
{
2929
$this->container = $container;
3030
$this->context = $requestContext;
@@ -94,13 +94,18 @@ public function getName()
9494
* @param string $url The URL that has to be absolute
9595
*
9696
* @return string The absolute URL
97+
* @throws \RuntimeException
9798
*/
9899
private function ensureUrlIsAbsolute($url)
99100
{
100101
if (false !== strpos($url, '://') || 0 === strpos($url, '//')) {
101102
return $url;
102103
}
103104

105+
if (!$this->context) {
106+
throw new \RuntimeException('To generate an absolute URL for an asset, the Symfony Routing component is required.');
107+
}
108+
104109
if ('' === $host = $this->context->getHost()) {
105110
return $url;
106111
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<service id="twig.extension.assets" class="%twig.extension.assets.class%" public="false">
6767
<tag name="twig.extension" />
6868
<argument type="service" id="service_container" />
69-
<argument type="service" id="router.request_context" />
69+
<argument type="service" id="router.request_context" on-invalid="null" />
7070
</service>
7171

7272
<service id="twig.extension.actions" class="%twig.extension.actions.class%" public="false">

0 commit comments

Comments
 (0)
0