1 | <?php |
||
2 | /** |
||
3 | * Twig Profiler plugin for Craft CMS |
||
4 | * |
||
5 | * Twig Profiler allows you to profile sections of your Twig templates, and see |
||
6 | * the resulting timings in the Yii2 Debug Toolbar |
||
7 | * |
||
8 | * @link https://nystudio107.com/ |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
9 | * @copyright Copyright (c) nystudio107 |
||
0 ignored issues
–
show
|
|||
10 | */ |
||
0 ignored issues
–
show
|
|||
11 | |||
12 | namespace nystudio107\twigprofiler; |
||
13 | |||
14 | use Craft; |
||
15 | use craft\base\Plugin; |
||
16 | use craft\events\TemplateEvent; |
||
17 | use craft\web\View; |
||
18 | use nystudio107\twigprofiler\models\Settings; |
||
19 | use nystudio107\twigprofiler\services\ServicesTrait; |
||
20 | use nystudio107\twigprofiler\twigextensions\ProfilerTwigExtension; |
||
21 | use yii\base\Event; |
||
22 | |||
23 | /** |
||
24 | * Class TwigProfiler |
||
25 | * |
||
26 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
27 | * @package TwigProfiler |
||
0 ignored issues
–
show
|
|||
28 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
29 | */ |
||
0 ignored issues
–
show
|
|||
30 | class TwigProfiler extends Plugin |
||
31 | { |
||
32 | // Traits |
||
33 | // ========================================================================= |
||
34 | |||
35 | use ServicesTrait; |
||
36 | |||
37 | // Public Static Properties |
||
38 | // ========================================================================= |
||
39 | |||
40 | /** |
||
0 ignored issues
–
show
|
|||
41 | * @var ?TwigProfiler |
||
42 | */ |
||
43 | public static ?TwigProfiler $plugin = null; |
||
44 | |||
45 | /** |
||
0 ignored issues
–
show
|
|||
46 | * @var string The name of the rendering template |
||
47 | */ |
||
48 | public static string $renderingTemplate = ''; |
||
49 | |||
50 | // Public Properties |
||
51 | // ========================================================================= |
||
52 | |||
53 | /** |
||
0 ignored issues
–
show
|
|||
54 | * @var string |
||
55 | */ |
||
56 | public string $schemaVersion = '1.0.0'; |
||
57 | |||
58 | /** |
||
0 ignored issues
–
show
|
|||
59 | * @var bool |
||
60 | */ |
||
61 | public bool $hasCpSection = false; |
||
62 | |||
63 | /** |
||
0 ignored issues
–
show
|
|||
64 | * @var bool |
||
65 | */ |
||
66 | public bool $hasCpSettings = false; |
||
67 | |||
68 | // Public Methods |
||
69 | // ========================================================================= |
||
70 | |||
71 | /** |
||
0 ignored issues
–
show
|
|||
72 | * @inheritdoc |
||
73 | */ |
||
0 ignored issues
–
show
|
|||
74 | public function init(): void |
||
75 | { |
||
76 | parent::init(); |
||
77 | self::$plugin = $this; |
||
78 | |||
79 | /** @var Settings $settings */ |
||
0 ignored issues
–
show
|
|||
80 | $settings = $this->getSettings(); |
||
81 | Craft::$app->view->registerTwigExtension(new ProfilerTwigExtension()); |
||
82 | |||
83 | if ($settings !== null && $settings->appendTemplateName) { |
||
84 | // Handler: View::EVENT_BEFORE_RENDER_TEMPLATE |
||
85 | Event::on( |
||
86 | View::class, |
||
87 | View::EVENT_BEFORE_RENDER_TEMPLATE, |
||
88 | static function(TemplateEvent $event): void { |
||
0 ignored issues
–
show
|
|||
89 | Craft::debug( |
||
90 | 'View::EVENT_BEFORE_RENDER_TEMPLATE', |
||
91 | __METHOD__ |
||
92 | ); |
||
93 | self::$renderingTemplate = ' - ' . $event->template; |
||
94 | } |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | Craft::info( |
||
99 | Craft::t( |
||
100 | 'twig-profiler', |
||
101 | '{name} plugin loaded', |
||
102 | ['name' => $this->name] |
||
103 | ), |
||
104 | __METHOD__ |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | // Protected Methods |
||
109 | // ========================================================================= |
||
110 | |||
111 | /** |
||
0 ignored issues
–
show
|
|||
112 | * @inheritdoc |
||
113 | */ |
||
0 ignored issues
–
show
|
|||
114 | protected function createSettingsModel(): Settings |
||
115 | { |
||
116 | return new Settings(); |
||
117 | } |
||
118 | } |
||
119 |