PageRenderTime 71ms CodeModel.GetById 43ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

https://github.com/tna/symfony
PHP | 383 lines | 224 code | 51 blank | 108 comment | 23 complexity | 3c2005419c1d2f4bda74a5cdc16ee9b4 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\WebProfilerBundle\Controller;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\Profiler\Profiler;
  15. use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. /**
  20. * ProfilerController.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class ProfilerController
  25. {
  26. private $templateManager;
  27. private $generator;
  28. private $profiler;
  29. private $twig;
  30. private $templates;
  31. private $toolbarPosition;
  32. /**
  33. * Constructor.
  34. *
  35. * @param UrlGeneratorInterface $generator The URL Generator
  36. * @param Profiler $profiler The profiler
  37. * @param \Twig_Environment $twig The twig environment
  38. * @param array $templates The templates
  39. * @param string $toolbarPosition The toolbar position (top, bottom, normal, or null -- use the configuration)
  40. */
  41. public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
  42. {
  43. $this->generator = $generator;
  44. $this->profiler = $profiler;
  45. $this->twig = $twig;
  46. $this->templates = $templates;
  47. $this->toolbarPosition = $toolbarPosition;
  48. }
  49. /**
  50. * Redirects to the last profiles.
  51. *
  52. * @return RedirectResponse A RedirectResponse instance
  53. *
  54. * @throws NotFoundHttpException
  55. */
  56. public function homeAction()
  57. {
  58. if (null === $this->profiler) {
  59. throw new NotFoundHttpException('The profiler must be enabled.');
  60. }
  61. $this->profiler->disable();
  62. return new RedirectResponse($this->generator->generate('_profiler_search_results', array('token' => 'empty', 'limit' => 10)), 302, array('Content-Type' => 'text/html'));
  63. }
  64. /**
  65. * Renders a profiler panel for the given token.
  66. *
  67. * @param Request $request The current HTTP request
  68. * @param string $token The profiler token
  69. *
  70. * @return Response A Response instance
  71. *
  72. * @throws NotFoundHttpException
  73. */
  74. public function panelAction(Request $request, $token)
  75. {
  76. if (null === $this->profiler) {
  77. throw new NotFoundHttpException('The profiler must be enabled.');
  78. }
  79. $this->profiler->disable();
  80. $panel = $request->query->get('panel', 'request');
  81. $page = $request->query->get('page', 'home');
  82. if (!$profile = $this->profiler->loadProfile($token)) {
  83. return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array('about' => 'no_token', 'token' => $token)), 200, array('Content-Type' => 'text/html'));
  84. }
  85. if (!$profile->hasCollector($panel)) {
  86. throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
  87. }
  88. return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
  89. 'token' => $token,
  90. 'profile' => $profile,
  91. 'collector' => $profile->getCollector($panel),
  92. 'panel' => $panel,
  93. 'page' => $page,
  94. 'request' => $request,
  95. 'templates' => $this->getTemplateManager()->getTemplates($profile),
  96. 'is_ajax' => $request->isXmlHttpRequest(),
  97. )), 200, array('Content-Type' => 'text/html'));
  98. }
  99. /**
  100. * Purges all tokens.
  101. *
  102. * @return Response A Response instance
  103. *
  104. * @throws NotFoundHttpException
  105. */
  106. public function purgeAction()
  107. {
  108. if (null === $this->profiler) {
  109. throw new NotFoundHttpException('The profiler must be enabled.');
  110. }
  111. $this->profiler->disable();
  112. $this->profiler->purge();
  113. return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'purge')), 302, array('Content-Type' => 'text/html'));
  114. }
  115. /**
  116. * Displays information page.
  117. *
  118. * @param string $about The about message
  119. *
  120. * @return Response A Response instance
  121. *
  122. * @throws NotFoundHttpException
  123. */
  124. public function infoAction($about)
  125. {
  126. if (null === $this->profiler) {
  127. throw new NotFoundHttpException('The profiler must be enabled.');
  128. }
  129. $this->profiler->disable();
  130. return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
  131. 'about' => $about,
  132. )), 200, array('Content-Type' => 'text/html'));
  133. }
  134. /**
  135. * Renders the Web Debug Toolbar.
  136. *
  137. * @param Request $request The current HTTP Request
  138. * @param string $token The profiler token
  139. *
  140. * @return Response A Response instance
  141. *
  142. * @throws NotFoundHttpException
  143. */
  144. public function toolbarAction(Request $request, $token)
  145. {
  146. if (null === $this->profiler) {
  147. throw new NotFoundHttpException('The profiler must be enabled.');
  148. }
  149. $session = $request->getSession();
  150. if (null !== $session && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
  151. // keep current flashes for one more request if using AutoExpireFlashBag
  152. $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
  153. }
  154. if ('empty' === $token || null === $token) {
  155. return new Response('', 200, array('Content-Type' => 'text/html'));
  156. }
  157. $this->profiler->disable();
  158. if (!$profile = $this->profiler->loadProfile($token)) {
  159. return new Response('', 404, array('Content-Type' => 'text/html'));
  160. }
  161. // the toolbar position (top, bottom, normal, or null -- use the configuration)
  162. if (null === $position = $request->query->get('position')) {
  163. $position = $this->toolbarPosition;
  164. }
  165. $url = null;
  166. try {
  167. $url = $this->generator->generate('_profiler', array('token' => $token));
  168. } catch (\Exception $e) {
  169. // the profiler is not enabled
  170. }
  171. return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
  172. 'position' => $position,
  173. 'profile' => $profile,
  174. 'templates' => $this->getTemplateManager()->getTemplates($profile),
  175. 'profiler_url' => $url,
  176. 'token' => $token,
  177. )), 200, array('Content-Type' => 'text/html'));
  178. }
  179. /**
  180. * Renders the profiler search bar.
  181. *
  182. * @param Request $request The current HTTP Request
  183. *
  184. * @return Response A Response instance
  185. *
  186. * @throws NotFoundHttpException
  187. */
  188. public function searchBarAction(Request $request)
  189. {
  190. if (null === $this->profiler) {
  191. throw new NotFoundHttpException('The profiler must be enabled.');
  192. }
  193. $this->profiler->disable();
  194. if (null === $session = $request->getSession()) {
  195. $ip =
  196. $method =
  197. $url =
  198. $start =
  199. $end =
  200. $limit =
  201. $token = null;
  202. } else {
  203. $ip = $session->get('_profiler_search_ip');
  204. $method = $session->get('_profiler_search_method');
  205. $url = $session->get('_profiler_search_url');
  206. $start = $session->get('_profiler_search_start');
  207. $end = $session->get('_profiler_search_end');
  208. $limit = $session->get('_profiler_search_limit');
  209. $token = $session->get('_profiler_search_token');
  210. }
  211. return new Response($this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
  212. 'token' => $token,
  213. 'ip' => $ip,
  214. 'method' => $method,
  215. 'url' => $url,
  216. 'start' => $start,
  217. 'end' => $end,
  218. 'limit' => $limit,
  219. )), 200, array('Content-Type' => 'text/html'));
  220. }
  221. /**
  222. * Renders the search results.
  223. *
  224. * @param Request $request The current HTTP Request
  225. * @param string $token The token
  226. *
  227. * @return Response A Response instance
  228. *
  229. * @throws NotFoundHttpException
  230. */
  231. public function searchResultsAction(Request $request, $token)
  232. {
  233. if (null === $this->profiler) {
  234. throw new NotFoundHttpException('The profiler must be enabled.');
  235. }
  236. $this->profiler->disable();
  237. $profile = $this->profiler->loadProfile($token);
  238. $ip = $request->query->get('ip');
  239. $method = $request->query->get('method');
  240. $url = $request->query->get('url');
  241. $start = $request->query->get('start', null);
  242. $end = $request->query->get('end', null);
  243. $limit = $request->query->get('limit');
  244. return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
  245. 'token' => $token,
  246. 'profile' => $profile,
  247. 'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
  248. 'ip' => $ip,
  249. 'method' => $method,
  250. 'url' => $url,
  251. 'start' => $start,
  252. 'end' => $end,
  253. 'limit' => $limit,
  254. 'panel' => null,
  255. )), 200, array('Content-Type' => 'text/html'));
  256. }
  257. /**
  258. * Narrows the search bar.
  259. *
  260. * @param Request $request The current HTTP Request
  261. *
  262. * @return Response A Response instance
  263. *
  264. * @throws NotFoundHttpException
  265. */
  266. public function searchAction(Request $request)
  267. {
  268. if (null === $this->profiler) {
  269. throw new NotFoundHttpException('The profiler must be enabled.');
  270. }
  271. $this->profiler->disable();
  272. $ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
  273. $method = $request->query->get('method');
  274. $url = $request->query->get('url');
  275. $start = $request->query->get('start', null);
  276. $end = $request->query->get('end', null);
  277. $limit = $request->query->get('limit');
  278. $token = $request->query->get('token');
  279. if (null !== $session = $request->getSession()) {
  280. $session->set('_profiler_search_ip', $ip);
  281. $session->set('_profiler_search_method', $method);
  282. $session->set('_profiler_search_url', $url);
  283. $session->set('_profiler_search_start', $start);
  284. $session->set('_profiler_search_end', $end);
  285. $session->set('_profiler_search_limit', $limit);
  286. $session->set('_profiler_search_token', $token);
  287. }
  288. if (!empty($token)) {
  289. return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)), 302, array('Content-Type' => 'text/html'));
  290. }
  291. $tokens = $this->profiler->find($ip, $url, $limit, $method, $start, $end);
  292. return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
  293. 'token' => $tokens ? $tokens[0]['token'] : 'empty',
  294. 'ip' => $ip,
  295. 'method' => $method,
  296. 'url' => $url,
  297. 'start' => $start,
  298. 'end' => $end,
  299. 'limit' => $limit,
  300. )), 302, array('Content-Type' => 'text/html'));
  301. }
  302. /**
  303. * Displays the PHP info.
  304. *
  305. * @return Response A Response instance
  306. *
  307. * @throws NotFoundHttpException
  308. */
  309. public function phpinfoAction()
  310. {
  311. if (null === $this->profiler) {
  312. throw new NotFoundHttpException('The profiler must be enabled.');
  313. }
  314. $this->profiler->disable();
  315. ob_start();
  316. phpinfo();
  317. $phpinfo = ob_get_clean();
  318. return new Response($phpinfo, 200, array('Content-Type' => 'text/html'));
  319. }
  320. /**
  321. * Gets the Template Manager.
  322. *
  323. * @return TemplateManager The Template Manager
  324. */
  325. protected function getTemplateManager()
  326. {
  327. if (null === $this->templateManager) {
  328. $this->templateManager = new TemplateManager($this->profiler, $this->twig, $this->templates);
  329. }
  330. return $this->templateManager;
  331. }
  332. }