Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / DependencyInjection / TraceableContainer.php
1 <?php
2
3 namespace Drupal\webprofiler\DependencyInjection;
4
5 use Drupal\Component\Utility\Timer;
6 use Drupal\Core\DependencyInjection\Container;
7
8 /**
9  * Extends the Drupal container class to trace service instantiations.
10  */
11 class TraceableContainer extends Container {
12
13   /**
14    * @var array
15    */
16   protected $tracedData;
17
18   /**
19    * @var \Symfony\Component\Stopwatch\Stopwatch
20    */
21   private $stopwatch = NULL;
22
23   /**
24    * @var bool
25    */
26   private $hasStopwatch = FALSE;
27
28   /**
29    * @param string $id
30    * @param int $invalidBehavior
31    *
32    * @return object
33    */
34   public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
35     if (!$this->stopwatch && $this->has('stopwatch')) {
36       $this->stopwatch = parent::get('stopwatch');
37       $this->stopwatch->openSection();
38       $this->hasStopwatch = TRUE;
39     }
40
41     if ('stopwatch' === $id) {
42       return $this->stopwatch;
43     }
44
45     Timer::start($id);
46     if ($this->hasStopwatch) {
47       $e = $this->stopwatch->start($id, 'service');
48     }
49
50     $service = parent::get($id, $invalidBehavior);
51
52     $this->tracedData[$id] = Timer::stop($id);
53     if ($this->hasStopwatch && $e->isStarted()) {
54       $e->stop();
55     }
56
57     return $service;
58   }
59
60   /**
61    * @return array
62    */
63   public function getTracedData() {
64     return $this->tracedData;
65   }
66 }