Skip to content

Commit 345a893

Browse files
dunglaschalasr
authored andcommitted
[FrameworkBundle][HttpKernel][TwigBridge] Add an helper to generate fragments URL
1 parent d2ba040 commit 345a893

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
* Deprecate all other values than "none", "php_array" and "file" for `framework.annotation.cache`
1919
* Add `KernelTestCase::getContainer()` as the best way to get a container in tests
2020
* Rename the container parameter `profiler_listener.only_master_requests` to `profiler_listener.only_main_requests`
21+
* Add service `fragment.uri_generator` to generate the URI of a fragment
2122

2223
5.2.0
2324
-----

Resources/config/fragment_renderer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler;
1515
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
16+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGenerator;
17+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
1618
use Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer;
1719
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
1820
use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer;
@@ -31,6 +33,10 @@
3133
param('kernel.debug'),
3234
])
3335

36+
->set('fragment.uri_generator', FragmentUriGenerator::class)
37+
->args([param('fragment.path'), service('uri_signer')])
38+
->alias(FragmentUriGeneratorInterface::class, 'fragment.uri_generator')
39+
3440
->set('fragment.renderer.inline', InlineFragmentRenderer::class)
3541
->args([service('http_kernel'), service('event_dispatcher')])
3642
->call('setFragmentPath', [param('fragment.path')])

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use Symfony\Component\HttpClient\ScopingHttpClient;
5050
use Symfony\Component\HttpFoundation\Session\SessionInterface;
5151
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
52+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
5253
use Symfony\Component\Messenger\Transport\TransportFactory;
5354
use Symfony\Component\PropertyAccess\PropertyAccessor;
5455
use Symfony\Component\Security\Core\Security;
@@ -181,6 +182,8 @@ public function testEsiDisabled()
181182
public function testFragmentsAndHinclude()
182183
{
183184
$container = $this->createContainerFromFile('fragments_and_hinclude');
185+
$this->assertTrue($container->has('fragment.uri_generator'));
186+
$this->assertTrue($container->hasAlias(FragmentUriGeneratorInterface::class));
184187
$this->assertTrue($container->hasParameter('fragment.renderer.hinclude.global_template'));
185188
$this->assertEquals('global_hinclude_template', $container->getParameter('fragment.renderer.hinclude.global_template'));
186189
}

Tests/Functional/Bundle/TestBundle/Controller/FragmentController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpKernel\Controller\ControllerReference;
19+
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
1820
use Twig\Environment;
1921

2022
class FragmentController implements ContainerAwareInterface
@@ -45,6 +47,11 @@ public function forwardLocaleAction(Request $request)
4547
{
4648
return new Response($request->getLocale());
4749
}
50+
51+
public function fragmentUriAction(Request $request, FragmentUriGeneratorInterface $fragmentUriGenerator)
52+
{
53+
return new Response($fragmentUriGenerator->generate(new ControllerReference(self::class.'::indexAction'), $request));
54+
}
4855
}
4956

5057
class Bar

Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ fragment_inlined:
5757
path: /fragment_inlined
5858
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::inlinedAction }
5959

60+
fragment_uri:
61+
path: /fragment_uri
62+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::fragmentUriAction }
63+
6064
array_controller:
6165
path: /array_controller
6266
defaults: { _controller: [ArrayController, someAction] }

Tests/Functional/FragmentTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public function getConfigs()
4444
[true],
4545
];
4646
}
47+
48+
public function testGenerateFragmentUri()
49+
{
50+
$client = self::createClient(['test_case' => 'Fragment', 'root_config' => 'config.yml', 'debug' => true]);
51+
$client->request('GET', '/fragment_uri');
52+
53+
$this->assertSame('/_fragment?_hash=CCRGN2D%2FoAJbeGz%2F%2FdoH3bNSPwLCrmwC1zAYCGIKJ0E%3D&_path=_format%3Dhtml%26_locale%3Den%26_controller%3DSymfony%255CBundle%255CFrameworkBundle%255CTests%255CFunctional%255CBundle%255CTestBundle%255CController%255CFragmentController%253A%253AindexAction', $client->getResponse()->getContent());
54+
}
4755
}

0 commit comments

Comments
 (0)