Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Compiler / ProfilerPass.php
1 <?php
2
3 namespace Drupal\webprofiler\Compiler;
4
5 use Drupal\Core\StreamWrapper\PublicStream;
6 use Symfony\Component\DependencyInjection\Reference;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
10 /**
11  * Class ProfilerPass
12  */
13 class ProfilerPass implements CompilerPassInterface {
14
15   /**
16    * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
17    *
18    * @throws \InvalidArgumentException
19    */
20   public function process(ContainerBuilder $container) {
21     // configure the profiler service
22     if (FALSE === $container->hasDefinition('profiler')) {
23       return;
24     }
25
26     $definition = $container->getDefinition('profiler');
27
28     $collectors = new \SplPriorityQueue();
29     $order = PHP_INT_MAX;
30     foreach ($container->findTaggedServiceIds('data_collector') as $id => $attributes) {
31       $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
32       $template = NULL;
33
34       if (isset($attributes[0]['template'])) {
35         if (!isset($attributes[0]['id'])) {
36           throw new \InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
37         }
38         if (!isset($attributes[0]['title'])) {
39           throw new \InvalidArgumentException(sprintf('Data collector service "%s" must have a title attribute', $id));
40         }
41
42         $template = [
43           $attributes[0]['id'],
44           $attributes[0]['template'],
45           $attributes[0]['title']
46         ];
47       }
48
49       $collectors->insert([$id, $template], [-$priority, --$order]);
50     }
51
52     $templates = [];
53     foreach ($collectors as $collector) {
54       $definition->addMethodCall('add', [new Reference($collector[0])]);
55       $templates[$collector[0]] = $collector[1];
56     }
57
58     $container->setParameter('data_collector.templates', $templates);
59
60     // set parameter to store the public folder path
61     $path = 'file:' . DRUPAL_ROOT . '/' . PublicStream::basePath() . '/profiler';
62     $container->setParameter('data_collector.storage', $path);
63   }
64 }