20b67f0b53c11e826cc6523dc90e0000a3f40aa2
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / WebprofilerServiceProvider.php
1 <?php
2
3 namespace Drupal\webprofiler;
4
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;
14
15 /**
16  * Defines a service profiler for the webprofiler module.
17  */
18 class WebprofilerServiceProvider extends ServiceProviderBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function register(ContainerBuilder $container) {
24     // Add a compiler pass to discover all data collector services.
25     $container->addCompilerPass(new ProfilerPass());
26
27     // Add a compiler pass to discover all available storage backend.
28     $container->addCompilerPass(new StoragePass());
29
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);
33
34     $modules = $container->getParameter('container.modules');
35
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',
43           'id' => 'views',
44           'title' => 'Views',
45           'priority' => 75,
46         ]);
47     }
48
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',
55           'id' => 'blocks',
56           'title' => 'Blocks',
57           'priority' => 78,
58         ]);
59     }
60     
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',
70           'priority' => 210,
71         ]);
72     }
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function alter(ContainerBuilder $container) {
79     $modules = $container->getParameter('container.modules');
80
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');
85     }
86
87     // Replace the regular form_builder service with a traceable one.
88     $container->getDefinition('form_builder')
89       ->setClass('Drupal\webprofiler\Form\FormBuilderWrapper');
90
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')]);
95
96     // Replace the regular theme.negotiator service with a traceable one.
97     $container->getDefinition('theme.negotiator')
98       ->setClass('Drupal\webprofiler\Theme\ThemeNegotiatorWrapper');
99
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')]);
104
105     // Replace the regular string_translation service with a traceable one.
106     $container->getDefinition('string_translation')
107       ->setClass('Drupal\webprofiler\StringTranslation\TranslationManagerWrapper');
108   }
109 }