RoutableFragmentRenderer.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\HttpKernel\Controller\ControllerReference;
  12. use Symfony\Component\HttpFoundation\Request;
  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. * @param string $path The path
  26. *
  27. * @see FragmentListener
  28. */
  29. public function setFragmentPath($path)
  30. {
  31. $this->fragmentPath = $path;
  32. }
  33. /**
  34. * Generates a fragment URI for a given controller.
  35. *
  36. * @param ControllerReference $reference A ControllerReference instance
  37. * @param Request $request A Request instance
  38. * @param bool $absolute Whether to generate an absolute URL or not
  39. * @param bool $strict Whether to allow non-scalar attributes or not
  40. *
  41. * @return string A fragment URI
  42. */
  43. protected function generateFragmentUri(ControllerReference $reference, Request $request, $absolute = false, $strict = true)
  44. {
  45. if ($strict) {
  46. $this->checkNonScalar($reference->attributes);
  47. }
  48. // We need to forward the current _format and _locale values as we don't have
  49. // a proper routing pattern to do the job for us.
  50. // This makes things inconsistent if you switch from rendering a controller
  51. // to rendering a route if the route pattern does not contain the special
  52. // _format and _locale placeholders.
  53. if (!isset($reference->attributes['_format'])) {
  54. $reference->attributes['_format'] = $request->getRequestFormat();
  55. }
  56. if (!isset($reference->attributes['_locale'])) {
  57. $reference->attributes['_locale'] = $request->getLocale();
  58. }
  59. $reference->attributes['_controller'] = $reference->controller;
  60. $reference->query['_path'] = http_build_query($reference->attributes, '', '&');
  61. $path = $this->fragmentPath.'?'.http_build_query($reference->query, '', '&');
  62. if ($absolute) {
  63. return $request->getUriForPath($path);
  64. }
  65. return $request->getBaseUrl().$path;
  66. }
  67. private function checkNonScalar($values)
  68. {
  69. foreach ($values as $key => $value) {
  70. if (is_array($value)) {
  71. $this->checkNonScalar($value);
  72. } elseif (!is_scalar($value) && null !== $value) {
  73. throw new \LogicException(sprintf('Controller attributes cannot contain non-scalar/non-null values (value for key "%s" is not a scalar or null).', $key));
  74. }
  75. }
  76. }
  77. }