Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Profiler / Profiler.php
1 <?php
2
3 namespace Drupal\webprofiler\Profiler;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Psr\Log\LoggerInterface;
7 use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
8 use Symfony\Component\HttpKernel\Profiler\Profile;
9 use Symfony\Component\HttpKernel\Profiler\Profiler as SymfonyProfiler;
10 use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
11
12 /**
13  * Class Profiler
14  */
15 class Profiler extends SymfonyProfiler {
16
17   /**
18    * @var \Drupal\Core\Config\ConfigFactoryInterface $config
19    */
20   private $config;
21
22   /**
23    * @var array
24    */
25   private $activeToolbarItems;
26
27   private $localStorage;
28   private $localLogger;
29
30   /**
31    * Constructor.
32    *
33    * @param \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface $storage
34    *   A ProfilerStorageInterface instance
35    * @param \Psr\Log\LoggerInterface $logger
36    *   A LoggerInterface instance
37    * @param \Drupal\Core\Config\ConfigFactoryInterface $config
38    */
39   public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = NULL, ConfigFactoryInterface $config) {
40     parent::__construct($storage, $logger);
41
42     $this->localStorage = $storage;
43     $this->localLogger = $logger;
44
45     $this->config = $config;
46     $this->activeToolbarItems = $this->config->get('webprofiler.config')
47       ->get('active_toolbar_items');
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function add(DataCollectorInterface $collector) {
54     // drupal collector should not be disabled
55     if ($collector->getName() == 'drupal') {
56       parent::add($collector);
57     }
58     else {
59       if ($this->activeToolbarItems && array_key_exists($collector->getName(), $this->activeToolbarItems) && $this->activeToolbarItems[$collector->getName()] !== '0') {
60         parent::add($collector);
61       }
62     }
63   }
64
65   /**
66    * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
67    *
68    * @return bool
69    */
70   public function updateProfile(Profile $profile) {
71     if (!($ret = $this->localStorage->write($profile)) && NULL !== $this->localLogger) {
72       $this->localLogger->warning('Unable to store the profiler information.');
73     }
74
75     return $ret;
76   }
77 }