123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?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\DependencyInjection\ContainerAware;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- /**
- * ProfilerController.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- */
- class ProfilerController extends ContainerAware
- {
- /**
- * Renders a profiler panel for the given token.
- *
- * @param string $token The profiler token
- *
- * @return Response A Response instance
- */
- public function panelAction($token)
- {
- $this->container->get('profiler')->disable();
- $panel = $this->container->get('request')->query->get('panel', 'request');
- $profiler = $this->container->get('profiler')->loadFromToken($token);
- if ($profiler->isEmpty()) {
- return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:notfound.html.twig', array('token' => $token));
- }
- if (!$profiler->has($panel)) {
- throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel));
- }
- return $this->container->get('templating')->renderResponse($this->getTemplateName($profiler, $panel), array(
- 'token' => $token,
- 'profiler' => $profiler,
- 'collector' => $profiler->get($panel),
- 'panel' => $panel,
- 'templates' => $this->getTemplates($profiler),
- ));
- }
- /**
- * Exports data for a given token.
- *
- * @param string $token The profiler token
- *
- * @return Response A Response instance
- */
- public function exportAction($token)
- {
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $profiler = $profiler->loadFromToken($token);
- if ($profiler->isEmpty()) {
- throw new NotFoundHttpException(sprintf('Token "%s" does not exist.', $token));
- }
- return new Response($profiler->export(), 200, array(
- 'Content-Type' => 'text/plain',
- 'Content-Disposition' => 'attachment; filename= '.$token.'.txt',
- ));
- }
- /**
- * Purges all tokens.
- *
- * @return Response A Response instance
- */
- public function purgeAction()
- {
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $profiler->purge();
- return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => '-')));
- }
- /**
- * Imports token data.
- *
- * @return Response A Response instance
- */
- public function importAction()
- {
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $file = $this->container->get('request')->files->get('file');
- if (!$file || UPLOAD_ERR_OK !== $file->getError()) {
- throw new \RuntimeException('Problem uploading the data.');
- }
- $token = $profiler->import(file_get_contents($file->getPath()));
- if (false === $token) {
- throw new \RuntimeException('Problem uploading the data (token already exists).');
- }
- return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
- }
- /**
- * Renders the Web Debug Toolbar.
- *
- * @param string $token The profiler token
- * @param string $position The toolbar position (bottom, normal, or null -- automatically guessed)
- *
- * @return Response A Response instance
- */
- public function toolbarAction($token, $position = null)
- {
- $request = $this->container->get('request');
- // keep current flashes for one more request
- $request->getSession()->setFlashes($request->getSession()->getFlashes());
- if (null === $token) {
- return new Response();
- }
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $profiler = $profiler->loadFromToken($token);
- if ($profiler->isEmpty()) {
- return new Response();
- }
- if (null === $position) {
- $position = false === strpos($this->container->get('request')->headers->get('user-agent'), 'Mobile') ? 'fixed' : 'absolute';
- }
- $url = null;
- try {
- $url = $this->container->get('router')->generate('_profiler', array('token' => $token));
- } catch (\Exception $e) {
- // the profiler is not enabled
- }
- return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:toolbar.html.twig', array(
- 'position' => $position,
- 'profiler' => $profiler,
- 'templates' => $this->getTemplates($profiler),
- 'profiler_url' => $url,
- 'verbose' => $this->container->get('web_profiler.debug.toolbar')->getVerbose()
- ));
- }
- /**
- * Renders the profiler search bar.
- *
- * @return Response A Response instance
- */
- public function searchBarAction()
- {
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $session = $this->container->get('request')->getSession();
- $ip = $session->get('_profiler_search_ip');
- $url = $session->get('_profiler_search_url');
- $limit = $session->get('_profiler_search_limit');
- $token = $session->get('_profiler_search_token');
- return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array(
- 'token' => $token,
- 'ip' => $ip,
- 'url' => $url,
- 'limit' => $limit,
- ));
- }
- /**
- * Search results.
- *
- * @return Response A Response instance
- */
- public function searchResultsAction($token)
- {
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $pofiler = $profiler->loadFromToken($token);
- $session = $this->container->get('request')->getSession();
- $ip = $session->get('_profiler_search_ip');
- $url = $session->get('_profiler_search_url');
- $limit = $session->get('_profiler_search_limit');
- return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array(
- 'token' => $token,
- 'profiler' => $profiler,
- 'tokens' => $profiler->find($ip, $url, $limit),
- 'ip' => $ip,
- 'url' => $url,
- 'limit' => $limit,
- 'panel' => null,
- ));
- }
- /**
- * Narrow the search bar.
- *
- * @return Response A Response instance
- */
- public function searchAction()
- {
- $profiler = $this->container->get('profiler');
- $profiler->disable();
- $request = $this->container->get('request');
- $session = $request->getSession();
- $session->set('_profiler_search_ip', $ip = preg_replace('/[^\d\.]/', '', $request->query->get('ip')));
- $session->set('_profiler_search_url', $url = $request->query->get('url'));
- $session->set('_profiler_search_limit', $limit = $request->query->get('limit'));
- $session->set('_profiler_search_token', $token = $request->query->get('token'));
- if (!empty($token)) {
- return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
- }
- $tokens = $profiler->find($ip, $url, $limit);
- return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array('token' => $tokens ? $tokens[0]['token'] : 'empty')));
- }
- protected function getTemplateNames($profiler)
- {
- $templates = array();
- foreach ($this->container->getParameter('data_collector.templates') as $id => $arguments) {
- if (null === $arguments) {
- continue;
- }
- list($name, $template) = $arguments;
- if (!$profiler->has($name) || !$this->container->get('templating')->exists($template.'.html.twig')) {
- continue;
- }
- $templates[$name] = $template.'.html.twig';
- }
- return $templates;
- }
- protected function getTemplateName($profiler, $panel)
- {
- $templates = $this->getTemplateNames($profiler);
- if (!isset($templates[$panel])) {
- throw new NotFoundHttpException(sprintf('Panel "%s" is not registered.', $panel));
- }
- return $templates[$panel];
- }
- protected function getTemplates($profiler)
- {
- $templates = $this->getTemplateNames($profiler);
- foreach ($templates as $name => $template) {
- $templates[$name] = $this->container->get('twig')->loadTemplate($template);
- }
- return $templates;
- }
- }
|