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