1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Twig\Extension;
13
14use Twig\Profiler\NodeVisitor\ProfilerNodeVisitor;
15use Twig\Profiler\Profile;
16
17class ProfilerExtension extends AbstractExtension
18{
19    private $actives = [];
20
21    public function __construct(Profile $profile)
22    {
23        $this->actives[] = $profile;
24    }
25
26    public function enter(Profile $profile)
27    {
28        $this->actives[0]->addProfile($profile);
29        array_unshift($this->actives, $profile);
30    }
31
32    public function leave(Profile $profile)
33    {
34        $profile->leave();
35        array_shift($this->actives);
36
37        if (1 === \count($this->actives)) {
38            $this->actives[0]->leave();
39        }
40    }
41
42    public function getNodeVisitors()
43    {
44        return [new ProfilerNodeVisitor(static::class)];
45    }
46}
47
48class_alias('Twig\Extension\ProfilerExtension', 'Twig_Extension_Profiler');
49