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    /**
27     * @return void
28     */
29    public function enter(Profile $profile)
30    {
31        $this->actives[0]->addProfile($profile);
32        array_unshift($this->actives, $profile);
33    }
34
35    /**
36     * @return void
37     */
38    public function leave(Profile $profile)
39    {
40        $profile->leave();
41        array_shift($this->actives);
42
43        if (1 === \count($this->actives)) {
44            $this->actives[0]->leave();
45        }
46    }
47
48    public function getNodeVisitors(): array
49    {
50        return [new ProfilerNodeVisitor(static::class)];
51    }
52}
53