PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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