PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

https://gitlab.com/matijabelec/bigpandadev
PHP | 381 lines | 141 code | 38 blank | 202 comment | 10 complexity | 2c133a8ea2f8bb858582b765b962874d MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\StreamedResponse;
  15. use Symfony\Component\DependencyInjection\ContainerAware;
  16. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  17. use Symfony\Component\HttpKernel\HttpKernelInterface;
  18. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  19. use Symfony\Component\Security\Csrf\CsrfToken;
  20. use Symfony\Component\Form\FormTypeInterface;
  21. use Symfony\Component\Form\Form;
  22. use Symfony\Component\Form\FormBuilder;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  25. use Doctrine\Bundle\DoctrineBundle\Registry;
  26. /**
  27. * Controller is a simple implementation of a Controller.
  28. *
  29. * It provides methods to common features needed in controllers.
  30. *
  31. * @author Fabien Potencier <fabien@symfony.com>
  32. */
  33. class Controller extends ContainerAware
  34. {
  35. /**
  36. * Generates a URL from the given parameters.
  37. *
  38. * @param string $route The name of the route
  39. * @param mixed $parameters An array of parameters
  40. * @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
  41. *
  42. * @return string The generated URL
  43. *
  44. * @see UrlGeneratorInterface
  45. */
  46. public function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
  47. {
  48. return $this->container->get('router')->generate($route, $parameters, $referenceType);
  49. }
  50. /**
  51. * Forwards the request to another controller.
  52. *
  53. * @param string $controller The controller name (a string like BlogBundle:Post:index)
  54. * @param array $path An array of path parameters
  55. * @param array $query An array of query parameters
  56. *
  57. * @return Response A Response instance
  58. */
  59. public function forward($controller, array $path = array(), array $query = array())
  60. {
  61. $path['_controller'] = $controller;
  62. $subRequest = $this->container->get('request_stack')->getCurrentRequest()->duplicate($query, null, $path);
  63. return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  64. }
  65. /**
  66. * Returns a RedirectResponse to the given URL.
  67. *
  68. * @param string $url The URL to redirect to
  69. * @param int $status The status code to use for the Response
  70. *
  71. * @return RedirectResponse
  72. */
  73. public function redirect($url, $status = 302)
  74. {
  75. return new RedirectResponse($url, $status);
  76. }
  77. /**
  78. * Returns a RedirectResponse to the given route with the given parameters.
  79. *
  80. * @param string $route The name of the route
  81. * @param array $parameters An array of parameters
  82. * @param int $status The status code to use for the Response
  83. *
  84. * @return RedirectResponse
  85. */
  86. protected function redirectToRoute($route, array $parameters = array(), $status = 302)
  87. {
  88. return $this->redirect($this->generateUrl($route, $parameters), $status);
  89. }
  90. /**
  91. * Adds a flash message to the current session for type.
  92. *
  93. * @param string $type The type
  94. * @param string $message The message
  95. *
  96. * @throws \LogicException
  97. */
  98. protected function addFlash($type, $message)
  99. {
  100. if (!$this->container->has('session')) {
  101. throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
  102. }
  103. $this->container->get('session')->getFlashBag()->add($type, $message);
  104. }
  105. /**
  106. * Checks if the attributes are granted against the current authentication token and optionally supplied object.
  107. *
  108. * @param mixed $attributes The attributes
  109. * @param mixed $object The object
  110. *
  111. * @return bool
  112. *
  113. * @throws \LogicException
  114. */
  115. protected function isGranted($attributes, $object = null)
  116. {
  117. if (!$this->container->has('security.authorization_checker')) {
  118. throw new \LogicException('The SecurityBundle is not registered in your application.');
  119. }
  120. return $this->container->get('security.authorization_checker')->isGranted($attributes, $object);
  121. }
  122. /**
  123. * Throws an exception unless the attributes are granted against the current authentication token and optionally
  124. * supplied object.
  125. *
  126. * @param mixed $attributes The attributes
  127. * @param mixed $object The object
  128. * @param string $message The message passed to the exception
  129. *
  130. * @throws AccessDeniedException
  131. */
  132. protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
  133. {
  134. if (!$this->isGranted($attributes, $object)) {
  135. throw $this->createAccessDeniedException($message);
  136. }
  137. }
  138. /**
  139. * Returns a rendered view.
  140. *
  141. * @param string $view The view name
  142. * @param array $parameters An array of parameters to pass to the view
  143. *
  144. * @return string The rendered view
  145. */
  146. public function renderView($view, array $parameters = array())
  147. {
  148. return $this->container->get('templating')->render($view, $parameters);
  149. }
  150. /**
  151. * Renders a view.
  152. *
  153. * @param string $view The view name
  154. * @param array $parameters An array of parameters to pass to the view
  155. * @param Response $response A response instance
  156. *
  157. * @return Response A Response instance
  158. */
  159. public function render($view, array $parameters = array(), Response $response = null)
  160. {
  161. return $this->container->get('templating')->renderResponse($view, $parameters, $response);
  162. }
  163. /**
  164. * Streams a view.
  165. *
  166. * @param string $view The view name
  167. * @param array $parameters An array of parameters to pass to the view
  168. * @param StreamedResponse $response A response instance
  169. *
  170. * @return StreamedResponse A StreamedResponse instance
  171. */
  172. public function stream($view, array $parameters = array(), StreamedResponse $response = null)
  173. {
  174. $templating = $this->container->get('templating');
  175. $callback = function () use ($templating, $view, $parameters) {
  176. $templating->stream($view, $parameters);
  177. };
  178. if (null === $response) {
  179. return new StreamedResponse($callback);
  180. }
  181. $response->setCallback($callback);
  182. return $response;
  183. }
  184. /**
  185. * Returns a NotFoundHttpException.
  186. *
  187. * This will result in a 404 response code. Usage example:
  188. *
  189. * throw $this->createNotFoundException('Page not found!');
  190. *
  191. * @param string $message A message
  192. * @param \Exception|null $previous The previous exception
  193. *
  194. * @return NotFoundHttpException
  195. */
  196. public function createNotFoundException($message = 'Not Found', \Exception $previous = null)
  197. {
  198. return new NotFoundHttpException($message, $previous);
  199. }
  200. /**
  201. * Returns an AccessDeniedException.
  202. *
  203. * This will result in a 403 response code. Usage example:
  204. *
  205. * throw $this->createAccessDeniedException('Unable to access this page!');
  206. *
  207. * @param string $message A message
  208. * @param \Exception|null $previous The previous exception
  209. *
  210. * @return AccessDeniedException
  211. */
  212. public function createAccessDeniedException($message = 'Access Denied.', \Exception $previous = null)
  213. {
  214. return new AccessDeniedException($message, $previous);
  215. }
  216. /**
  217. * Creates and returns a Form instance from the type of the form.
  218. *
  219. * @param string|FormTypeInterface $type The built type of the form
  220. * @param mixed $data The initial data for the form
  221. * @param array $options Options for the form
  222. *
  223. * @return Form
  224. */
  225. public function createForm($type, $data = null, array $options = array())
  226. {
  227. return $this->container->get('form.factory')->create($type, $data, $options);
  228. }
  229. /**
  230. * Creates and returns a form builder instance.
  231. *
  232. * @param mixed $data The initial data for the form
  233. * @param array $options Options for the form
  234. *
  235. * @return FormBuilder
  236. */
  237. public function createFormBuilder($data = null, array $options = array())
  238. {
  239. return $this->container->get('form.factory')->createBuilder('form', $data, $options);
  240. }
  241. /**
  242. * Shortcut to return the request service.
  243. *
  244. * @return Request
  245. *
  246. * @deprecated since version 2.4, to be removed in 3.0.
  247. * Ask Symfony to inject the Request object into your controller
  248. * method instead by type hinting it in the method's signature.
  249. */
  250. public function getRequest()
  251. {
  252. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.4 and will be removed in 3.0. The only reliable way to get the "Request" object is to inject it in the action method.', E_USER_DEPRECATED);
  253. return $this->container->get('request_stack')->getCurrentRequest();
  254. }
  255. /**
  256. * Shortcut to return the Doctrine Registry service.
  257. *
  258. * @return Registry
  259. *
  260. * @throws \LogicException If DoctrineBundle is not available
  261. */
  262. public function getDoctrine()
  263. {
  264. if (!$this->container->has('doctrine')) {
  265. throw new \LogicException('The DoctrineBundle is not registered in your application.');
  266. }
  267. return $this->container->get('doctrine');
  268. }
  269. /**
  270. * Get a user from the Security Token Storage.
  271. *
  272. * @return mixed
  273. *
  274. * @throws \LogicException If SecurityBundle is not available
  275. *
  276. * @see TokenInterface::getUser()
  277. */
  278. public function getUser()
  279. {
  280. if (!$this->container->has('security.token_storage')) {
  281. throw new \LogicException('The SecurityBundle is not registered in your application.');
  282. }
  283. if (null === $token = $this->container->get('security.token_storage')->getToken()) {
  284. return;
  285. }
  286. if (!is_object($user = $token->getUser())) {
  287. // e.g. anonymous authentication
  288. return;
  289. }
  290. return $user;
  291. }
  292. /**
  293. * Returns true if the service id is defined.
  294. *
  295. * @param string $id The service id
  296. *
  297. * @return bool true if the service id is defined, false otherwise
  298. */
  299. public function has($id)
  300. {
  301. return $this->container->has($id);
  302. }
  303. /**
  304. * Gets a container service by its id.
  305. *
  306. * @param string $id The service id
  307. *
  308. * @return object The service
  309. */
  310. public function get($id)
  311. {
  312. if ('request' === $id) {
  313. @trigger_error('The "request" service is deprecated and will be removed in 3.0. Add a typehint for Symfony\\Component\\HttpFoundation\\Request to your controller parameters to retrieve the request instead.', E_USER_DEPRECATED);
  314. }
  315. return $this->container->get($id);
  316. }
  317. /**
  318. * Gets a container configuration parameter by its name.
  319. *
  320. * @param string $name The parameter name
  321. *
  322. * @return mixed
  323. */
  324. protected function getParameter($name)
  325. {
  326. return $this->container->getParameter($name);
  327. }
  328. /**
  329. * Checks the validity of a CSRF token.
  330. *
  331. * @param string $id The id used when generating the token
  332. * @param string $token The actual token sent with the request that should be validated
  333. *
  334. * @return bool
  335. */
  336. protected function isCsrfTokenValid($id, $token)
  337. {
  338. if (!$this->container->has('security.csrf.token_manager')) {
  339. throw new \LogicException('CSRF protection is not enabled in your application.');
  340. }
  341. return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
  342. }
  343. }