RoutableFragmentRenderer.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Fragment;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  13. use Symfony\Component\HttpKernel\EventListener\FragmentListener;
  14. /**
  15. * Adds the possibility to generate a fragment URI for a given Controller.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. abstract class RoutableFragmentRenderer implements FragmentRendererInterface
  20. {
  21. private $fragmentPath = '/_fragment';
  22. /**
  23. * Sets the fragment path that triggers the fragment listener.
  24. *
  25. * @see FragmentListener
  26. */
  27. public function setFragmentPath(string $path)
  28. {
  29. $this->fragmentPath = $path;
  30. }
  31. /**
  32. * Generates a fragment URI for a given controller.
  33. *
  34. * @param bool $absolute Whether to generate an absolute URL or not
  35. * @param bool $strict Whether to allow non-scalar attributes or not
  36. *
  37. * @return string A fragment URI
  38. */
  39. protected function generateFragmentUri(ControllerReference $reference, Request $request, bool $absolute = false, bool $strict = true)
  40. {
  41. if ($strict) {
  42. $this->checkNonScalar($reference->attributes);
  43. }
  44. // We need to forward the current _format and _locale values as we don't have
  45. // a proper routing pattern to do the job for us.
  46. // This makes things inconsistent if you switch from rendering a controller
  47. // to rendering a route if the route pattern does not contain the special
  48. // _format and _locale placeholders.
  49. if (!isset($reference->attributes['_format'])) {
  50. $reference->attributes['_format'] = $request->getRequestFormat();
  51. }
  52. if (!isset($reference->attributes['_locale'])) {
  53. $reference->attributes['_locale'] = $request->getLocale();
  54. }
  55. $reference->attributes['_controller'] = $reference->controller;
  56. $reference->query['_path'] = http_build_query($reference->attributes, '', '&');
  57. $path = $this->fragmentPath.'?'.http_build_query($reference->query, '', '&');
  58. if ($absolute) {
  59. return $request->getUriForPath($path);
  60. }
  61. return $request->getBaseUrl().$path;
  62. }
  63. private function checkNonScalar(array $values)
  64. {
  65. foreach ($values as $key => $value) {
  66. if (\is_array($value)) {
  67. $this->checkNonScalar($value);
  68. } elseif (!is_scalar($value) && null !== $value) {
  69. throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key));
  70. }
  71. }
  72. }
  73. }