3 namespace Drupal\webprofiler;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\DependencyInjection\ServiceProviderBase;
7 use Drupal\webprofiler\Compiler\DecoratorPass;
8 use Drupal\webprofiler\Compiler\EventPass;
9 use Drupal\webprofiler\Compiler\ProfilerPass;
10 use Drupal\webprofiler\Compiler\ServicePass;
11 use Drupal\webprofiler\Compiler\StoragePass;
12 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
13 use Symfony\Component\DependencyInjection\Reference;
16 * Defines a service profiler for the webprofiler module.
18 class WebprofilerServiceProvider extends ServiceProviderBase {
23 public function register(ContainerBuilder $container) {
24 // Add a compiler pass to discover all data collector services.
25 $container->addCompilerPass(new ProfilerPass());
27 // Add a compiler pass to discover all available storage backend.
28 $container->addCompilerPass(new StoragePass());
30 $container->addCompilerPass(new ServicePass(), PassConfig::TYPE_AFTER_REMOVING);
31 $container->addCompilerPass(new EventPass(), PassConfig::TYPE_AFTER_REMOVING);
32 $container->addCompilerPass(new DecoratorPass(), PassConfig::TYPE_AFTER_REMOVING);
34 $modules = $container->getParameter('container.modules');
36 // Add ViewsDataCollector only if Views module is enabled.
37 if (isset($modules['views'])) {
38 $container->register('webprofiler.views', 'Drupal\webprofiler\DataCollector\ViewsDataCollector')
39 ->addArgument(new Reference(('views.executable')))
40 ->addArgument(new Reference(('entity.manager')))
41 ->addTag('data_collector', [
42 'template' => '@webprofiler/Collector/views.html.twig',
49 // Add BlockDataCollector only if Block module is enabled.
50 if (isset($modules['block'])) {
51 $container->register('webprofiler.blocks', 'Drupal\webprofiler\DataCollector\BlocksDataCollector')
52 ->addArgument(new Reference(('entity_type.manager')))
53 ->addTag('data_collector', [
54 'template' => '@webprofiler/Collector/blocks.html.twig',
61 // Add TranslationsDataCollector only if Locale module is enabled.
62 if (isset($modules['locale'])) {
63 $container->register('webprofiler.translations', 'Drupal\webprofiler\DataCollector\TranslationsDataCollector')
64 ->addArgument(new Reference(('string_translation')))
65 ->addArgument(new Reference(('url_generator')))
66 ->addTag('data_collector', [
67 'template' => '@webprofiler/Collector/translations.html.twig',
68 'id' => 'translations',
69 'title' => 'Translations',
78 public function alter(ContainerBuilder $container) {
79 $modules = $container->getParameter('container.modules');
81 // Alter the views.executable service only if Views module is enabled.
82 if (isset($modules['views'])) {
83 $container->getDefinition('views.executable')
84 ->setClass('Drupal\webprofiler\Views\ViewExecutableFactoryWrapper');
87 // Replace the regular form_builder service with a traceable one.
88 $container->getDefinition('form_builder')
89 ->setClass('Drupal\webprofiler\Form\FormBuilderWrapper');
91 // Replace the regular access_manager service with a traceable one.
92 $container->getDefinition('access_manager')
93 ->setClass('Drupal\webprofiler\Access\AccessManagerWrapper')
94 ->addMethodCall('setDataCollector', [new Reference('webprofiler.request')]);
96 // Replace the regular theme.negotiator service with a traceable one.
97 $container->getDefinition('theme.negotiator')
98 ->setClass('Drupal\webprofiler\Theme\ThemeNegotiatorWrapper');
100 // Replace the regular config.factory service with a traceable one.
101 $container->getDefinition('config.factory')
102 ->setClass('Drupal\webprofiler\Config\ConfigFactoryWrapper')
103 ->addMethodCall('setDataCollector', [new Reference('webprofiler.config')]);
105 // Replace the regular string_translation service with a traceable one.
106 $container->getDefinition('string_translation')
107 ->setClass('Drupal\webprofiler\StringTranslation\TranslationManagerWrapper');