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-08-29 06:19:56 +01:00
|
|
|
* @param Request $request A Request instance
|
2014-04-12 18:54:57 +01:00
|
|
|
* @param bool $absolute Whether to generate an absolute URL 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-08-29 06:19:56 +01:00
|
|
|
protected function generateFragmentUri(ControllerReference $reference, Request $request, $absolute = false)
|
2013-01-22 08:30:21 +00:00
|
|
|
{
|
2013-08-22 08:01:15 +01:00
|
|
|
// We need to forward the current _format and _locale values as we don't have
|
|
|
|
// a proper routing pattern to do the job for us.
|
|
|
|
// This makes things inconsistent if you switch from rendering a controller
|
|
|
|
// to rendering a route if the route pattern does not contain the special
|
|
|
|
// _format and _locale placeholders.
|
2013-01-22 08:30:21 +00:00
|
|
|
if (!isset($reference->attributes['_format'])) {
|
|
|
|
$reference->attributes['_format'] = $request->getRequestFormat();
|
|
|
|
}
|
2013-07-13 11:18:20 +01:00
|
|
|
if (!isset($reference->attributes['_locale'])) {
|
|
|
|
$reference->attributes['_locale'] = $request->getLocale();
|
|
|
|
}
|
|
|
|
|
2013-01-22 08:30:21 +00:00
|
|
|
$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-08-29 06:19:56 +01:00
|
|
|
$path = $this->fragmentPath.'?'.http_build_query($reference->query, '', '&');
|
|
|
|
|
|
|
|
if ($absolute) {
|
|
|
|
return $request->getUriForPath($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->getBaseUrl().$path;
|
2013-01-22 08:30:21 +00:00
|
|
|
}
|
|
|
|
}
|