8000 [FrameworkBundle] Add fragment.uri_generator service · symfony/symfony@df1c1ff · GitHub
[go: up one dir, main page]

Skip to content

Commit df1c1ff

Browse files
committed
[FrameworkBundle] Add fragment.uri_generator service
1 parent ded6359 commit df1c1ff

File tree

7 files changed

+32
-3
lines changed

7 files changed

+32
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/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')])

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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] }

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Component/HttpKernel/Fragment/FragmentUriGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Fragment;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Controller\ControllerReference;
1516
use Symfony\Component\HttpKernel\UriSigner;
1617

1718
/**
@@ -34,7 +35,7 @@ public function __construct(string $fragmentPath, UriSigner $signer = null)
3435
/**
3536
* {@inheritDoc}
3637
*/
37-
public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string
38+
public function generate(ControllerReference $controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string
3839
{
3940
if ($sign && null === $this->signer) {
4041
throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');

src/Symfony/Component/HttpKernel/Fragment/FragmentUriGeneratorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ interface FragmentUriGeneratorInterface
2424
/**
2525
* Generates a fragment URI for a given controller.
2626
*
27-
* @param string|ControllerReference $controller The name of a controller as a string or a ControllerReference instance
27+
* @param ControllerReference $controller The name of a controller as a string or a ControllerReference instance
2828
* @param bool $absolute Whether to generate an absolute URL or not
2929
* @param bool $strict Whether to allow non-scalar attributes or not
3030
*
3131
* @return string A fragment URI
3232
*/
33-
public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string;
33+
public function generate(ControllerReference $controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string;
3434
}

0 commit comments

Comments
 (0)
0