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

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

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