ProfilerController.php 8.8 KB

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