Skip to content

Commit

Permalink
[FrameworkBundle] Add fragment.uri_generator service
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Mar 24, 2021
1 parent ded6359 commit 982ba87
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler;
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
use Symfony\Component\HttpKernel\Fragment\FragmentUriGenerator;
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
use Symfony\Component\HttpKernel\Fragment\HIncludeFragmentRenderer;
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer;
Expand All @@ -31,6 +33,10 @@
param('kernel.debug'),
])

->set('fragment.uri_generator', FragmentUriGenerator::class)
->args([param('fragment.path'), service('uri_signer')])
->alias(FragmentUriGeneratorInterface::class, 'fragment.uri_generator')

->set('fragment.renderer.inline', InlineFragmentRenderer::class)
->args([service('http_kernel'), service('event_dispatcher')])
->call('setFragmentPath', [param('fragment.path')])
Expand Down
Expand Up @@ -49,6 +49,7 @@
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
use Symfony\Component\Messenger\Transport\TransportFactory;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Security\Core\Security;
Expand Down Expand Up @@ -181,6 +182,8 @@ public function testEsiDisabled()
public function testFragmentsAndHinclude()
{
$container = $this->createContainerFromFile('fragments_and_hinclude');
$this->assertTrue($container->has('fragment.uri_generator'));
$this->assertTrue($container->hasAlias(FragmentUriGeneratorInterface::class));
$this->assertTrue($container->hasParameter('fragment.renderer.hinclude.global_template'));
$this->assertEquals('global_hinclude_template', $container->getParameter('fragment.renderer.hinclude.global_template'));
}
Expand Down
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
use Twig\Environment;

class FragmentController implements ContainerAwareInterface
Expand Down Expand Up @@ -45,6 +47,11 @@ public function forwardLocaleAction(Request $request)
{
return new Response($request->getLocale());
}

public function fragmentUriAction(Request $request, FragmentUriGeneratorInterface $fragmentUriGenerator)
{
return new Response($fragmentUriGenerator->generate(new ControllerReference(self::class.'::indexAction'), $request));
}
}

class Bar
Expand Down
Expand Up @@ -57,6 +57,10 @@ fragment_inlined:
path: /fragment_inlined
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::inlinedAction }

fragment_uri:
path: /fragment_uri
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\FragmentController::fragmentUriAction }

array_controller:
path: /array_controller
defaults: { _controller: [ArrayController, someAction] }
Expand Down
Expand Up @@ -44,4 +44,12 @@ public function getConfigs()
[true],
];
}

public function testGenerateFragmentUri()
{
$client = self::createClient(['test_case' => 'Fragment', 'root_config' => 'config.yml', 'debug' => true]);
$client->request('GET', '/fragment_uri');

$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());
}
}
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Fragment;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpKernel\UriSigner;

/**
Expand All @@ -34,7 +35,7 @@ public function __construct(string $fragmentPath, UriSigner $signer = null)
/**
* {@inheritDoc}
*/
public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string
public function generate(ControllerReference $controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string
{
if ($sign && null === $this->signer) {
throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
Expand Down
Expand Up @@ -24,11 +24,11 @@ interface FragmentUriGeneratorInterface
/**
* Generates a fragment URI for a given controller.
*
* @param string|ControllerReference $controller The name of a controller as a string or a ControllerReference instance
* @param bool $absolute Whether to generate an absolute URL or not
* @param bool $strict Whether to allow non-scalar attributes or not
* @param ControllerReference $controller The name of a controller as a string or a ControllerReference instance
* @param bool $absolute Whether to generate an absolute URL or not
* @param bool $strict Whether to allow non-scalar attributes or not
*
* @return string A fragment URI
*/
public function generate($controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string;
public function generate(ControllerReference $controller, Request $request, bool $absolute = false, bool $strict = true, bool $sign = true): string;
}

0 comments on commit 982ba87

Please sign in to comment.