ProfilerController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. // keep current flashes for one more request
  111. $request->getSession()->setFlashes($request->getSession()->getFlashes());
  112. if (null === $token) {
  113. return new Response();
  114. }
  115. $profiler = $this->container->get('profiler');
  116. $profiler->disable();
  117. $profiler = $profiler->loadFromToken($token);
  118. if ($profiler->isEmpty()) {
  119. return new Response();
  120. }
  121. if (null === $position) {
  122. $position = false === strpos($this->container->get('request')->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute';
  123. }
  124. $url = null;
  125. try {
  126. $url = $this->container->get('router')->generate('_profiler', array('token' => $token));
  127. } catch (\Exception $e) {
  128. // the profiler is not enabled
  129. }
  130. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array(
  131. 'position' => $position,
  132. 'profiler' => $profiler,
  133. 'templates' => $this->getTemplates($profiler),
  134. 'profiler_url' => $url,
  135. 'verbose' => $this->container->get('web_profiler.debug.toolbar')->getVerbose()
  136. ));
  137. }
  138. /**
  139. * Renders the profiler search bar.
  140. *
  141. * @return Response A Response instance
  142. */
  143. public function searchBarAction()
  144. {
  145. $profiler = $this->container->get('profiler');
  146. $profiler->disable();
  147. $session = $this->container->get('request')->getSession();
  148. $ip = $session->get('_profiler_search_ip');
  149. $url = $session->get('_profiler_search_url');
  150. $limit = $session->get('_profiler_search_limit');
  151. $token = $session->get('_profiler_search_token');
  152. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array(
  153. 'token' => $token,
  154. 'ip' => $ip,
  155. 'url' => $url,
  156. 'limit' => $limit,
  157. ));
  158. }
  159. /**
  160. * Search results.
  161. *
  162. * @return Response A Response instance
  163. */
  164. public function searchResultsAction($token)
  165. {
  166. $profiler = $this->container->get('profiler');
  167. $profiler->disable();
  168. $pofiler = $profiler->loadFromToken($token);
  169. $session = $this->container->get('request')->getSession();
  170. $ip = $session->get('_profiler_search_ip');
  171. $url = $session->get('_profiler_search_url');
  172. $limit = $session->get('_profiler_search_limit');
  173. return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array(
  174. 'token' => $token,
  175. 'profiler' => $profiler,
  176. 'tokens' => $profiler->find($ip, $url, $limit),
  177. 'ip' => $ip,
  178. 'url' => $url,
  179. 'limit' => $limit,
  180. 'panel' => null,
  181. ));
  182. }
  183. /**
  184. * Narrow the search bar.
  185. *
  186. * @return Response A Response instance
  187. */
  188. public function searchAction()
  189. {
  190. $profiler = $this->container->get('profiler');
  191. $profiler->disable();
  192. $request = $this->container->get('request');
  193. $session = $request->getSession();
  194. $session->set('_profiler_search_ip', $ip = preg_replace('/[^\d\.]/', '', $request->query->get('ip')));
  195. $session->set('_profiler_search_url', $url = $request->query->get('url'));
  196. $session->set('_profiler_search_limit', $limit = $request->query->get('limit'));
  197. $session->set('_profiler_search_token', $token = $request->query->get('token'));
  198. if (!empty($token)) {
  199. return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
  200. }
  201. $tokens = $profiler->find($ip, $url, $limit);
  202. return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : 'empty')));
  203. }
  204. protected function getTemplateNames($profiler)
  205. {
  206. $templates = array();
  207. foreach ($this->container->getParameter('data_collector.templates') as $id => $arguments) {
  208. if (null === $arguments) {
  209. continue;
  210. }
  211. list($name, $template) = $arguments;
  212. if (!$profiler->has($name) || !$this->container->get('templating')->exists($template.'.html.twig')) {
  213. continue;
  214. }
  215. $templates[$name] = $template.'.html.twig';
  216. }
  217. return $templates;
  218. }
  219. protected function getTemplateName($profiler, $panel)
  220. {
  221. $templates = $this->getTemplateNames($profiler);
  222. if (!isset($templates[$panel])) {
  223. throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel));
  224. }
  225. return $templates[$panel];
  226. }
  227. protected function getTemplates($profiler)
  228. {
  229. $templates = $this->getTemplateNames($profiler);
  230. foreach ($templates as $name => $template) {
  231. $templates[$name] = $this->container->get('twig')->loadTemplate($template);
  232. }
  233. return $templates;
  234. }
  235. }