3 namespace Drupal\webprofiler\DependencyInjection;
5 use Drupal\Component\Utility\Timer;
6 use Drupal\Core\DependencyInjection\Container;
9 * Extends the Drupal container class to trace service instantiations.
11 class TraceableContainer extends Container {
16 protected $tracedData;
19 * @var \Symfony\Component\Stopwatch\Stopwatch
21 private $stopwatch = NULL;
26 private $hasStopwatch = FALSE;
30 * @param int $invalidBehavior
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;
41 if ('stopwatch' === $id) {
42 return $this->stopwatch;
46 if ($this->hasStopwatch) {
47 $e = $this->stopwatch->start($id, 'service');
50 $service = parent::get($id, $invalidBehavior);
52 $this->tracedData[$id] = Timer::stop($id);
53 if ($this->hasStopwatch && $e->isStarted()) {
63 public function getTracedData() {
64 return $this->tracedData;