Passed
Push — v1 ( 65d641...7780c4 )
by Andrew
02:59
created

Webperf::addComponents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\webperf;
12
13
use nystudio107\webperf\log\ProfileTarget;
14
use nystudio107\webperf\models\Settings;
15
use nystudio107\webperf\services\DataSamples as DataSamplesService;
16
use nystudio107\webperf\services\Beacons as BeaconsService;
17
use nystudio107\webperf\variables\WebperfVariable;
18
use nystudio107\webperf\widgets\Metrics as MetricsWidget;
19
20
use Craft;
0 ignored issues
show
Bug introduced by
The type Craft was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use craft\base\Plugin;
0 ignored issues
show
Bug introduced by
The type craft\base\Plugin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use craft\services\Plugins;
0 ignored issues
show
Bug introduced by
The type craft\services\Plugins was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use craft\events\PluginEvent;
0 ignored issues
show
Bug introduced by
The type craft\events\PluginEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use craft\events\RegisterComponentTypesEvent;
0 ignored issues
show
Bug introduced by
The type craft\events\RegisterComponentTypesEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use craft\events\RegisterUserPermissionsEvent;
0 ignored issues
show
Bug introduced by
The type craft\events\RegisterUserPermissionsEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use craft\events\RegisterUrlRulesEvent;
0 ignored issues
show
Bug introduced by
The type craft\events\RegisterUrlRulesEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use craft\helpers\UrlHelper;
0 ignored issues
show
Bug introduced by
The type craft\helpers\UrlHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use craft\services\Dashboard;
0 ignored issues
show
Bug introduced by
The type craft\services\Dashboard was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use craft\services\UserPermissions;
0 ignored issues
show
Bug introduced by
The type craft\services\UserPermissions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use craft\web\twig\variables\CraftVariable;
0 ignored issues
show
Bug introduced by
The type craft\web\twig\variables\CraftVariable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use craft\web\UrlManager;
0 ignored issues
show
Bug introduced by
The type craft\web\UrlManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use craft\web\View;
0 ignored issues
show
Bug introduced by
The type craft\web\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
34
use yii\base\Application;
0 ignored issues
show
Bug introduced by
The type yii\base\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use yii\base\Event;
0 ignored issues
show
Bug introduced by
The type yii\base\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
use yii\base\InvalidConfigException;
0 ignored issues
show
Bug introduced by
The type yii\base\InvalidConfigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
38
/**
39
 * Class Webperf
40
 *
41
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
42
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
43
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
44
 *
45
 * @property  DataSamplesService  $dataSamples
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
46
 * @property  BeaconsService      $beacons
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
47
 * @property  ProfileTarget       $profileTarget
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 2
Loading history...
48
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
49
class Webperf extends Plugin
50
{
51
    // Static Properties
52
    // =========================================================================
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @var Webperf
56
     */
57
    public static $plugin;
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @var Settings
61
     */
62
    public static $settings;
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @var int|null
66
     */
67
    public static $requestUuid;
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @var bool
71
     */
72
    public static $beaconIncluded = false;
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
75
     * @var string
76
     */
77
    public static $renderType = 'html';
78
79
    // Public Properties
80
    // =========================================================================
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @var string
84
     */
85
    public $schemaVersion = '1.0.0';
86
87
    // Public Methods
88
    // =========================================================================
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @inheritdoc
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function init()
94
    {
95
        parent::init();
96
        // Initialize properties
97
        self::$plugin = $this;
98
        self::$settings = $this->getSettings();
99
        try {
100
            self::$requestUuid = random_int(0, PHP_INT_MAX);
101
        } catch (\Exception $e) {
102
            self::$requestUuid = null;
103
        }
104
        $this->name = self::$settings->pluginName;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
105
        // Add in our components
106
        $this->addComponents();
107
        // Install event listeners
108
        $this->installEventListeners();
109
        // Load that we've loaded
110
        Craft::info(
111
            Craft::t(
112
                'webperf',
113
                '{name} plugin loaded',
114
                ['name' => $this->name]
115
            ),
116
            __METHOD__
117
        );
118
    }
119
120
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
121
     * @inheritdoc
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    public function getSettingsResponse()
124
    {
125
        // Just redirect to the plugin settings page
126
        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('webperf/settings'));
127
    }
128
129
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
130
     * @inheritdoc
131
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
132
    public function getCpNavItem()
133
    {
134
        $subNavs = [];
135
        $navItem = parent::getCpNavItem();
136
        $currentUser = Craft::$app->getUser()->getIdentity();
137
        // Only show sub-navs the user has permission to view
138
        if ($currentUser->can('webperf:dashboard')) {
139
            $subNavs['dashboard'] = [
140
                'label' => 'Dashboard',
141
                'url' => 'webperf/dashboard',
142
            ];
143
        }
144
        if ($currentUser->can('webperf:pages')) {
145
            $subNavs['pages'] = [
146
                'label' => 'Pages',
147
                'url' => 'webperf/pages',
148
            ];
149
        }
150
        if ($currentUser->can('webperf:settings')) {
151
            $subNavs['settings'] = [
152
                'label' => 'Settings',
153
                'url' => 'webperf/settings',
154
            ];
155
        }
156
        $navItem = array_merge($navItem, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
157
            'subnav' => $subNavs,
158
        ]);
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
159
160
        return $navItem;
161
    }
162
163
    // Protected Methods
164
    // =========================================================================
165
166
    /**
167
     * Add in our components
168
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
169
    protected function addComponents()
170
    {
171
        // Add in the ProfileTarget component
172
        try {
173
            $this->set('profileTarget', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
174
                'class' => ProfileTarget::class,
175
                'levels' => ['profile'],
176
                'categories' => [],
177
                'logVars' => [],
178
                'except' => [],
179
            ]);
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
180
        } catch (InvalidConfigException $e) {
181
            Craft::error($e->getMessage(), __METHOD__);
182
        }
183
        // Attach our log target
184
        Craft::$app->getLog()->targets['webperf'] = $this->profileTarget;
185
    }
186
187
    /**
188
     * Install our event listeners.
189
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
190
    protected function installEventListeners()
191
    {
192
        $request = Craft::$app->getRequest();
193
        // Add in our event listeners that are needed for every request
194
        $this->installGlobalEventListeners();
195
        // Install only for non-console site requests
196
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
197
            $this->installSiteEventListeners();
198
        }
199
        // Install only for non-console Control Panel requests
200
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
201
            $this->installCpEventListeners();
202
        }
203
        // Handler: EVENT_AFTER_INSTALL_PLUGIN
204
        Event::on(
205
            Plugins::class,
206
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
207
            function (PluginEvent $event) {
208
                if ($event->plugin === $this) {
209
                    // Invalidate our caches after we've been installed
210
                    $this->clearAllCaches();
211
                    // Send them to our welcome screen
212
                    $request = Craft::$app->getRequest();
213
                    if ($request->isCpRequest) {
214
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
215
                            'webperf/dashboard',
216
                            [
217
                                'showWelcome' => true,
218
                            ]
219
                        ))->send();
0 ignored issues
show
Coding Style introduced by
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.
Loading history...
220
                    }
221
                }
222
            }
223
        );
224
    }
225
226
    /**
227
     * Install global event listeners for all request types
228
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
229
    protected function installGlobalEventListeners()
230
    {
231
        // Handler: CraftVariable::EVENT_INIT
232
        Event::on(
233
            CraftVariable::class,
234
            CraftVariable::EVENT_INIT,
235
            function (Event $event) {
236
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
237
                $variable = $event->sender;
238
                $variable->set('webperf', WebperfVariable::class);
239
            }
240
        );
241
        // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
242
        Event::on(
243
            Plugins::class,
244
            Plugins::EVENT_AFTER_LOAD_PLUGINS,
245
            function () {
246
                // Install these only after all other plugins have loaded
247
                $request = Craft::$app->getRequest();
248
                // Only respond to non-console site requests
249
                if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
250
                    $this->handleSiteRequest();
251
                }
252
                // Respond to Control Panel requests
253
                if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
254
                    $this->handleAdminCpRequest();
255
                }
256
            }
257
        );
258
    }
259
260
    /**
261
     * Install site event listeners for site requests only
262
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
263
    protected function installSiteEventListeners()
264
    {
265
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
266
        Event::on(
267
            UrlManager::class,
268
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
269
            function (RegisterUrlRulesEvent $event) {
270
                Craft::debug(
271
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
272
                    __METHOD__
273
                );
274
                // Register our Control Panel routes
275
                $event->rules = array_merge(
276
                    $event->rules,
277
                    $this->customFrontendRoutes()
278
                );
279
            }
280
        );
281
    }
282
283
    /**
284
     * Install site event listeners for Control Panel requests only
285
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
286
    protected function installCpEventListeners()
287
    {
288
        // Handler: UrlManager::EVENT_REGISTER_CP_URL_RULES
289
        Event::on(
290
            UrlManager::class,
291
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
292
            function (RegisterUrlRulesEvent $event) {
293
                Craft::debug(
294
                    'UrlManager::EVENT_REGISTER_CP_URL_RULES',
295
                    __METHOD__
296
                );
297
                // Register our Control Panel routes
298
                $event->rules = array_merge(
299
                    $event->rules,
300
                    $this->customAdminCpRoutes()
301
                );
302
            }
303
        );
304
        // Handler: Dashboard::EVENT_REGISTER_WIDGET_TYPES
305
        Event::on(
306
            Dashboard::class,
307
            Dashboard::EVENT_REGISTER_WIDGET_TYPES,
308
            function (RegisterComponentTypesEvent $event) {
309
                $event->types[] = MetricsWidget::class;
310
            }
311
        );
312
        // Handler: UserPermissions::EVENT_REGISTER_PERMISSIONS
313
        Event::on(
314
            UserPermissions::class,
315
            UserPermissions::EVENT_REGISTER_PERMISSIONS,
316
            function (RegisterUserPermissionsEvent $event) {
317
                Craft::debug(
318
                    'UserPermissions::EVENT_REGISTER_PERMISSIONS',
319
                    __METHOD__
320
                );
321
                // Register our custom permissions
322
                $event->permissions[Craft::t('webperf', 'Webperf')] = $this->customAdminCpPermissions();
323
            }
324
        );
325
    }
326
327
    /**
328
     * Handle site requests.  We do it only after we receive the event
329
     * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run
330
     * before our event listeners kick in
331
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
332
    protected function handleSiteRequest()
333
    {
334
        // Don't include the beacon for response codes >= 400
335
        $response = Craft::$app->getResponse();
336
        if ($response->statusCode < 400) {
337
            // Handler: View::EVENT_END_PAGE
338
            Event::on(
339
                View::class,
340
                View::EVENT_END_PAGE,
341
                function () {
342
                    Craft::debug(
343
                        'View::EVENT_END_PAGE',
344
                        __METHOD__
345
                    );
346
                    $view = Craft::$app->getView();
347
                    // The page is done rendering, include our beacon
348
                    if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) {
349
                        switch (self::$renderType) {
350
                            case 'html':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
351
                                Webperf::$plugin->beacons->includeHtmlBeacon();
352
                                self::$beaconIncluded = true;
353
                                break;
354
                            case 'amp-html':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
355
                                Webperf::$plugin->beacons->includeAmpHtmlScript();
356
                                break;
357
                        }
358
                    }
359
                }
360
            );
361
            // Handler: View::EVENT_END_BODY
362
            Event::on(
363
                View::class,
364
                View::EVENT_END_BODY,
365
                function () {
366
                    Craft::debug(
367
                        'View::EVENT_END_BODY',
368
                        __METHOD__
369
                    );
370
                    $view = Craft::$app->getView();
371
                    // The page is done rendering, include our beacon
372
                    if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) {
373
                        switch (self::$renderType) {
374
                            case 'html':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
375
                                break;
376
                            case 'amp-html':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 24 spaces, found 28
Loading history...
377
                                Webperf::$plugin->beacons->includeAmpHtmlBeacon();
378
                                self::$beaconIncluded = true;
379
                                break;
380
                        }
381
                    }
382
                }
383
            );
384
            // Handler: Application::EVENT_AFTER_REQUEST
385
            Event::on(
386
                Application::class,
387
                Application::EVENT_AFTER_REQUEST,
388
                function () {
389
                    Craft::debug(
390
                        'Application::EVENT_AFTER_REQUEST',
391
                        __METHOD__
392
                    );
393
                }
394
            );
395
        }
396
    }
397
398
    /**
399
     * Handle Control Panel requests. We do it only after we receive the event
400
     * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run
401
     * before our event listeners kick in
402
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
403
    protected function handleAdminCpRequest()
404
    {
405
    }
406
407
    /**
408
     * Clear all the caches!
409
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
410
    public function clearAllCaches()
411
    {
412
    }
413
414
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
415
     * @inheritdoc
416
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
417
    protected function createSettingsModel()
418
    {
419
        return new Settings();
420
    }
421
422
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
423
     * @inheritdoc
424
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
425
    protected function settingsHtml(): string
426
    {
427
        return Craft::$app->view->renderTemplate(
428
            'webperf/settings',
429
            [
430
                'settings' => $this->getSettings()
431
            ]
432
        );
433
    }
434
435
    /**
436
     * Return the custom frontend routes
437
     *
438
     * @return array
439
     */
440
    protected function customFrontendRoutes(): array
441
    {
442
        return [
443
            // Beacon
444
            '/webperf/metrics/beacon' => 'webperf/metrics/beacon',
445
            // Render
446
            '/webperf/render/amp-iframe' => 'webperf/render/amp-iframe',
447
            // Tables
448
            '/webperf/tables/dashboard' => 'webperf/tables/dashboard',
449
            '/webperf/tables/redirects' => 'webperf/tables/redirects',
450
            // Charts
451
            '/webperf/charts/dashboard-stats-average/<days:\d+>/<column:{handle}>'
452
            => 'webperf/charts/dashboard-stats-average',
453
            '/webperf/charts/dashboard-stats-average/<days:\d+>/<column:{handle}>/<siteId:\d+>'
454
            => 'webperf/charts/dashboard-stats-average',
455
            '/webperf/charts/dashboard-slowest-pages/<days:\d+>/<column:{handle}>/<limit:\d+>'
456
            => 'webperf/charts/dashboard-slowest-pages',
457
            '/webperf/charts/dashboard-slowest-pages/<days:\d+>/<column:{handle}>/<limit:\d+>/<siteId:\d+>'
458
            => 'webperf/charts/dashboard-slowest-pages',
459
            '/webperf/charts/widget/<days>' => 'webperf/charts/widget',
460
        ];
461
    }
462
    /**
463
     * Return the custom Control Panel routes
464
     *
465
     * @return array
466
     */
467
    protected function customAdminCpRoutes(): array
468
    {
469
        return [
470
            'webperf' => 'webperf/sections/dashboard',
471
            'webperf/dashboard' => 'webperf/sections/dashboard',
472
            'webperf/dashboard/<siteHandle:{handle}>' => 'webperf/sections/dashboard',
473
474
            'webperf/pages' => 'webperf/sections/pages-index',
475
            'webperf/pages/<siteHandle:{handle}>' => 'webperf/sections/pages-index',
476
477
            'webperf/settings' => 'webperf/settings/plugin-settings',
478
        ];
479
    }
480
481
    /**
482
     * Returns the custom Control Panel user permissions.
483
     *
484
     * @return array
485
     */
486
    protected function customAdminCpPermissions(): array
487
    {
488
        return [
489
            'webperf:dashboard' => [
490
                'label' => Craft::t('webperf', 'Dashboard'),
491
            ],
492
            'webperf:pages' => [
493
                'label' => Craft::t('webperf', 'Pages'),
494
            ],
495
            'webperf:settings' => [
496
                'label' => Craft::t('webperf', 'Settings'),
497
            ],
498
        ];
499
    }
500
}
501