8000 feature #36681 [FrameworkBundle] use the router context by default fo… · symfony/symfony@36c0ce7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36c0ce7

Browse files
committed
feature #36681 [FrameworkBundle] use the router context by default for assets (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] use the router context by default for assets | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Follows #36651 and #21027 This means assets are going to be configured automatically most of the time. The only case where `asset.request_context.base_path` is useful is when the webserver still keeps a `/index.php/` in URLs. (I'm not sure if the doc should tell ppl to use the parameter, or if we should tell ppl to improve the config of their server...) Commits ------- 1ac5f68 [FrameworkBundle] use the router context by default for assets
2 parents 3cb4056 + 1ac5f68 commit 36c0ce7

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class AssetsContextPass implements CompilerPassInterface
20+
{
21+
public function process(ContainerBuilder $container)
22+
{
23+
if (!$container->hasDefinition('assets.context')) {
24+
return;
25+
}
26+
27+
if (!$container->hasDefinition('router.request_context')) {
28+
$container->setParameter('asset.request_context.base_path', $container->getParameter('asset.request_context.base_path') ?? '');
29+
$container->setParameter('asset.request_context.secure', $container->getParameter('asset.request_context.secure') ?? false);
30+
31+
return;
32+
}
33+
34+
$context = $container->getDefinition('assets.context');
35+
36+
if (null === $container->getParameter('asset.request_context.base_path')) {
37+
$context->replaceArgument(1, (new Definition('string'))->setFactory([new Reference('router.request_context'), 'getBaseUrl']));
38+
}
39+
40+
if (null === $container->getParameter('asset.request_context.secure')) {
41+
$context->replaceArgument(2, (new Definition('bool'))->setFactory([new Reference('router.request_context'), 'isSecure']));
42+
}
43+
}
44+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
17+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AssetsContextPass;
1718
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
1819
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
1920
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
@@ -120,6 +121,7 @@ public function build(ContainerBuilder $container)
120121
]);
121122
}
122123

124+
$container->addCompilerPass(new AssetsContextPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
123125
$container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
124126
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
125127
$container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);

src/Symfony/Bundle/FrameworkBundle/Resources/config/assets.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="asset.request_context.base_path"></parameter>
9-
<parameter key="asset.request_context.secure">false</parameter>
8+
<parameter key="asset.request_context.base_path">null</parameter>
9+
<parameter key="asset.request_context.secure">null</parameter>
1010
</parameters>
1111

1212
<services>

src/Symfony/Component/Routing/RequestContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,9 @@ public function setParameter(string $name, $parameter)
319319

320320
return $this;
321321
}
322+
323+
public function isSecure(): bool
324+
{
325+
return 'https' === $this->scheme;
326+
}
322327
}

0 commit comments

Comments
 (0)
0