ProfilerController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. /**
  16. * ProfilerController.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ProfilerController extends ContainerAware
  21. {
  22. /**
  23. * Renders a profiler panel for the given token.
  24. *
  25. * @param string $token The profiler token
  26. *
  27. * @return Response A Response instance
  28. */
  29. public function panelAction($token)
  30. {
  31. $this->container->get('profiler')->disable();
  32. $panel = $this->container->get('request')->query->get('panel', 'request');
  33. $profiler = $this->container->get('profiler')->loadFromToken($token);
  34. if ($profiler->isEmpty()) {
  35. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound.html.twig', array('token' => $token));
  36. }
  37. if (!$profiler->has($panel)) {
  38. throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel));
  39. }
  40. return $this->container->get('templating')->renderResponse($this->getTemplateName($profiler, $panel), array(
  41. 'token' => $token,
  42. 'profiler' => $profiler,
  43. 'collector' => $profiler->get($panel),
  44. 'panel' => $panel,
  45. 'templates' => $this->getTemplates($profiler),
  46. ));
  47. }
  48. /**
  49. * Exports data for a given token.
  50. *
  51. * @param string $token The profiler token
  52. *
  53. * @return Response A Response instance
  54. */
  55. public function exportAction($token)
  56. {
  57. $profiler = $this->container->get('profiler');
  58. $profiler->disable();
  59. $profiler = $profiler->loadFromToken($token);
  60. if ($profiler->isEmpty()) {
  61. throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
  62. }
  63. return new Response($profiler->export(), 200, array(
  64. 'Content-Type' => 'text/plain',
  65. 'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
  66. ));
  67. }
  68. /**
  69. * Purges all tokens.
  70. *
  71. * @return Response A Response instance
  72. */
  73. public function purgeAction()
  74. {
  75. $profiler = $this->container->get('profiler');
  76. $profiler->disable();
  77. $profiler->purge();
  78. return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => '-')));
  79. }
  80. /**
  81. * Imports token data.
  82. *
  83. * @return Response A Response instance
  84. */
  85. public function importAction()
  86. {
  87. $profiler = $this->container->get('profiler');
  88. $profiler->disable();
  89. $file = $this->container->get('request')->files->get('file');
  90. if (!$file || UPLOAD_ERR_OK !== $file->getError()) {
  91. throw new \RuntimeException('Problem uploading the data.');
  92. }
  93. $token = $profiler->import(file_get_contents($file->getPath()));
  94. if (false === $token) {
  95. throw new \RuntimeException('Problem uploading the data (token already exists).');
  96. }
  97. return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
  98. }
  99. /**
  100. * Renders the Web Debug Toolbar.
  101. *
  102. * @param string $token The profiler token
  103. * @param string $position The toolbar position (bottom, normal, or null -- automatically guessed)
  104. *
  105. * @return Response A Response instance
  106. */
  107. public function toolbarAction($token, $position = null)
  108. {
  109. $request = $this->container->get('request');
  110. if (null !== $session = $request->getSession()) {
  111. // keep current flashes for one more request
  112. $session->setFlashes($session->getFlashes());
  113. }
  114. if (null === $token) {
  115. return new Response();
  116. }
  117. $profiler = $this->container->get('profiler');
  118. $profiler->disable();
  119. $profiler = $profiler->loadFromToken($token);
  120. if ($profiler->isEmpty()) {
  121. return new Response();
  122. }
  123. if (null === $position) {
  124. $position = false === strpos($this->container->get('request')->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute';
  125. }
  126. $url = null;
  127. try {
  128. $url = $this->container->get('router')->generate('_profiler', array('token' => $token));
  129. } catch (\Exception $e) {
  130. // the profiler is not enabled
  131. }
  132. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array(
  133. 'position' => $position,
  134. 'profiler' => $profiler,
  135. 'templates' => $this->getTemplates($profiler),
  136. 'profiler_url' => $url,
  137. 'verbose' => $this->container->get('web_profiler.debug.toolbar')->getVerbose()
  138. ));
  139. }
  140. /**
  141. * Renders the profiler search bar.
  142. *
  143. * @return Response A Response instance
  144. */
  145. public function searchBarAction()
  146. {
  147. $profiler = $this->container->get('profiler');
  148. $profiler->disable();
  149. if (null === $session = $this->container->get('request')->getSession()) {
  150. $ip =
  151. $url =
  152. $limit =
  153. $token = null;
  154. } else {
  155. $ip = $session->get('_profiler_search_ip');
  156. $url = $session->get('_profiler_search_url');
  157. $limit = $session->get('_profiler_search_limit');
  158. $token = $session->get('_profiler_search_token');
  159. }
  160. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array(
  161. 'token' => $token,
  162. 'ip' => $ip,
  163. 'url' => $url,
  164. 'limit' => $limit,
  165. ));
  166. }
  167. /**
  168. * Search results.
  169. *
  170. * @param string $token The token
  171. * @return Response A Response instance
  172. */
  173. public function searchResultsAction($token)
  174. {
  175. $profiler = $this->container->get('profiler');
  176. $profiler->disable();
  177. $profiler = $profiler->loadFromToken($token);
  178. $ip = $this->container->get('request')->query->get('ip');
  179. $url = $this->container->get('request')->query->get('url');
  180. $limit = $this->container->get('request')->query->get('limit');
  181. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array(
  182. 'token' => $token,
  183. 'profiler' => $profiler,
  184. 'tokens' => $profiler->find($ip, $url, $limit),
  185. 'ip' => $ip,
  186. 'url' => $url,
  187. 'limit' => $limit,
  188. 'panel' => null,
  189. ));
  190. }
  191. /**
  192. * Narrow the search bar.
  193. *
  194. * @return Response A Response instance
  195. */
  196. public function searchAction()
  197. {
  198. $profiler = $this->container->get('profiler');
  199. $profiler->disable();
  200. $request = $this->container->get('request');
  201. $ip = preg_replace('/[^\d\.]/', '', $request->query->get('ip'));
  202. $url = $request->query->get('url');
  203. $limit = $request->query->get('limit');
  204. $token = $request->query->get('token');
  205. if (null !== $session = $request->getSession()) {
  206. $session->set('_profiler_search_ip', $ip);
  207. $session->set('_profiler_search_url', $url);
  208. $session->set('_profiler_search_limit', $limit);
  209. $session->set('_profiler_search_token', $token);
  210. }
  211. if (!empty($token)) {
  212. return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
  213. }
  214. $tokens = $profiler->find($ip, $url, $limit);
  215. return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array(
  216. 'token' => $tokens ? $tokens[0]['token'] : 'empty',
  217. 'ip' => $ip,
  218. 'url' => $url,
  219. 'limit' => $limit,
  220. )));
  221. }
  222. protected function getTemplateNames($profiler)
  223. {
  224. $templates = array();
  225. foreach ($this->container->getParameter('data_collector.templates') as $id => $arguments) {
  226. if (null === $arguments) {
  227. continue;
  228. }
  229. list($name, $template) = $arguments;
  230. if (!$profiler->has($name) || !$this->container->get('templating')->exists($template.'.html.twig')) {
  231. continue;
  232. }
  233. $templates[$name] = $template.'.html.twig';
  234. }
  235. return $templates;
  236. }
  237. protected function getTemplateName($profiler, $panel)
  238. {
  239. $templates = $this->getTemplateNames($profiler);
  240. if (!isset($templates[$panel])) {
  241. throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel));
  242. }
  243. return $templates[$panel];
  244. }
  245. protected function getTemplates($profiler)
  246. {
  247. $templates = $this->getTemplateNames($profiler);
  248. foreach ($templates as $name => $template) {
  249. $templates[$name] = $this->container->get('twig')->loadTemplate($template);
  250. }
  251. return $templates;
  252. }
  253. }