WebProfilerServiceProvider.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /*
  3. * This file is part of the Silex framework.
  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 Silex\Provider;
  11. use Symfony\Bundle\WebProfilerBundle\Controller\ExceptionController;
  12. use Symfony\Bundle\WebProfilerBundle\Controller\RouterController;
  13. use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
  14. use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
  15. use Symfony\Component\HttpKernel\Profiler\Profiler;
  16. use Symfony\Component\HttpKernel\EventListener\ProfilerListener;
  17. use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
  18. use Symfony\Component\HttpKernel\DataCollector\ConfigDataCollector;
  19. use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector;
  20. use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
  21. use Symfony\Component\HttpKernel\DataCollector\RouterDataCollector;
  22. use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
  23. use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
  24. use Symfony\Component\HttpKernel\DataCollector\LoggerDataCollector;
  25. use Symfony\Component\HttpKernel\DataCollector\EventDataCollector;
  26. use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
  27. use Symfony\Component\Stopwatch\Stopwatch;
  28. use Symfony\Bridge\Twig\Extension\CodeExtension;
  29. use Silex\Application;
  30. use Silex\ServiceProviderInterface;
  31. use Silex\ControllerProviderInterface;
  32. use Silex\ServiceControllerResolver;
  33. /**
  34. * Symfony Web Profiler provider.
  35. *
  36. * @author Fabien Potencier <fabien@symfony.com>
  37. */
  38. class WebProfilerServiceProvider implements ServiceProviderInterface, ControllerProviderInterface
  39. {
  40. public function register(Application $app)
  41. {
  42. $app['profiler.mount_prefix'] = '/_profiler';
  43. $app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) {
  44. $dispatcher = new TraceableEventDispatcher($dispatcher, $app['stopwatch'], $app['logger']);
  45. $dispatcher->setProfiler($app['profiler']);
  46. return $dispatcher;
  47. }));
  48. $app['data_collector.templates'] = array(
  49. array('config', '@WebProfiler/Collector/config.html.twig'),
  50. array('request', '@WebProfiler/Collector/request.html.twig'),
  51. array('exception', '@WebProfiler/Collector/exception.html.twig'),
  52. array('events', '@WebProfiler/Collector/events.html.twig'),
  53. array('logger', '@WebProfiler/Collector/logger.html.twig'),
  54. array('time', '@WebProfiler/Collector/time.html.twig'),
  55. array('router', '@WebProfiler/Collector/router.html.twig'),
  56. array('memory', '@WebProfiler/Collector/memory.html.twig'),
  57. );
  58. $app['data_collectors'] = array(
  59. 'config' => $app->share(function ($app) { return new ConfigDataCollector(); }),
  60. 'request' => $app->share(function ($app) { return new RequestDataCollector($app); }),
  61. 'exception' => $app->share(function ($app) { return new ExceptionDataCollector(); }),
  62. 'events' => $app->share(function ($app) { return new EventDataCollector(); }),
  63. 'logger' => $app->share(function ($app) { return new LoggerDataCollector($app['logger']); }),
  64. 'time' => $app->share(function ($app) { return new TimeDataCollector(); }),
  65. 'router' => $app->share(function ($app) { return new RouterDataCollector(); }),
  66. 'memory' => $app->share(function ($app) { return new MemoryDataCollector(); }),
  67. );
  68. $app['web_profiler.controller.profiler'] = $app->share(function ($app) {
  69. return new ProfilerController($app['url_generator'], $app['profiler'], $app['twig'], $app['data_collector.templates'], $app['web_profiler.debug_toolbar.position']);
  70. });
  71. $app['web_profiler.controller.router'] = $app->share(function ($app) {
  72. return new RouterController($app['profiler'], $app['twig'], isset($app['url_matcher']) ? $app['url_matcher'] : null, $app['routes']);
  73. });
  74. $app['web_profiler.controller.exception'] = $app->share(function ($app) {
  75. return new ExceptionController($app['profiler'], $app['twig'], $app['debug']);
  76. });
  77. $app['web_profiler.toolbar.listener'] = $app->share(function ($app) {
  78. return new WebDebugToolbarListener($app['twig']);
  79. });
  80. $app['web_profiler.debug_toolbar.position'] = 'bottom';
  81. $app['profiler'] = $app->share(function ($app) {
  82. $profiler = new Profiler($app['profiler.storage'], $app['logger']);
  83. foreach ($app['data_collectors'] as $collector) {
  84. $profiler->add($collector($app));
  85. }
  86. return $profiler;
  87. });
  88. $app['profiler.storage'] = $app->share(function ($app) {
  89. return new FileProfilerStorage('file:'.$app['profiler.cache_dir']);
  90. });
  91. $app['profiler.request_matcher'] = null;
  92. $app['profiler.only_exceptions'] = false;
  93. $app['profiler.only_master_requests'] = false;
  94. $app['profiler.listener'] = $app->share(function ($app) {
  95. return new ProfilerListener(
  96. $app['profiler'],
  97. $app['profiler.request_matcher'],
  98. $app['profiler.only_exceptions'],
  99. $app['profiler.only_master_requests']
  100. );
  101. });
  102. $app['stopwatch'] = $app->share(function () {
  103. return new Stopwatch();
  104. });
  105. $app['code.file_link_format'] = null;
  106. $app['twig'] = $app->share($app->extend('twig', function ($twig, $app) {
  107. $twig->addExtension(new CodeExtension($app['code.file_link_format'], '', $app['charset']));
  108. return $twig;
  109. }));
  110. $app['twig.loader.filesystem'] = $app->share($app->extend('twig.loader.filesystem', function ($loader, $app) {
  111. $loader->addPath($app['profiler.templates_path'], 'WebProfiler');
  112. return $loader;
  113. }));
  114. $app['profiler.templates_path'] = function () {
  115. $r = new \ReflectionClass('Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener');
  116. return dirname(dirname($r->getFileName())).'/Resources/views';
  117. };
  118. }
  119. public function connect(Application $app)
  120. {
  121. if (!$app['resolver'] instanceof ServiceControllerResolver) {
  122. // using RuntimeException crashes PHP?!
  123. throw new \LogicException('You must enable the ServiceController service provider to be able to use the WebProfiler.');
  124. }
  125. $controllers = $app['controllers_factory'];
  126. $controllers->get('/router/{token}', 'web_profiler.controller.router:panelAction')->bind('_profiler_router');
  127. $controllers->get('/exception/{token}.css', 'web_profiler.controller.exception:cssAction')->bind('_profiler_exception_css');
  128. $controllers->get('/exception/{token}', 'web_profiler.controller.exception:showAction')->bind('_profiler_exception');
  129. $controllers->get('/search', 'web_profiler.controller.profiler:searchAction')->bind('_profiler_search');
  130. $controllers->get('/search_bar', 'web_profiler.controller.profiler:searchBarAction')->bind('_profiler_search_bar');
  131. $controllers->get('/purge', 'web_profiler.controller.profiler:purgeAction')->bind('_profiler_purge');
  132. $controllers->get('/info/{about}', 'web_profiler.controller.profiler:infoAction')->bind('_profiler_info');
  133. $controllers->get('/import', 'web_profiler.controller.profiler:importAction')->bind('_profiler_import');
  134. $controllers->get('/export/{token}.txt', 'web_profiler.controller.profiler:exportAction')->bind('_profiler_export');
  135. $controllers->get('/phpinfo', 'web_profiler.controller.profiler:phpinfoAction')->bind('_profiler_phpinfo');
  136. $controllers->get('/{token}/search/results', 'web_profiler.controller.profiler:searchResultsAction')->bind('_profiler_search_results');
  137. $controllers->get('/{token}', 'web_profiler.controller.profiler:panelAction')->bind('_profiler');
  138. $controllers->get('/wdt/{token}', 'web_profiler.controller.profiler:toolbarAction')->bind('_wdt');
  139. $controllers->get('/', 'web_profiler.controller.profiler:homeAction')->bind('_profiler_home');
  140. return $controllers;
  141. }
  142. public function boot(Application $app)
  143. {
  144. $dispatcher = $app['dispatcher'];
  145. $dispatcher->addSubscriber($app['profiler.listener']);
  146. $dispatcher->addSubscriber($app['web_profiler.toolbar.listener']);
  147. $dispatcher->addSubscriber($app['profiler']->get('request'));
  148. $app->mount($app['profiler.mount_prefix'], $this->connect($app));
  149. }
  150. }