2013-01-22 08:30:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2013-01-31 09:37:27 +00:00
|
|
|
namespace Symfony\Component\HttpKernel\Fragment;
|
2013-01-22 08:30:21 +00:00
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-01-31 09:37:27 +00:00
|
|
|
use Symfony\Component\HttpKernel\EventListener\FragmentListener;
|
2013-01-22 08:30:21 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-31 09:37:27 +00:00
|
|
|
* Adds the possibility to generate a fragment URI for a given Controller.
|
2013-01-22 08:30:21 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
2013-01-31 09:37:27 +00:00
|
|
|
abstract class RoutableFragmentRenderer implements FragmentRendererInterface
|
2013-01-22 08:30:21 +00:00
|
|
|
{
|
2013-01-31 09:37:27 +00:00
|
|
|
private $fragmentPath = '/_fragment';
|
2013-01-22 09:24:26 +00:00
|
|
|
|
|
|
|
/**
|
2013-01-31 09:37:27 +00:00
|
|
|
* Sets the fragment path that triggers the fragment listener.
|
2013-01-22 09:24:26 +00:00
|
|
|
*
|
|
|
|
* @param string $path The path
|
|
|
|
*
|
2013-01-31 09:37:27 +00:00
|
|
|
* @see FragmentListener
|
2013-01-22 09:24:26 +00:00
|
|
|
*/
|
2013-01-31 09:37:27 +00:00
|
|
|
public function setFragmentPath($path)
|
2013-01-22 09:24:26 +00:00
|
|
|
{
|
2013-01-31 09:37:27 +00:00
|
|
|
$this->fragmentPath = $path;
|
2013-01-22 09:24:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 08:30:21 +00:00
|
|
|
/**
|
2013-01-31 09:37:27 +00:00
|
|
|
* Generates a fragment URI for a given controller.
|
2013-01-22 08:30:21 +00:00
|
|
|
*
|
|
|
|
* @param ControllerReference $reference A ControllerReference instance
|
2013-07-08 07:42:57 +01:00
|
|
|
* @param Request $request A Request instance
|
|
|
|
* @param Boolean $strict Whether to allow non-scalar attributes or not
|
2013-01-22 08:30:21 +00:00
|
|
|
*
|
2013-01-31 09:37:27 +00:00
|
|
|
* @return string A fragment URI
|
2013-01-22 08:30:21 +00:00
|
|
|
*/
|
2013-07-08 07:42:57 +01:00
|
|
|
protected function generateFragmentUri(ControllerReference $reference, Request $request, $strict = true)
|
2013-01-22 08:30:21 +00:00
|
|
|
{
|
2013-07-08 07:42:57 +01:00
|
|
|
if ($strict) {
|
|
|
|
$this->checkNonScalar($reference->attributes);
|
|
|
|
}
|
|
|
|
|
2013-01-22 08:30:21 +00:00
|
|
|
if (!isset($reference->attributes['_format'])) {
|
|
|
|
$reference->attributes['_format'] = $request->getRequestFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
$reference->attributes['_controller'] = $reference->controller;
|
|
|
|
|
2013-01-23 06:53:21 +00:00
|
|
|
$reference->query['_path'] = http_build_query($reference->attributes, '', '&');
|
2013-01-22 08:30:21 +00:00
|
|
|
|
2013-01-31 09:37:27 +00:00
|
|
|
return $request->getUriForPath($this->fragmentPath.'?'.http_build_query($reference->query, '', '&'));
|
2013-01-22 08:30:21 +00:00
|
|
|
}
|
2013-07-08 07:42:57 +01:00
|
|
|
|
|
|
|
private function checkNonScalar($values)
|
|
|
|
{
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
$this->checkNonScalar($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_scalar($value)) {
|
|
|
|
throw new \LogicException('Controller attributes cannot contain non-scalar values.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 08:30:21 +00:00
|
|
|
}
|