Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\Fragment; Chris@0: Chris@17: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpKernel\Controller\ControllerReference; Chris@0: use Symfony\Component\HttpKernel\EventListener\FragmentListener; Chris@0: Chris@0: /** Chris@0: * Adds the possibility to generate a fragment URI for a given Controller. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class RoutableFragmentRenderer implements FragmentRendererInterface Chris@0: { Chris@0: private $fragmentPath = '/_fragment'; Chris@0: Chris@0: /** Chris@0: * Sets the fragment path that triggers the fragment listener. Chris@0: * Chris@0: * @param string $path The path Chris@0: * Chris@0: * @see FragmentListener Chris@0: */ Chris@0: public function setFragmentPath($path) Chris@0: { Chris@0: $this->fragmentPath = $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a fragment URI for a given controller. Chris@0: * Chris@0: * @param ControllerReference $reference A ControllerReference instance Chris@0: * @param Request $request A Request instance Chris@0: * @param bool $absolute Whether to generate an absolute URL or not Chris@0: * @param bool $strict Whether to allow non-scalar attributes or not Chris@0: * Chris@0: * @return string A fragment URI Chris@0: */ Chris@0: protected function generateFragmentUri(ControllerReference $reference, Request $request, $absolute = false, $strict = true) Chris@0: { Chris@0: if ($strict) { Chris@0: $this->checkNonScalar($reference->attributes); Chris@0: } Chris@0: Chris@0: // We need to forward the current _format and _locale values as we don't have Chris@0: // a proper routing pattern to do the job for us. Chris@0: // This makes things inconsistent if you switch from rendering a controller Chris@0: // to rendering a route if the route pattern does not contain the special Chris@0: // _format and _locale placeholders. Chris@0: if (!isset($reference->attributes['_format'])) { Chris@0: $reference->attributes['_format'] = $request->getRequestFormat(); Chris@0: } Chris@0: if (!isset($reference->attributes['_locale'])) { Chris@0: $reference->attributes['_locale'] = $request->getLocale(); Chris@0: } Chris@0: Chris@0: $reference->attributes['_controller'] = $reference->controller; Chris@0: Chris@0: $reference->query['_path'] = http_build_query($reference->attributes, '', '&'); Chris@0: Chris@0: $path = $this->fragmentPath.'?'.http_build_query($reference->query, '', '&'); Chris@0: Chris@0: if ($absolute) { Chris@0: return $request->getUriForPath($path); Chris@0: } Chris@0: Chris@0: return $request->getBaseUrl().$path; Chris@0: } Chris@0: Chris@0: private function checkNonScalar($values) Chris@0: { Chris@0: foreach ($values as $key => $value) { Chris@17: if (\is_array($value)) { Chris@0: $this->checkNonScalar($value); Chris@0: } elseif (!is_scalar($value) && null !== $value) { Chris@0: 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: } Chris@0: } Chris@0: } Chris@0: }