Без опису

RoutableFragmentRenderer.php 2.9KB

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