123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bundle\WebProfilerBundle\Controller;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\HttpKernel\Profiler\Profiler;
- use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- /**
- * ProfilerController.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class ProfilerController
- {
- private $templateManager;
- private $generator;
- private $profiler;
- private $twig;
- private $templates;
- private $toolbarPosition;
- /**
- * Constructor.
- *
- * @param UrlGeneratorInterface $generator The Url Generator
- * @param Profiler $profiler The profiler
- * @param \Twig_Environment $twig The twig environment
- * @param array $templates The templates
- * @param string $toolbarPosition The toolbar position (top, bottom, normal, or null -- use the configuration)
- */
- public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
- {
- $this->generator = $generator;
- $this->profiler = $profiler;
- $this->twig = $twig;
- $this->templates = $templates;
- $this->toolbarPosition = $toolbarPosition;
- }
- /**
- * Redirects to the last profiles.
- *
- * @return RedirectResponse A RedirectResponse instance
- */
- public function homeAction()
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- return new RedirectResponse($this->generator->generate('_profiler_search_results', array('token' => 'empty', 'limit' => 10)), 302, array('Content-Type' => 'text/html'));
- }
- /**
- * Renders a profiler panel for the given token.
- *
- * @param Request $request The current HTTP request
- * @param string $token The profiler token
- *
- * @return Response A Response instance
- *
- * @throws NotFoundHttpException
- */
- public function panelAction(Request $request, $token)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- $panel = $request->query->get('panel', 'request');
- $page = $request->query->get('page', 'home');
- if (!$profile = $this->profiler->loadProfile($token)) {
- return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array('about' => 'no_token', 'token' => $token)), 200, array('Content-Type' => 'text/html'));
- }
- if (!$profile->hasCollector($panel)) {
- throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
- }
- return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), array(
- 'token' => $token,
- 'profile' => $profile,
- 'collector' => $profile->getCollector($panel),
- 'panel' => $panel,
- 'page' => $page,
- 'request' => $request,
- 'templates' => $this->getTemplateManager()->getTemplates($profile),
- 'is_ajax' => $request->isXmlHttpRequest(),
- )), 200, array('Content-Type' => 'text/html'));
- }
- /**
- * Exports data for a given token.
- *
- * @param string $token The profiler token
- *
- * @return Response A Response instance
- *
- * @throws NotFoundHttpException
- */
- public function exportAction($token)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- if (!$profile = $this->profiler->loadProfile($token)) {
- throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
- }
- return new Response($this->profiler->export($profile), 200, array(
- 'Content-Type' => 'text/plain',
- 'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
- ));
- }
- /**
- * Purges all tokens.
- *
- * @return Response A Response instance
- */
- public function purgeAction()
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- $this->profiler->purge();
- return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'purge')), 302, array('Content-Type' => 'text/html'));
- }
- /**
- * Imports token data.
- *
- * @param Request $request The current HTTP Request
- *
- * @return Response A Response instance
- */
- public function importAction(Request $request)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- $file = $request->files->get('file');
- if (empty($file) || !$file->isValid()) {
- return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'upload_error')), 302, array('Content-Type' => 'text/html'));
- }
- if (!$profile = $this->profiler->import(file_get_contents($file->getPathname()))) {
- return new RedirectResponse($this->generator->generate('_profiler_info', array('about' => 'already_exists')), 302, array('Content-Type' => 'text/html'));
- }
- return new RedirectResponse($this->generator->generate('_profiler', array('token' => $profile->getToken())), 302, array('Content-Type' => 'text/html'));
- }
- /**
- * Displays information page.
- *
- * @param string $about The about message
- *
- * @return Response A Response instance
- */
- public function infoAction($about)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
- 'about' => $about
- )), 200, array('Content-Type' => 'text/html'));
- }
- /**
- * Renders the Web Debug Toolbar.
- *
- * @param Request $request The current HTTP Request
- * @param string $token The profiler token
- *
- * @return Response A Response instance
- */
- public function toolbarAction(Request $request, $token)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $session = $request->getSession();
- if (null !== $session && $session->getFlashBag() instanceof AutoExpireFlashBag) {
- // keep current flashes for one more request if using AutoExpireFlashBag
- $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
- }
- if (null === $token) {
- return new Response('', 200, array('Content-Type' => 'text/html'));
- }
- $this->profiler->disable();
- if (!$profile = $this->profiler->loadProfile($token)) {
- return new Response('', 200, array('Content-Type' => 'text/html'));
- }
- // the toolbar position (top, bottom, normal, or null -- use the configuration)
- if (null === $position = $request->query->get('position')) {
- $position = $this->toolbarPosition;
- }
- $url = null;
- try {
- $url = $this->generator->generate('_profiler', array('token' => $token));
- } catch (\Exception $e) {
- // the profiler is not enabled
- }
- return new Response($this->twig->render('@WebProfiler/Profiler/toolbar.html.twig', array(
- 'position' => $position,
- 'profile' => $profile,
- 'templates' => $this->getTemplateManager()->getTemplates($profile),
- 'profiler_url' => $url,
- 'token' => $token,
- )), 200, array('Content-Type' => 'text/html'));
- }
- /**
- * Renders the profiler search bar.
- *
- * @param Request $request The current HTTP Request
- *
- * @return Response A Response instance
- */
- public function searchBarAction(Request $request)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- if (null === $session = $request->getSession()) {
- $ip =
- $method =
- $url =
- $start =
- $end =
- $limit =
- $token = null;
- } else {
- $ip = $session->get('_profiler_search_ip');
- $method = $session->get('_profiler_search_method');
- $url = $session->get('_profiler_search_url');
- $start = $session->get('_profiler_search_start');
- $end = $session->get('_profiler_search_end');
- $limit = $session->get('_profiler_search_limit');
- $token = $session->get('_profiler_search_token');
- }
- return new Response($this->twig->render('@WebProfiler/Profiler/search.html.twig', array(
- 'token' => $token,
- 'ip' => $ip,
- 'method' => $method,
- 'url' => $url,
- 'start' => $start,
- 'end' => $end,
- 'limit' => $limit,
- )), 200, array('Content-Type' => 'text/html'));
- }
- /**
- * Search results.
- *
- * @param Request $request The current HTTP Request
- * @param string $token The token
- *
- * @return Response A Response instance
- */
- public function searchResultsAction(Request $request, $token)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- $profile = $this->profiler->loadProfile($token);
- $ip = $request->query->get('ip');
- $method = $request->query->get('method');
- $url = $request->query->get('url');
- $start = $request->query->get('start', null);
- $end = $request->query->get('end', null);
- $limit = $request->query->get('limit');
- return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', array(
- 'token' => $token,
- 'profile' => $profile,
- 'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
- 'ip' => $ip,
- 'method' => $method,
- 'url' => $url,
- 'start' => $start,
- 'end' => $end,
- 'limit' => $limit,
- 'panel' => null,
- )), 200, array('Content-Type' => 'text/html'));
- }
- /**
- * Narrow the search bar.
- *
- * @param Request $request The current HTTP Request
- *
- * @return Response A Response instance
- */
- public function searchAction(Request $request)
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- $ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
- $method = $request->query->get('method');
- $url = $request->query->get('url');
- $start = $request->query->get('start', null);
- $end = $request->query->get('end', null);
- $limit = $request->query->get('limit');
- $token = $request->query->get('token');
- if (null !== $session = $request->getSession()) {
- $session->set('_profiler_search_ip', $ip);
- $session->set('_profiler_search_method', $method);
- $session->set('_profiler_search_url', $url);
- $session->set('_profiler_search_start', $start);
- $session->set('_profiler_search_end', $end);
- $session->set('_profiler_search_limit', $limit);
- $session->set('_profiler_search_token', $token);
- }
- if (!empty($token)) {
- return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)), 302, array('Content-Type' => 'text/html'));
- }
- $tokens = $this->profiler->find($ip, $url, $limit, $method, $start, $end);
- return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
- 'token' => $tokens ? $tokens[0]['token'] : 'empty',
- 'ip' => $ip,
- 'method' => $method,
- 'url' => $url,
- 'start' => $start,
- 'end' => $end,
- 'limit' => $limit,
- )), 302, array('Content-Type' => 'text/html'));
- }
- /**
- * Displays the PHP info.
- *
- * @return Response A Response instance
- */
- public function phpinfoAction()
- {
- if (null === $this->profiler) {
- throw new NotFoundHttpException('The profiler must be enabled.');
- }
- $this->profiler->disable();
- ob_start();
- phpinfo();
- $phpinfo = ob_get_clean();
- return new Response($phpinfo, 200, array('Content-Type' => 'text/html'));
- }
- /**
- * Gets the Template Manager.
- *
- * @return TemplateManager The Template Manager
- */
- protected function getTemplateManager()
- {
- if (null === $this->templateManager) {
- $this->templateManager = new TemplateManager($this->profiler, $this->twig, $this->templates);
- }
- return $this->templateManager;
- }
- }
|