2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Framework\WebBundle\Helper;
|
|
|
|
|
|
|
|
use Symfony\Components\Templating\Helper\Helper;
|
|
|
|
use Symfony\Components\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Components\OutputEscaper\Escaper;
|
2010-05-13 19:23:23 +01:00
|
|
|
use Symfony\Components\HttpKernel\HttpKernelInterface;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-07 01:51:29 +01:00
|
|
|
* ActionsHelper.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Framework_WebBundle
|
2010-02-17 13:55:05 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class ActionsHelper extends Helper
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param Constructor $container A ContainerInterface instance
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
2010-05-13 07:29:04 +01:00
|
|
|
/**
|
|
|
|
* Outputs the Response content for a given controller.
|
|
|
|
*
|
|
|
|
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index)
|
|
|
|
* @param array $path An array of path parameters
|
|
|
|
* @param array $query An array of query parameters
|
2010-05-13 19:23:23 +01:00
|
|
|
* @param array $options An array of options
|
2010-05-13 07:29:04 +01:00
|
|
|
*
|
|
|
|
* @see render()
|
|
|
|
*/
|
2010-05-13 19:23:23 +01:00
|
|
|
public function output($controller, array $path = array(), array $query = array(), array $options = array())
|
|
|
|
{
|
|
|
|
echo $this->render($controller, $path, $query, $options);
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-05-13 07:29:04 +01:00
|
|
|
/**
|
|
|
|
* Returns the Response content for a given controller.
|
|
|
|
*
|
2010-05-13 19:23:23 +01:00
|
|
|
* Available options:
|
|
|
|
*
|
|
|
|
* * ignore_errors: true to return an empty string in case of an error
|
|
|
|
* * alt: an alternative controller to execute in case of an error (an array with the controller, the path arguments, the query arguments)
|
|
|
|
*
|
2010-05-13 07:29:04 +01:00
|
|
|
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index)
|
|
|
|
* @param array $path An array of path parameters
|
|
|
|
* @param array $query An array of query parameters
|
2010-05-13 19:23:23 +01:00
|
|
|
* @param array $options An array of options
|
2010-05-13 07:29:04 +01:00
|
|
|
*/
|
2010-05-13 19:23:23 +01:00
|
|
|
public function render($controller, array $path = array(), array $query = array(), array $options = array())
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-05-13 19:23:23 +01:00
|
|
|
$options = array_merge(array(
|
|
|
|
'ignore_errors' => true,
|
|
|
|
'alt' => array(),
|
|
|
|
), $options);
|
|
|
|
|
|
|
|
if (!is_array($options['alt']))
|
|
|
|
{
|
|
|
|
$options['alt'] = array($options['alt']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = Escaper::unescape($path);
|
|
|
|
$query = Escaper::unescape($query);
|
|
|
|
|
|
|
|
$request = $this->container->getRequestService();
|
2010-05-13 07:29:04 +01:00
|
|
|
$path['_controller'] = $controller;
|
2010-05-13 19:23:23 +01:00
|
|
|
$path['_format'] = $request->getRequestFormat();
|
|
|
|
$subRequest = $request->duplicate($query, null, $path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
if ($options['alt']) {
|
|
|
|
$alt = $options['alt'];
|
|
|
|
unset($options['alt']);
|
|
|
|
|
|
|
|
return $this->render($alt[0], isset($alt[1]) ? $alt[1] : array(), isset($alt[2]) ? $alt[2] : array(), $options);
|
|
|
|
}
|
2010-05-13 07:29:04 +01:00
|
|
|
|
2010-05-13 19:23:23 +01:00
|
|
|
if (!$options['ignore_errors']) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the canonical name of this helper.
|
|
|
|
*
|
|
|
|
* @return string The canonical name
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'actions';
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|