8000 bug #11749 [TwigBundle] Remove hard dependency of RequestContext in A… · symfony/symfony@f1611ab · GitHub
[go: up one dir, main page]

Skip to content

Commit f1611ab

Browse files
committed
bug #11749 [TwigBundle] Remove hard dependency of RequestContext in AssetsExtension (pgodel)
This PR was merged into the 2.5 branch. Discussion ---------- [TwigBundle] Remove hard dependency of RequestContext in AssetsExtension | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11466 | License | MIT | Doc PR | - #10451 introduced the requirement of RequestContext in AssetsExtension. This is only needed when generating absolute URLs for assets. When sending emails with Twig from the CLI, the router may not be present and most times is not required. This PR attempts to remove the hard dependency and set RequestContext if the router is present according to issue #11466. Commits ------- 5ad4d8a Remove hard dependency of RequestContext in AssetsExtension
2 parents b03530b + 5ad4d8a commit f1611ab

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