ActionsHelper.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Symfony\Framework\WebBundle\Helper;
  3. use Symfony\Components\Templating\Helper\Helper;
  4. use Symfony\Components\DependencyInjection\ContainerInterface;
  5. use Symfony\Components\OutputEscaper\Escaper;
  6. use Symfony\Components\HttpKernel\HttpKernelInterface;
  7. use Symfony\Components\HttpKernel\Request;
  8. /*
  9. * This file is part of the Symfony framework.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * This source file is subject to the MIT license that is bundled
  14. * with this source code in the file LICENSE.
  15. */
  16. /**
  17. * ActionsHelper.
  18. *
  19. * @package Symfony
  20. * @subpackage Framework_WebBundle
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. */
  23. class ActionsHelper extends Helper
  24. {
  25. protected $container;
  26. /**
  27. * Constructor.
  28. *
  29. * @param Constructor $container A ContainerInterface instance
  30. */
  31. public function __construct(ContainerInterface $container)
  32. {
  33. $this->container = $container;
  34. }
  35. /**
  36. * Outputs the Response content for a given controller.
  37. *
  38. * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
  39. * @param array $options An array of options
  40. *
  41. * @see render()
  42. */
  43. public function output($controller, array $options = array())
  44. {
  45. echo $this->render($controller, $options);
  46. }
  47. /**
  48. * Returns the Response content for a given controller or URI.
  49. *
  50. * Available options:
  51. *
  52. * * path: An array of path parameters (only when the first argument is a controller)
  53. * * query: An array of query parameters (only when the first argument is a controller)
  54. * * ignore_errors: true to return an empty string in case of an error
  55. * * alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the path arguments, and the query arguments)
  56. *
  57. * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
  58. * @param array $options An array of options
  59. */
  60. public function render($controller, array $options = array())
  61. {
  62. $options = array_merge(array(
  63. 'path' => array(),
  64. 'query' => array(),
  65. 'ignore_errors' => true,
  66. 'alt' => array(),
  67. ), $options);
  68. if (!is_array($options['alt'])) {
  69. $options['alt'] = array($options['alt']);
  70. }
  71. $options['path'] = Escaper::unescape($options['path']);
  72. $options['query'] = Escaper::unescape($options['query']);
  73. return $this->doRender($controller, $options);
  74. }
  75. protected function doRender($controller, array $options = array())
  76. {
  77. // controller or URI?
  78. $request = $this->container->getRequestService();
  79. if (0 === strpos($controller, '/')) {
  80. // URI
  81. $subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all());
  82. } else {
  83. // controller
  84. $options['path']['_controller'] = $controller;
  85. $options['path']['_format'] = $request->getRequestFormat();
  86. $subRequest = $request->duplicate($options['query'], null, $options['path']);
  87. }
  88. try {
  89. return $this->container->getKernelService()->handle($subRequest, HttpKernelInterface::EMBEDDED_REQUEST, true);
  90. } catch (\Exception $e) {
  91. if ($options['alt']) {
  92. $alt = $options['alt'];
  93. unset($options['alt']);
  94. $options['path'] = isset($alt[1]) ? $alt[1] : array();
  95. $options['query'] = isset($alt[2]) ? $alt[2] : array();
  96. return $this->doRender($alt[0], $options);
  97. }
  98. if (!$options['ignore_errors']) {
  99. throw $e;
  100. }
  101. }
  102. }
  103. /**
  104. * Returns the canonical name of this helper.
  105. *
  106. * @return string The canonical name
  107. */
  108. public function getName()
  109. {
  110. return 'actions';
  111. }
  112. }