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/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
27
|
|
|
* @package TwigProfiler |
|
|
|
|
28
|
|
|
* @since 1.0.0 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class TwigProfiler extends Plugin |
31
|
|
|
{ |
32
|
|
|
// Traits |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
use ServicesTrait; |
36
|
|
|
|
37
|
|
|
// Public Static Properties |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @var ?TwigProfiler |
42
|
|
|
*/ |
43
|
|
|
public static ?TwigProfiler $plugin = null; |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var string The name of the rendering template |
47
|
|
|
*/ |
48
|
|
|
public static string $renderingTemplate = ''; |
49
|
|
|
|
50
|
|
|
// Public Properties |
51
|
|
|
// ========================================================================= |
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
public string $schemaVersion = '1.0.0'; |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
public bool $hasCpSection = false; |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
public bool $hasCpSettings = false; |
67
|
|
|
|
68
|
|
|
// Public Methods |
69
|
|
|
// ========================================================================= |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
public function init(): void |
75
|
|
|
{ |
76
|
|
|
parent::init(); |
77
|
|
|
self::$plugin = $this; |
78
|
|
|
|
79
|
|
|
/** @var Settings $settings */ |
|
|
|
|
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 { |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
112
|
|
|
* @inheritdoc |
113
|
|
|
*/ |
|
|
|
|
114
|
|
|
protected function createSettingsModel(): Settings |
115
|
|
|
{ |
116
|
|
|
return new Settings(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|