PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/Burgov/symfony
PHP | 330 lines | 199 code | 48 blank | 83 comment | 16 complexity | 6bf751fab890252ff6fa18461f77fde7 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. public function __construct(UrlGeneratorInterface $generator, Profiler $profiler, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
  33. {
  34. $this->generator = $generator;
  35. $this->profiler = $profiler;
  36. $this->twig = $twig;
  37. $this->templates = $templates;
  38. $this->toolbarPosition = $toolbarPosition;
  39. }
  40. /**
  41. * Renders a profiler panel for the given token.
  42. *
  43. * @param Request $request The HTTP request
  44. * @param string $token The profiler token
  45. *
  46. * @return Response A Response instance
  47. */
  48. public function panelAction(Request $request, $token)
  49. {
  50. $this->profiler->disable();
  51. $panel = $request->query->get('panel', 'request');
  52. $page = $request->query->get('page', 'home');
  53. if (!$profile = $this->profiler->loadProfile($token)) {
  54. return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array('about' => 'no_token', 'token' => $token)));
  55. }
  56. if (!$profile->hasCollector($panel)) {
  57. throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
  58. }
  59. return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
  60. 'token' => $token,
  61. 'profile' => $profile,
  62. 'collector' => $profile->getCollector($panel),
  63. 'panel' => $panel,
  64. 'page' => $page,
  65. 'request' => $request,
  66. 'templates' => $this->getTemplateManager()->getTemplates($profile),
  67. 'is_ajax' => $request->isXmlHttpRequest(),
  68. )));
  69. }
  70. /**
  71. * Exports data for a given token.
  72. *
  73. * @param string $token The profiler token
  74. *
  75. * @return Response A Response instance
  76. */
  77. public function exportAction($token)
  78. {
  79. $this->profiler->disable();
  80. if (!$profile = $this->profiler->loadProfile($token)) {
  81. throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
  82. }
  83. return new Response($this->profiler->export($profile), 200, array(
  84. 'Content-Type' => 'text/plain',
  85. 'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
  86. ));
  87. }
  88. /**
  89. * Purges all tokens.
  90. *
  91. * @return Response A Response instance
  92. */
  93. public function purgeAction()
  94. {
  95. $this->profiler->disable();
  96. $this->profiler->purge();
  97. return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'purge')));
  98. }
  99. /**
  100. * Imports token data.
  101. *
  102. * @return Response A Response instance
  103. */
  104. public function importAction(Request $request)
  105. {
  106. $this->profiler->disable();
  107. $file = $request->files->get('file');
  108. if (empty($file) || !$file->isValid()) {
  109. return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'upload_error')));
  110. }
  111. if (!$profile = $this->profiler->import(file_get_contents($file->getPathname()))) {
  112. return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'already_exists')));
  113. }
  114. return new RedirectResponse($this->generator->generate('_profiler', array('token' => $profile->getToken())));
  115. }
  116. /**
  117. * Displays information page.
  118. *
  119. * @param string $about
  120. *
  121. * @return Response A Response instance
  122. */
  123. public function infoAction($about)
  124. {
  125. $this->profiler->disable();
  126. return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
  127. 'about' => $about
  128. )));
  129. }
  130. /**
  131. * Renders the Web Debug Toolbar.
  132. *
  133. * @param Request $request The current Request
  134. * @param string $token The profiler token
  135. * @param string $position The toolbar position (top, bottom, normal, or null -- use the configuration)
  136. *
  137. * @return Response A Response instance
  138. */
  139. public function toolbarAction(Request $request, $token, $position = null)
  140. {
  141. $session = $request->getSession();
  142. if (null !== $session && $session->getFlashBag() instanceof AutoExpireFlashBag) {
  143. // keep current flashes for one more request if using AutoExpireFlashBag
  144. $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
  145. }
  146. if (null === $token) {
  147. return new Response();
  148. }
  149. $this->profiler->disable();
  150. if (!$profile = $this->profiler->loadProfile($token)) {
  151. return new Response();
  152. }
  153. if (null === $position) {
  154. $position = $this->toolbarPosition;
  155. }
  156. $url = null;
  157. try {
  158. $url = $this->generator->generate('_profiler', array('token' => $token));
  159. } catch (\Exception $e) {
  160. // the profiler is not enabled
  161. }
  162. return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
  163. 'position' => $position,
  164. 'profile' => $profile,
  165. 'templates' => $this->getTemplateManager()->getTemplates($profile),
  166. 'profiler_url' => $url,
  167. 'token' => $token,
  168. )));
  169. }
  170. /**
  171. * Renders the profiler search bar.
  172. *
  173. * @param Request $request The current Request
  174. *
  175. * @return Response A Response instance
  176. */
  177. public function searchBarAction(Request $request)
  178. {
  179. $this->profiler->disable();
  180. if (null === $session = $request->getSession()) {
  181. $ip =
  182. $method =
  183. $url =
  184. $limit =
  185. $token = null;
  186. } else {
  187. $ip = $session->get('_profiler_search_ip');
  188. $method = $session->get('_profiler_search_method');
  189. $url = $session->get('_profiler_search_url');
  190. $limit = $session->get('_profiler_search_limit');
  191. $token = $session->get('_profiler_search_token');
  192. }
  193. return new Response($this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
  194. 'token' => $token,
  195. 'ip' => $ip,
  196. 'method' => $method,
  197. 'url' => $url,
  198. 'limit' => $limit,
  199. )));
  200. }
  201. /**
  202. * Search results.
  203. *
  204. * @param Request $request The current Request
  205. * @param string $token The token
  206. *
  207. * @return Response A Response instance
  208. */
  209. public function searchResultsAction(Request $request, $token)
  210. {
  211. $this->profiler->disable();
  212. $profile = $this->profiler->loadProfile($token);
  213. $ip = $request->query->get('ip');
  214. $method = $request->query->get('method');
  215. $url = $request->query->get('url');
  216. $limit = $request->query->get('limit');
  217. return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
  218. 'token' => $token,
  219. 'profile' => $profile,
  220. 'tokens' => $this->profiler->find($ip, $url, $limit, $method),
  221. 'ip' => $ip,
  222. 'method' => $method,
  223. 'url' => $url,
  224. 'limit' => $limit,
  225. 'panel' => null,
  226. )));
  227. }
  228. /**
  229. * Narrow the search bar.
  230. *
  231. * @param Request $request The current Request
  232. *
  233. * @return Response A Response instance
  234. */
  235. public function searchAction(Request $request)
  236. {
  237. $this->profiler->disable();
  238. $ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
  239. $method = $request->query->get('method');
  240. $url = $request->query->get('url');
  241. $limit = $request->query->get('limit');
  242. $token = $request->query->get('token');
  243. if (null !== $session = $request->getSession()) {
  244. $session->set('_profiler_search_ip', $ip);
  245. $session->set('_profiler_search_method', $method);
  246. $session->set('_profiler_search_url', $url);
  247. $session->set('_profiler_search_limit', $limit);
  248. $session->set('_profiler_search_token', $token);
  249. }
  250. if (!empty($token)) {
  251. return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)));
  252. }
  253. $tokens = $this->profiler->find($ip, $url, $limit, $method);
  254. return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
  255. 'token' => $tokens ? $tokens[0]['token'] : 'empty',
  256. 'ip' => $ip,
  257. 'method' => $method,
  258. 'url' => $url,
  259. 'limit' => $limit,
  260. )));
  261. }
  262. /**
  263. * Displays the PHP info.
  264. *
  265. * @return Response A Response instance
  266. */
  267. public function phpinfoAction()
  268. {
  269. $this->profiler->disable();
  270. ob_start();
  271. phpinfo();
  272. $phpinfo = ob_get_clean();
  273. return new Response($phpinfo);
  274. }
  275. protected function getTemplateManager()
  276. {
  277. if (null === $this->templateManager) {
  278. $this->templateManager = new TemplateManager($this->profiler, $this->twig, $this->templates);
  279. }
  280. return $this->templateManager;
  281. }
  282. }