| Total Complexity | 75 |
| Total Lines | 748 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Webperf often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Webperf, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | class Webperf extends Plugin |
||
| 61 | { |
||
| 62 | // Constants |
||
| 63 | // ========================================================================= |
||
| 64 | |||
| 65 | const RECOMMENDATIONS_CACHE_KEY = 'webperf-recommendations'; |
||
| 66 | const RECOMMENDATIONS_CACHE_DURATION = 60; |
||
| 67 | |||
| 68 | const ERRORS_CACHE_KEY = 'webperf-errors'; |
||
| 69 | const ERRORS_CACHE_DURATION = 60; |
||
| 70 | |||
| 71 | // Static Properties |
||
| 72 | // ========================================================================= |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Webperf |
||
| 76 | */ |
||
| 77 | public static $plugin; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Settings |
||
| 81 | */ |
||
| 82 | public static $settings; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var int|null |
||
| 86 | */ |
||
| 87 | public static $requestUuid; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int|null |
||
| 91 | */ |
||
| 92 | public static $requestUrl; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | public static $beaconIncluded = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | public static $renderType = 'html'; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | public static $craft31 = false; |
||
| 108 | |||
| 109 | // Public Properties |
||
| 110 | // ========================================================================= |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | public $schemaVersion = '1.0.0'; |
||
| 116 | |||
| 117 | // Public Methods |
||
| 118 | // ========================================================================= |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | public function init() |
||
| 124 | { |
||
| 125 | parent::init(); |
||
| 126 | // Initialize properties |
||
| 127 | self::$plugin = $this; |
||
| 128 | self::$settings = $this->getSettings(); |
||
| 129 | try { |
||
| 130 | self::$requestUuid = random_int(0, PHP_INT_MAX); |
||
| 131 | } catch (\Exception $e) { |
||
| 132 | self::$requestUuid = null; |
||
| 133 | } |
||
| 134 | self::$craft31 = version_compare(Craft::$app->getVersion(), '3.1', '>='); |
||
| 135 | $this->name = self::$settings->pluginName; |
||
| 136 | // Handle any console commands |
||
| 137 | $request = Craft::$app->getRequest(); |
||
| 138 | if ($request->getIsConsoleRequest()) { |
||
| 139 | $this->controllerNamespace = 'nystudio107\webperf\console\controllers'; |
||
| 140 | } |
||
| 141 | // Add in our components |
||
| 142 | $this->addComponents(); |
||
| 143 | // Install event listeners |
||
| 144 | $this->installEventListeners(); |
||
| 145 | // Load that we've loaded |
||
| 146 | Craft::info( |
||
| 147 | Craft::t( |
||
| 148 | 'webperf', |
||
| 149 | '{name} plugin loaded', |
||
| 150 | ['name' => $this->name] |
||
| 151 | ), |
||
| 152 | __METHOD__ |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | public function getSettingsResponse() |
||
| 160 | { |
||
| 161 | // Just redirect to the plugin settings page |
||
| 162 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('webperf/settings')); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | public function getCpNavItem() |
||
| 169 | { |
||
| 170 | $subNavs = []; |
||
| 171 | $navItem = parent::getCpNavItem(); |
||
| 172 | $recommendations = $this->getRecommendationsCount(); |
||
| 173 | $errors = $this->getErrorsCount(); |
||
| 174 | $navItem['badgeCount'] = $errors.$recommendations; |
||
| 175 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
| 176 | if ($currentUser) { |
||
| 177 | // Only show sub-navs the user has permission to view |
||
| 178 | if ($currentUser->can('webperf:dashboard')) { |
||
| 179 | $subNavs['dashboard'] = [ |
||
| 180 | 'label' => Craft::t('webperf', 'Dashboard'), |
||
| 181 | 'url' => 'webperf/dashboard', |
||
| 182 | ]; |
||
| 183 | } |
||
| 184 | if ($currentUser->can('webperf:performance')) { |
||
| 185 | $subNavs['performance'] = [ |
||
| 186 | 'label' => Craft::t('webperf', 'Performance'), |
||
| 187 | 'url' => 'webperf/performance', |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | if ($currentUser->can('webperf:errors')) { |
||
| 191 | $subNavs['errors'] = [ |
||
| 192 | 'label' => Craft::t('webperf', 'Errors').' '.$errors, |
||
| 193 | 'url' => 'webperf/errors', |
||
| 194 | 'badge' => $errors, |
||
| 195 | ]; |
||
| 196 | } |
||
| 197 | if ($currentUser->can('webperf:alerts')) { |
||
| 198 | $subNavs['alerts'] = [ |
||
| 199 | 'label' => 'Alerts', |
||
| 200 | 'url' => 'webperf/alerts', |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | if ($currentUser->can('webperf:settings')) { |
||
| 204 | $subNavs['settings'] = [ |
||
| 205 | 'label' => Craft::t('webperf', 'Settings'), |
||
| 206 | 'url' => 'webperf/settings', |
||
| 207 | ]; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | $navItem = array_merge($navItem, [ |
||
| 211 | 'subnav' => $subNavs, |
||
| 212 | ]); |
||
| 213 | |||
| 214 | return $navItem; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Clear all the caches! |
||
| 219 | */ |
||
| 220 | public function clearAllCaches() |
||
| 221 | { |
||
| 222 | } |
||
| 223 | |||
| 224 | // Protected Methods |
||
| 225 | // ========================================================================= |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Add in our components |
||
| 229 | */ |
||
| 230 | protected function addComponents() |
||
| 231 | { |
||
| 232 | $request = Craft::$app->getRequest(); |
||
| 233 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 234 | $this->setRequestUrl(); |
||
| 235 | try { |
||
| 236 | $uri = $request->getPathInfo(); |
||
| 237 | } catch (InvalidConfigException $e) { |
||
| 238 | $uri = ''; |
||
| 239 | } |
||
| 240 | // Ignore our own controllers |
||
| 241 | if (self::$settings->includeCraftProfiling && !$this->excludeUri($uri)) { |
||
| 242 | // Add in the ProfileTarget component |
||
| 243 | try { |
||
| 244 | $this->set('profileTarget', [ |
||
| 245 | 'class' => ProfileTarget::class, |
||
| 246 | 'levels' => ['profile'], |
||
| 247 | 'categories' => [], |
||
| 248 | 'logVars' => [], |
||
| 249 | 'except' => [], |
||
| 250 | ]); |
||
| 251 | } catch (InvalidConfigException $e) { |
||
| 252 | Craft::error($e->getMessage(), __METHOD__); |
||
| 253 | } |
||
| 254 | // Attach our log target |
||
| 255 | Craft::$app->getLog()->targets['webperf-profile'] = $this->profileTarget; |
||
| 256 | // Add in the ErrorsTarget component |
||
| 257 | $except = []; |
||
| 258 | // If devMode is on, exclude errors/warnings from `seomatic` |
||
| 259 | if (Craft::$app->getConfig()->getGeneral()->devMode) { |
||
| 260 | $except = ['nystudio107\seomatic\*']; |
||
| 261 | } |
||
| 262 | $levels = ['error']; |
||
| 263 | if (self::$settings->includeCraftWarnings) { |
||
| 264 | $levels[] = 'warning'; |
||
| 265 | } |
||
| 266 | try { |
||
| 267 | $this->set('errorsTarget', [ |
||
| 268 | 'class' => ErrorsTarget::class, |
||
| 269 | 'levels' => $levels, |
||
| 270 | 'categories' => [], |
||
| 271 | 'logVars' => [], |
||
| 272 | 'except' => $except, |
||
| 273 | ]); |
||
| 274 | } catch (InvalidConfigException $e) { |
||
| 275 | Craft::error($e->getMessage(), __METHOD__); |
||
| 276 | } |
||
| 277 | // Attach our log target |
||
| 278 | Craft::$app->getLog()->targets['webperf-errors'] = $this->errorsTarget; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Set the request URL |
||
| 285 | * |
||
| 286 | * @param bool $force |
||
| 287 | */ |
||
| 288 | protected function setRequestUrl(bool $force = false) |
||
| 289 | { |
||
| 290 | self::$requestUrl = CraftDataSample::PLACEHOLDER_URL; |
||
| 291 | if (!self::$settings->includeBeacon || $force || self::$settings->staticCachedSite) { |
||
| 292 | $request = Craft::$app->getRequest(); |
||
| 293 | self::$requestUrl = UrlHelper::stripQueryString( |
||
| 294 | urldecode($request->getAbsoluteUrl()) |
||
| 295 | ); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Install our event listeners. |
||
| 301 | */ |
||
| 302 | protected function installEventListeners() |
||
| 303 | { |
||
| 304 | $request = Craft::$app->getRequest(); |
||
| 305 | // Add in our event listeners that are needed for every request |
||
| 306 | $this->installGlobalEventListeners(); |
||
| 307 | // Install only for non-console site requests |
||
| 308 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 309 | $this->installSiteEventListeners(); |
||
| 310 | } |
||
| 311 | // Install only for non-console Control Panel requests |
||
| 312 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
| 313 | $this->installCpEventListeners(); |
||
| 314 | } |
||
| 315 | // Handler: EVENT_AFTER_INSTALL_PLUGIN |
||
| 316 | Event::on( |
||
| 317 | Plugins::class, |
||
| 318 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 319 | function (PluginEvent $event) { |
||
| 320 | if ($event->plugin === $this) { |
||
| 321 | // Invalidate our caches after we've been installed |
||
| 322 | $this->clearAllCaches(); |
||
| 323 | // Send them to our welcome screen |
||
| 324 | $request = Craft::$app->getRequest(); |
||
| 325 | if ($request->isCpRequest) { |
||
| 326 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl( |
||
| 327 | 'webperf/dashboard', |
||
| 328 | [ |
||
| 329 | 'showWelcome' => true, |
||
| 330 | ] |
||
| 331 | ))->send(); |
||
| 332 | } |
||
| 333 | } |
||
| 334 | } |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Install global event listeners for all request types |
||
| 340 | */ |
||
| 341 | protected function installGlobalEventListeners() |
||
| 342 | { |
||
| 343 | // Handler: CraftVariable::EVENT_INIT |
||
| 344 | Event::on( |
||
| 345 | CraftVariable::class, |
||
| 346 | CraftVariable::EVENT_INIT, |
||
| 347 | function (Event $event) { |
||
| 348 | /** @var CraftVariable $variable */ |
||
| 349 | $variable = $event->sender; |
||
| 350 | $variable->set('webperf', WebperfVariable::class); |
||
| 351 | } |
||
| 352 | ); |
||
| 353 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
| 354 | Event::on( |
||
| 355 | Plugins::class, |
||
| 356 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
| 357 | function () { |
||
| 358 | // Install these only after all other plugins have loaded |
||
| 359 | $request = Craft::$app->getRequest(); |
||
| 360 | // Only respond to non-console site requests |
||
| 361 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 362 | $this->handleSiteRequest(); |
||
| 363 | } |
||
| 364 | // Respond to Control Panel requests |
||
| 365 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
| 366 | $this->handleAdminCpRequest(); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | ); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Install site event listeners for site requests only |
||
| 374 | */ |
||
| 375 | protected function installSiteEventListeners() |
||
| 376 | { |
||
| 377 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
| 378 | Event::on( |
||
| 379 | UrlManager::class, |
||
| 380 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
| 381 | function (RegisterUrlRulesEvent $event) { |
||
| 382 | Craft::debug( |
||
| 383 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
| 384 | __METHOD__ |
||
| 385 | ); |
||
| 386 | // Register our Control Panel routes |
||
| 387 | $event->rules = array_merge( |
||
| 388 | $event->rules, |
||
| 389 | $this->customFrontendRoutes() |
||
| 390 | ); |
||
| 391 | } |
||
| 392 | ); |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Install site event listeners for Control Panel requests only |
||
| 397 | */ |
||
| 398 | protected function installCpEventListeners() |
||
| 399 | { |
||
| 400 | // Handler: UrlManager::EVENT_REGISTER_CP_URL_RULES |
||
| 401 | Event::on( |
||
| 402 | UrlManager::class, |
||
| 403 | UrlManager::EVENT_REGISTER_CP_URL_RULES, |
||
| 404 | function (RegisterUrlRulesEvent $event) { |
||
| 405 | Craft::debug( |
||
| 406 | 'UrlManager::EVENT_REGISTER_CP_URL_RULES', |
||
| 407 | __METHOD__ |
||
| 408 | ); |
||
| 409 | // Register our Control Panel routes |
||
| 410 | $event->rules = array_merge( |
||
| 411 | $event->rules, |
||
| 412 | $this->customAdminCpRoutes() |
||
| 413 | ); |
||
| 414 | } |
||
| 415 | ); |
||
| 416 | // Handler: Dashboard::EVENT_REGISTER_WIDGET_TYPES |
||
| 417 | Event::on( |
||
| 418 | Dashboard::class, |
||
| 419 | Dashboard::EVENT_REGISTER_WIDGET_TYPES, |
||
| 420 | function (RegisterComponentTypesEvent $event) { |
||
| 421 | $event->types[] = MetricsWidget::class; |
||
| 422 | } |
||
| 423 | ); |
||
| 424 | // Handler: UserPermissions::EVENT_REGISTER_PERMISSIONS |
||
| 425 | Event::on( |
||
| 426 | UserPermissions::class, |
||
| 427 | UserPermissions::EVENT_REGISTER_PERMISSIONS, |
||
| 428 | function (RegisterUserPermissionsEvent $event) { |
||
| 429 | Craft::debug( |
||
| 430 | 'UserPermissions::EVENT_REGISTER_PERMISSIONS', |
||
| 431 | __METHOD__ |
||
| 432 | ); |
||
| 433 | // Register our custom permissions |
||
| 434 | $event->permissions[Craft::t('webperf', 'Webperf')] = $this->customAdminCpPermissions(); |
||
| 435 | } |
||
| 436 | ); |
||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Handle site requests. We do it only after we receive the event |
||
| 441 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
| 442 | * before our event listeners kick in |
||
| 443 | */ |
||
| 444 | protected function handleSiteRequest() |
||
| 445 | { |
||
| 446 | $request = Craft::$app->getRequest(); |
||
| 447 | try { |
||
| 448 | $uri = $request->getPathInfo(); |
||
| 449 | } catch (InvalidConfigException $e) { |
||
| 450 | $uri = ''; |
||
| 451 | } |
||
| 452 | // Don't include the beacon for response codes >= 400 |
||
| 453 | $response = Craft::$app->getResponse(); |
||
| 454 | if ($response->statusCode < 400 && !$this->excludeUri($uri)) { |
||
| 455 | // Handler: View::EVENT_END_PAGE |
||
| 456 | Event::on( |
||
| 457 | View::class, |
||
| 458 | View::EVENT_END_PAGE, |
||
| 459 | function () { |
||
| 460 | Craft::debug( |
||
| 461 | 'View::EVENT_END_PAGE', |
||
| 462 | __METHOD__ |
||
| 463 | ); |
||
| 464 | $view = Craft::$app->getView(); |
||
| 465 | // The page is done rendering, include our beacon |
||
| 466 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
| 467 | switch (self::$renderType) { |
||
| 468 | case 'html': |
||
| 469 | Webperf::$plugin->beacons->includeHtmlBeacon(); |
||
| 470 | self::$beaconIncluded = true; |
||
| 471 | break; |
||
| 472 | case 'amp-html': |
||
| 473 | Webperf::$plugin->beacons->includeAmpHtmlScript(); |
||
| 474 | break; |
||
| 475 | } |
||
| 476 | } |
||
| 477 | } |
||
| 478 | ); |
||
| 479 | // Handler: View::EVENT_END_BODY |
||
| 480 | Event::on( |
||
| 481 | View::class, |
||
| 482 | View::EVENT_END_BODY, |
||
| 483 | function () { |
||
| 484 | Craft::debug( |
||
| 485 | 'View::EVENT_END_BODY', |
||
| 486 | __METHOD__ |
||
| 487 | ); |
||
| 488 | $view = Craft::$app->getView(); |
||
| 489 | // The page is done rendering, include our beacon |
||
| 490 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
| 491 | switch (self::$renderType) { |
||
| 492 | case 'html': |
||
| 493 | break; |
||
| 494 | case 'amp-html': |
||
| 495 | Webperf::$plugin->beacons->includeAmpHtmlBeacon(); |
||
| 496 | self::$beaconIncluded = true; |
||
| 497 | break; |
||
| 498 | } |
||
| 499 | } |
||
| 500 | } |
||
| 501 | ); |
||
| 502 | // Handler: Application::EVENT_AFTER_REQUEST |
||
| 503 | Event::on( |
||
| 504 | Application::class, |
||
| 505 | Application::EVENT_AFTER_REQUEST, |
||
| 506 | function () { |
||
| 507 | Craft::debug( |
||
| 508 | 'Application::EVENT_AFTER_REQUEST', |
||
| 509 | __METHOD__ |
||
| 510 | ); |
||
| 511 | // If the beacon wasn't included, allow for the Craft timings |
||
| 512 | if (!self::$beaconIncluded) { |
||
| 513 | $this->setRequestUrl(true); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | ); |
||
| 517 | } |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Handle Control Panel requests. We do it only after we receive the event |
||
| 522 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
| 523 | * before our event listeners kick in |
||
| 524 | */ |
||
| 525 | protected function handleAdminCpRequest() |
||
| 526 | { |
||
| 527 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
| 528 | // Only show sub-navs the user has permission to view |
||
| 529 | if (self::$settings->displaySidebar && $currentUser && $currentUser->can('webperf:sidebar')) { |
||
| 530 | $view = Craft::$app->getView(); |
||
| 531 | // Entries sidebar |
||
| 532 | $view->hook('cp.entries.edit.details', function (&$context) { |
||
| 533 | /** @var Element $element */ |
||
| 534 | $element = $context['entry'] ?? null; |
||
| 535 | |||
| 536 | return $this->renderSidebar($element); |
||
| 537 | }); |
||
| 538 | // Category Groups sidebar |
||
| 539 | $view->hook('cp.categories.edit.details', function (&$context) { |
||
| 540 | /** @var Element $element */ |
||
| 541 | $element = $context['category'] ?? null; |
||
| 542 | |||
| 543 | return $this->renderSidebar($element); |
||
| 544 | }); |
||
| 545 | // Commerce Product Types sidebar |
||
| 546 | $view->hook('cp.commerce.product.edit.details', function (&$context) { |
||
| 547 | /** @var Element $element */ |
||
| 548 | $element = $context['product'] ?? null; |
||
| 549 | |||
| 550 | return $this->renderSidebar($element); |
||
| 551 | }); |
||
| 552 | } |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param Element $element |
||
| 557 | * |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | protected function renderSidebar(Element $element): string |
||
| 561 | { |
||
| 562 | $html = ''; |
||
| 563 | if ($element !== null && $element->url !== null) { |
||
| 564 | $view = Craft::$app->getView(); |
||
| 565 | try { |
||
| 566 | $view->registerAssetBundle(WebperfAsset::class); |
||
| 567 | } catch (InvalidConfigException $e) { |
||
| 568 | } |
||
| 569 | try { |
||
| 570 | $now = new \DateTime(); |
||
| 571 | } catch (\Exception $e) { |
||
| 572 | return $html; |
||
| 573 | } |
||
| 574 | $end = $now->format('Y-m-d'); |
||
| 575 | $start = $now->modify('-30 days')->format('Y-m-d'); |
||
| 576 | $html .= PluginTemplate::renderPluginTemplate( |
||
| 577 | '_sidebars/webperf-sidebar.twig', |
||
| 578 | [ |
||
| 579 | 'settings' => self::$settings, |
||
| 580 | 'pageUrl' => $element->url, |
||
| 581 | 'start' => $start, |
||
| 582 | 'end' => $end, |
||
| 583 | 'currentSiteId' => $element->siteId ?? 0, |
||
| 584 | ] |
||
| 585 | ); |
||
| 586 | } |
||
| 587 | |||
| 588 | return $html; |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * @param $uri |
||
| 593 | * |
||
| 594 | * @return bool |
||
| 595 | */ |
||
| 596 | protected function excludeUri($uri): bool |
||
| 597 | { |
||
| 598 | foreach (self::$settings->excludePatterns as $excludePattern) { |
||
| 599 | $pattern = '`'.$excludePattern['pattern'].'`i'; |
||
| 600 | if (preg_match($pattern, $uri) === 1) { |
||
| 601 | return true; |
||
| 602 | } |
||
| 603 | } |
||
| 604 | |||
| 605 | return false; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @inheritdoc |
||
| 610 | */ |
||
| 611 | protected function createSettingsModel() |
||
| 612 | { |
||
| 613 | return new Settings(); |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @inheritdoc |
||
| 618 | */ |
||
| 619 | protected function settingsHtml(): string |
||
| 620 | { |
||
| 621 | return Craft::$app->view->renderTemplate( |
||
| 622 | 'webperf/settings', |
||
| 623 | [ |
||
| 624 | 'settings' => $this->getSettings() |
||
| 625 | ] |
||
| 626 | ); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Return the custom frontend routes |
||
| 631 | * |
||
| 632 | * @return array |
||
| 633 | */ |
||
| 634 | protected function customFrontendRoutes(): array |
||
| 635 | { |
||
| 636 | return [ |
||
| 637 | // Beacon |
||
| 638 | '/webperf/metrics/beacon' => 'webperf/metrics/beacon', |
||
| 639 | // Render |
||
| 640 | '/webperf/render/amp-iframe' => 'webperf/render/amp-iframe', |
||
| 641 | // Tables |
||
| 642 | '/webperf/tables/pages-index' => 'webperf/tables/pages-index', |
||
| 643 | '/webperf/tables/page-detail' => 'webperf/tables/page-detail', |
||
| 644 | '/webperf/tables/errors-index' => 'webperf/tables/errors-index', |
||
| 645 | '/webperf/tables/errors-detail' => 'webperf/tables/errors-detail', |
||
| 646 | // Charts |
||
| 647 | '/webperf/charts/dashboard-stats-average/<column:{handle}>' |
||
| 648 | => 'webperf/charts/dashboard-stats-average', |
||
| 649 | '/webperf/charts/dashboard-stats-average/<column:{handle}>/<siteId:\d+>' |
||
| 650 | => 'webperf/charts/dashboard-stats-average', |
||
| 651 | |||
| 652 | '/webperf/charts/dashboard-slowest-pages/<column:{handle}>/<limit:\d+>' |
||
| 653 | => 'webperf/charts/dashboard-slowest-pages', |
||
| 654 | '/webperf/charts/dashboard-slowest-pages/<column:{handle}>/<limit:\d+>/<siteId:\d+>' |
||
| 655 | => 'webperf/charts/dashboard-slowest-pages', |
||
| 656 | |||
| 657 | '/webperf/charts/pages-area-chart' |
||
| 658 | => 'webperf/charts/pages-area-chart', |
||
| 659 | '/webperf/charts/pages-area-chart/<siteId:\d+>' |
||
| 660 | => 'webperf/charts/pages-area-chart', |
||
| 661 | |||
| 662 | '/webperf/charts/errors-area-chart' |
||
| 663 | => 'webperf/charts/errors-area-chart', |
||
| 664 | '/webperf/charts/errors-area-chart/<siteId:\d+>' |
||
| 665 | => 'webperf/charts/errors-area-chart', |
||
| 666 | |||
| 667 | '/webperf/recommendations/list' |
||
| 668 | => 'webperf/recommendations/list', |
||
| 669 | '/webperf/recommendations/list/<siteId:\d+>' |
||
| 670 | => 'webperf/recommendations/list', |
||
| 671 | |||
| 672 | '/webperf/charts/widget/<days>' => 'webperf/charts/widget', |
||
| 673 | ]; |
||
| 674 | } |
||
| 675 | /** |
||
| 676 | * Return the custom Control Panel routes |
||
| 677 | * |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | protected function customAdminCpRoutes(): array |
||
| 681 | { |
||
| 682 | return [ |
||
| 683 | 'webperf' => 'webperf/sections/dashboard', |
||
| 684 | 'webperf/dashboard' => 'webperf/sections/dashboard', |
||
| 685 | 'webperf/dashboard/<siteHandle:{handle}>' => 'webperf/sections/dashboard', |
||
| 686 | |||
| 687 | 'webperf/performance' => 'webperf/sections/pages-index', |
||
| 688 | 'webperf/performance/<siteHandle:{handle}>' => 'webperf/sections/pages-index', |
||
| 689 | |||
| 690 | 'webperf/performance/page-detail' => 'webperf/sections/page-detail', |
||
| 691 | 'webperf/performance/page-detail/<siteHandle:{handle}>' => 'webperf/sections/page-detail', |
||
| 692 | |||
| 693 | 'webperf/errors' => 'webperf/sections/errors-index', |
||
| 694 | 'webperf/errors/<siteHandle:{handle}>' => 'webperf/sections/errors-index', |
||
| 695 | |||
| 696 | 'webperf/errors/page-detail' => 'webperf/sections/errors-detail', |
||
| 697 | 'webperf/errors/page-detail/<siteHandle:{handle}>' => 'webperf/errors/page-detail', |
||
| 698 | |||
| 699 | 'webperf/alerts' => 'webperf/sections/alerts', |
||
| 700 | 'webperf/alerts/<siteHandle:{handle}>' => 'webperf/sections/alerts', |
||
| 701 | |||
| 702 | 'webperf/settings' => 'webperf/settings/plugin-settings', |
||
| 703 | ]; |
||
| 704 | } |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Returns the custom Control Panel user permissions. |
||
| 708 | * |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | protected function customAdminCpPermissions(): array |
||
| 712 | { |
||
| 713 | return [ |
||
| 714 | 'webperf:dashboard' => [ |
||
| 715 | 'label' => Craft::t('webperf', 'Dashboard'), |
||
| 716 | ], |
||
| 717 | 'webperf:performance' => [ |
||
| 718 | 'label' => Craft::t('webperf', 'Performance'), |
||
| 719 | 'nested' => [ |
||
| 720 | 'webperf:performance-detail' => [ |
||
| 721 | 'label' => Craft::t('webperf', 'Performance Detail'), |
||
| 722 | ], |
||
| 723 | 'webperf:delete-data-samples' => [ |
||
| 724 | 'label' => Craft::t('webperf', 'Delete Data Samples'), |
||
| 725 | ], |
||
| 726 | ], |
||
| 727 | ], |
||
| 728 | 'webperf:errors' => [ |
||
| 729 | 'label' => Craft::t('webperf', 'Errors'), |
||
| 730 | 'nested' => [ |
||
| 731 | 'webperf:errors-detail' => [ |
||
| 732 | 'label' => Craft::t('webperf', 'Errors Detail'), |
||
| 733 | ], |
||
| 734 | 'webperf:delete-error-samples' => [ |
||
| 735 | 'label' => Craft::t('webperf', 'Delete Error Samples'), |
||
| 736 | ], |
||
| 737 | ], |
||
| 738 | ], |
||
| 739 | 'webperf:alerts' => [ |
||
| 740 | 'label' => Craft::t('webperf', 'Alerts'), |
||
| 741 | ], |
||
| 742 | 'webperf:recommendations' => [ |
||
| 743 | 'label' => Craft::t('webperf', 'Recommendations'), |
||
| 744 | ], |
||
| 745 | 'webperf:sidebar' => [ |
||
| 746 | 'label' => Craft::t('webperf', 'Performance Sidebar'), |
||
| 747 | ], |
||
| 748 | 'webperf:settings' => [ |
||
| 749 | 'label' => Craft::t('webperf', 'Settings'), |
||
| 750 | ], |
||
| 751 | ]; |
||
| 752 | } |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Get a string value with the number of recommendations |
||
| 756 | * |
||
| 757 | * @return string |
||
| 758 | */ |
||
| 759 | protected function getRecommendationsCount(): string |
||
| 760 | { |
||
| 761 | $cache = Craft::$app->getCache(); |
||
| 762 | // See if there are any recommendations to add as a badge |
||
| 763 | $recommendations = $cache->getOrSet(self::RECOMMENDATIONS_CACHE_KEY, function () { |
||
| 764 | $data = []; |
||
| 765 | $now = new \DateTime(); |
||
| 766 | $end = $now->format('Y-m-d'); |
||
| 767 | $start = $now->modify('-30 days')->format('Y-m-d'); |
||
| 768 | $stats = Webperf::$plugin->recommendations->data('', $start, $end); |
||
| 769 | if (!empty($stats)) { |
||
| 770 | $recSample = new RecommendationDataSample($stats); |
||
| 771 | $data = Webperf::$plugin->recommendations->list($recSample); |
||
| 772 | } |
||
| 773 | |||
| 774 | return count($data); |
||
| 775 | }, self::RECOMMENDATIONS_CACHE_DURATION); |
||
| 776 | |||
| 777 | if (!$recommendations) { |
||
| 778 | $recommendations = ''; |
||
| 779 | } |
||
| 780 | |||
| 781 | return (string)$recommendations; |
||
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Get a string value with the number of errors |
||
| 786 | * |
||
| 787 | * @return string |
||
| 788 | */ |
||
| 789 | protected function getErrorsCount(): string |
||
| 808 | } |
||
| 809 | } |
||
| 810 |