1 | <?php |
||
2 | /** |
||
3 | * Template Comments plugin for Craft CMS |
||
4 | * |
||
5 | * Adds a HTML comment to demarcate each Twig template that is included or extended. |
||
6 | * |
||
7 | * @link https://nystudio107.com/ |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\templatecomments; |
||
12 | |||
13 | use Craft; |
||
14 | use craft\base\Model; |
||
15 | use craft\base\Plugin; |
||
16 | use craft\events\TemplateEvent; |
||
17 | use craft\web\View; |
||
18 | use nystudio107\templatecomments\models\Settings; |
||
19 | use nystudio107\templatecomments\web\twig\CommentsTwigExtension; |
||
20 | use nystudio107\templatecomments\web\twig\CommentTemplateLoader; |
||
21 | use nystudio107\templatecomments\web\twig\TemplateCommentsParser; |
||
22 | use Twig\Loader\LoaderInterface; |
||
23 | use yii\base\Event; |
||
24 | |||
25 | /** |
||
26 | * Class TemplateComments |
||
27 | * |
||
28 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
29 | * @package TemplateComments |
||
0 ignored issues
–
show
|
|||
30 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
31 | * |
||
32 | */ |
||
0 ignored issues
–
show
|
|||
33 | class TemplateComments extends Plugin |
||
34 | { |
||
35 | // Static Properties |
||
36 | // ========================================================================= |
||
37 | |||
38 | /** |
||
0 ignored issues
–
show
|
|||
39 | * @var ?TemplateComments |
||
40 | */ |
||
41 | public static ?TemplateComments $plugin = null; |
||
42 | |||
43 | /** |
||
0 ignored issues
–
show
|
|||
44 | * @var ?Settings $settings |
||
45 | */ |
||
46 | public static ?Settings $settings = null; |
||
47 | |||
48 | /** |
||
0 ignored issues
–
show
|
|||
49 | * @var ?LoaderInterface |
||
50 | */ |
||
51 | public static ?LoaderInterface $originalTwigLoader = null; |
||
52 | |||
53 | // Public Properties |
||
54 | // ========================================================================= |
||
55 | |||
56 | /** |
||
0 ignored issues
–
show
|
|||
57 | * @var string |
||
58 | */ |
||
59 | public string $schemaVersion = '1.0.0'; |
||
60 | |||
61 | // Public Methods |
||
62 | // ========================================================================= |
||
63 | |||
64 | /** |
||
0 ignored issues
–
show
|
|||
65 | * @inheritdoc |
||
66 | */ |
||
0 ignored issues
–
show
|
|||
67 | public function init(): void |
||
68 | { |
||
69 | parent::init(); |
||
70 | // Initialize properties |
||
71 | self::$plugin = $this; |
||
72 | /** @var ?Settings $settings */ |
||
0 ignored issues
–
show
|
|||
73 | $settings = $this->getSettings(); |
||
74 | self::$settings = $settings; |
||
75 | // Defer some setup tasks until Craft is fully initialized: |
||
76 | Craft::$app->onInit(function() { |
||
0 ignored issues
–
show
|
|||
77 | // Add in our Craft components |
||
78 | $this->addComponents(); |
||
79 | // Install our global event handlers |
||
80 | $this->installEventListeners(); |
||
81 | }); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
82 | |||
83 | Craft::info( |
||
84 | Craft::t( |
||
85 | 'templatecomments', |
||
86 | '{name} plugin loaded', |
||
87 | ['name' => $this->name] |
||
88 | ), |
||
89 | __METHOD__ |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | // Protected Methods |
||
94 | // ========================================================================= |
||
95 | |||
96 | /** |
||
97 | * Add in our Craft components |
||
98 | */ |
||
0 ignored issues
–
show
|
|||
99 | protected function addComponents(): void |
||
100 | { |
||
101 | $request = Craft::$app->getRequest(); |
||
102 | if (!$request->getIsConsoleRequest()) { |
||
103 | // Do nothing at all on AJAX requests |
||
104 | if ($request->getIsAjax()) { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | // Install only for site requests |
||
109 | if ($request->getIsSiteRequest()) { |
||
110 | $this->installSiteComponents(); |
||
111 | } |
||
112 | |||
113 | // Install only for Control Panel requests |
||
114 | if ($request->getIsCpRequest()) { |
||
115 | $this->installCpComponents(); |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Install components for site requests only |
||
122 | */ |
||
0 ignored issues
–
show
|
|||
123 | protected function installSiteComponents(): void |
||
124 | { |
||
125 | if (self::$settings->siteTemplateComments) { |
||
126 | $this->installTemplateComponents(); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Install components for Control Panel requests only |
||
132 | */ |
||
0 ignored issues
–
show
|
|||
133 | protected function installCpComponents(): void |
||
134 | { |
||
135 | if (self::$settings->cpTemplateComments) { |
||
136 | $this->installTemplateComponents(); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
0 ignored issues
–
show
|
|||
141 | * @inheritdoc |
||
142 | */ |
||
0 ignored issues
–
show
|
|||
143 | protected function createSettingsModel(): ?Model |
||
144 | { |
||
145 | return new Settings(); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Install our event listeners |
||
150 | */ |
||
0 ignored issues
–
show
|
|||
151 | protected function installEventListeners() |
||
152 | { |
||
153 | $request = Craft::$app->getRequest(); |
||
154 | // Do nothing at all on AJAX requests |
||
155 | if (!$request->getIsConsoleRequest() && $request->getIsAjax()) { |
||
156 | return; |
||
157 | } |
||
158 | // Install only for non-console site requests |
||
159 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
160 | $this->installSiteEventListeners(); |
||
161 | } |
||
162 | // Install only for non-console Control Panel requests |
||
163 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
164 | $this->installCpEventListeners(); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Install site event listeners for site requests only |
||
170 | */ |
||
0 ignored issues
–
show
|
|||
171 | protected function installSiteEventListeners() |
||
172 | { |
||
173 | if (self::$settings->siteTemplateComments) { |
||
174 | $this->installTemplateEventListeners(); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Install site event listeners for Control Panel requests only |
||
180 | */ |
||
0 ignored issues
–
show
|
|||
181 | protected function installCpEventListeners() |
||
182 | { |
||
183 | if (self::$settings->cpTemplateComments) { |
||
184 | $this->installTemplateEventListeners(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // Private Methods |
||
189 | // ========================================================================= |
||
190 | |||
191 | /** |
||
192 | * Install our template components |
||
193 | */ |
||
0 ignored issues
–
show
|
|||
194 | private function installTemplateComponents(): void |
||
0 ignored issues
–
show
|
|||
195 | { |
||
196 | $devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
||
197 | if (!self::$settings->onlyCommentsInDevMode || $devMode) { |
||
198 | $view = Craft::$app->getView(); |
||
199 | self::$originalTwigLoader = $view->getTwig()->getLoader(); |
||
200 | $view->registerTwigExtension(new CommentsTwigExtension()); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Install our template event listeners |
||
206 | */ |
||
0 ignored issues
–
show
|
|||
207 | private function installTemplateEventListeners() |
||
0 ignored issues
–
show
|
|||
208 | { |
||
209 | $devMode = Craft::$app->getConfig()->getGeneral()->devMode; |
||
210 | if (!self::$settings->onlyCommentsInDevMode || $devMode) { |
||
211 | // Remember the name of the currently rendering template |
||
212 | Event::on( |
||
213 | View::class, |
||
214 | View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
||
215 | function(TemplateEvent $event) { |
||
0 ignored issues
–
show
|
|||
216 | $view = Craft::$app->getView(); |
||
217 | $twig = $view->getTwig(); |
||
218 | if ($this->enabledForTemplate($event->template)) { |
||
219 | $twig->setLoader(new CommentTemplateLoader($view)); |
||
220 | $twig->setParser(new TemplateCommentsParser($twig)); |
||
221 | } |
||
222 | } |
||
223 | ); |
||
224 | } |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Is template parsing enabled for this template? |
||
229 | * |
||
230 | * @param string $templateName |
||
0 ignored issues
–
show
|
|||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | private function enabledForTemplate(string $templateName): bool |
||
0 ignored issues
–
show
|
|||
235 | { |
||
236 | $ext = pathinfo($templateName, PATHINFO_EXTENSION); |
||
237 | return (self::$settings->templateCommentsEnabled |
||
238 | && in_array($ext, self::$settings->allowedTemplateSuffixes, false)); |
||
239 | } |
||
240 | } |
||
241 |