| Total Complexity | 52 |
| Total Lines | 532 |
| 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 |
||
| 53 | class Webperf extends Plugin |
||
| 54 | { |
||
| 55 | // Constants |
||
| 56 | // ========================================================================= |
||
| 57 | |||
| 58 | const RECOMMENDATIONS_CACHE_KEY = 'webperf-recommendations'; |
||
| 59 | const RECOMMENDATIONS_CACHE_DURATION = 60; |
||
| 60 | |||
| 61 | // Static Properties |
||
| 62 | // ========================================================================= |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Webperf |
||
| 66 | */ |
||
| 67 | public static $plugin; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Settings |
||
| 71 | */ |
||
| 72 | public static $settings; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int|null |
||
| 76 | */ |
||
| 77 | public static $requestUuid; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int|null |
||
| 81 | */ |
||
| 82 | public static $requestUrl; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | public static $beaconIncluded = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | public static $renderType = 'html'; |
||
| 93 | |||
| 94 | // Public Properties |
||
| 95 | // ========================================================================= |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | public $schemaVersion = '1.0.0'; |
||
| 101 | |||
| 102 | // Public Methods |
||
| 103 | // ========================================================================= |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritdoc |
||
| 107 | */ |
||
| 108 | public function init() |
||
| 109 | { |
||
| 110 | parent::init(); |
||
| 111 | // Initialize properties |
||
| 112 | self::$plugin = $this; |
||
| 113 | self::$settings = $this->getSettings(); |
||
| 114 | try { |
||
| 115 | self::$requestUuid = random_int(0, PHP_INT_MAX); |
||
| 116 | } catch (\Exception $e) { |
||
| 117 | self::$requestUuid = null; |
||
| 118 | } |
||
| 119 | $this->name = self::$settings->pluginName; |
||
| 120 | // Add in our components |
||
| 121 | $this->addComponents(); |
||
| 122 | // Install event listeners |
||
| 123 | $this->installEventListeners(); |
||
| 124 | // Load that we've loaded |
||
| 125 | Craft::info( |
||
| 126 | Craft::t( |
||
| 127 | 'webperf', |
||
| 128 | '{name} plugin loaded', |
||
| 129 | ['name' => $this->name] |
||
| 130 | ), |
||
| 131 | __METHOD__ |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritdoc |
||
| 137 | */ |
||
| 138 | public function getSettingsResponse() |
||
| 139 | { |
||
| 140 | // Just redirect to the plugin settings page |
||
| 141 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('webperf/settings')); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | */ |
||
| 147 | public function getCpNavItem() |
||
| 148 | { |
||
| 149 | $subNavs = []; |
||
| 150 | $navItem = parent::getCpNavItem(); |
||
| 151 | $cache = Craft::$app->getCache(); |
||
| 152 | // See if there are any recommendations to add as a badge |
||
| 153 | $recommendations = $cache->getOrSet(self::RECOMMENDATIONS_CACHE_KEY, function () { |
||
| 154 | $data = []; |
||
| 155 | $now = new \DateTime(); |
||
| 156 | $end = $now->format('Y-m-d'); |
||
| 157 | $start = $now->modify('-1 year')->format('Y-m-d'); |
||
| 158 | $stats = Webperf::$plugin->recommendations->data('', $start, $end); |
||
| 159 | if (!empty($stats)) { |
||
| 160 | $recSample = new RecommendationDataSample($stats); |
||
| 161 | $data = Webperf::$plugin->recommendations->list($recSample); |
||
| 162 | } |
||
| 163 | return count($data); |
||
| 164 | }, self::RECOMMENDATIONS_CACHE_DURATION); |
||
| 165 | if ($recommendations) { |
||
| 166 | $navItem['badgeCount'] = $recommendations; |
||
| 167 | } |
||
| 168 | $currentUser = Craft::$app->getUser()->getIdentity(); |
||
| 169 | // Only show sub-navs the user has permission to view |
||
| 170 | if ($currentUser->can('webperf:dashboard')) { |
||
| 171 | $subNavs['dashboard'] = [ |
||
| 172 | 'label' => 'Dashboard', |
||
| 173 | 'url' => 'webperf/dashboard', |
||
| 174 | ]; |
||
| 175 | } |
||
| 176 | if ($currentUser->can('webperf:pages')) { |
||
| 177 | $subNavs['pages'] = [ |
||
| 178 | 'label' => 'Pages', |
||
| 179 | 'url' => 'webperf/pages', |
||
| 180 | ]; |
||
| 181 | } |
||
| 182 | if ($currentUser->can('webperf:settings')) { |
||
| 183 | $subNavs['settings'] = [ |
||
| 184 | 'label' => 'Settings', |
||
| 185 | 'url' => 'webperf/settings', |
||
| 186 | ]; |
||
| 187 | } |
||
| 188 | $navItem = array_merge($navItem, [ |
||
| 189 | 'subnav' => $subNavs, |
||
| 190 | ]); |
||
| 191 | |||
| 192 | return $navItem; |
||
| 193 | } |
||
| 194 | |||
| 195 | // Protected Methods |
||
| 196 | // ========================================================================= |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add in our components |
||
| 200 | */ |
||
| 201 | protected function addComponents() |
||
| 202 | { |
||
| 203 | $request = Craft::$app->getRequest(); |
||
| 204 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 205 | $this->setRequestUrl(); |
||
| 206 | try { |
||
| 207 | $uri = $request->getPathInfo(); |
||
| 208 | } catch (InvalidConfigException $e) { |
||
| 209 | $uri = ''; |
||
| 210 | } |
||
| 211 | // Ignore our own controllers |
||
| 212 | if (self::$settings->includeCraftProfiling && strpos($uri, 'webperf/') === false) { |
||
| 213 | // Add in the ProfileTarget component |
||
| 214 | try { |
||
| 215 | $this->set('profileTarget', [ |
||
| 216 | 'class' => ProfileTarget::class, |
||
| 217 | 'levels' => ['profile'], |
||
| 218 | 'categories' => [], |
||
| 219 | 'logVars' => [], |
||
| 220 | 'except' => [], |
||
| 221 | ]); |
||
| 222 | } catch (InvalidConfigException $e) { |
||
| 223 | Craft::error($e->getMessage(), __METHOD__); |
||
| 224 | } |
||
| 225 | // Attach our log target |
||
| 226 | Craft::$app->getLog()->targets['webperf'] = $this->profileTarget; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set the request URL |
||
| 233 | * |
||
| 234 | * @param bool $force |
||
| 235 | */ |
||
| 236 | protected function setRequestUrl(bool $force = false) |
||
| 237 | { |
||
| 238 | self::$requestUrl = CraftDataSample::PLACEHOLDER_URL; |
||
| 239 | if (!self::$settings->includeBeacon || $force || self::$settings->staticCachedSite) { |
||
| 240 | $request = Craft::$app->getRequest(); |
||
| 241 | self::$requestUrl = UrlHelper::stripQueryString( |
||
| 242 | urldecode($request->getAbsoluteUrl()) |
||
| 243 | ); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Install our event listeners. |
||
| 249 | */ |
||
| 250 | protected function installEventListeners() |
||
| 251 | { |
||
| 252 | $request = Craft::$app->getRequest(); |
||
| 253 | // Add in our event listeners that are needed for every request |
||
| 254 | $this->installGlobalEventListeners(); |
||
| 255 | // Install only for non-console site requests |
||
| 256 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 257 | $this->installSiteEventListeners(); |
||
| 258 | } |
||
| 259 | // Install only for non-console Control Panel requests |
||
| 260 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
| 261 | $this->installCpEventListeners(); |
||
| 262 | } |
||
| 263 | // Handler: EVENT_AFTER_INSTALL_PLUGIN |
||
| 264 | Event::on( |
||
| 265 | Plugins::class, |
||
| 266 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
| 267 | function (PluginEvent $event) { |
||
| 268 | if ($event->plugin === $this) { |
||
| 269 | // Invalidate our caches after we've been installed |
||
| 270 | $this->clearAllCaches(); |
||
| 271 | // Send them to our welcome screen |
||
| 272 | $request = Craft::$app->getRequest(); |
||
| 273 | if ($request->isCpRequest) { |
||
| 274 | Craft::$app->getResponse()->redirect(UrlHelper::cpUrl( |
||
| 275 | 'webperf/dashboard', |
||
| 276 | [ |
||
| 277 | 'showWelcome' => true, |
||
| 278 | ] |
||
| 279 | ))->send(); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Install global event listeners for all request types |
||
| 288 | */ |
||
| 289 | protected function installGlobalEventListeners() |
||
| 290 | { |
||
| 291 | // Handler: CraftVariable::EVENT_INIT |
||
| 292 | Event::on( |
||
| 293 | CraftVariable::class, |
||
| 294 | CraftVariable::EVENT_INIT, |
||
| 295 | function (Event $event) { |
||
| 296 | /** @var CraftVariable $variable */ |
||
| 297 | $variable = $event->sender; |
||
| 298 | $variable->set('webperf', WebperfVariable::class); |
||
| 299 | } |
||
| 300 | ); |
||
| 301 | // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
||
| 302 | Event::on( |
||
| 303 | Plugins::class, |
||
| 304 | Plugins::EVENT_AFTER_LOAD_PLUGINS, |
||
| 305 | function () { |
||
| 306 | // Install these only after all other plugins have loaded |
||
| 307 | $request = Craft::$app->getRequest(); |
||
| 308 | // Only respond to non-console site requests |
||
| 309 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 310 | $this->handleSiteRequest(); |
||
| 311 | } |
||
| 312 | // Respond to Control Panel requests |
||
| 313 | if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
||
| 314 | $this->handleAdminCpRequest(); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | ); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Install site event listeners for site requests only |
||
| 322 | */ |
||
| 323 | protected function installSiteEventListeners() |
||
| 324 | { |
||
| 325 | // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
||
| 326 | Event::on( |
||
| 327 | UrlManager::class, |
||
| 328 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
||
| 329 | function (RegisterUrlRulesEvent $event) { |
||
| 330 | Craft::debug( |
||
| 331 | 'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
||
| 332 | __METHOD__ |
||
| 333 | ); |
||
| 334 | // Register our Control Panel routes |
||
| 335 | $event->rules = array_merge( |
||
| 336 | $event->rules, |
||
| 337 | $this->customFrontendRoutes() |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | ); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Install site event listeners for Control Panel requests only |
||
| 345 | */ |
||
| 346 | protected function installCpEventListeners() |
||
| 347 | { |
||
| 348 | // Handler: UrlManager::EVENT_REGISTER_CP_URL_RULES |
||
| 349 | Event::on( |
||
| 350 | UrlManager::class, |
||
| 351 | UrlManager::EVENT_REGISTER_CP_URL_RULES, |
||
| 352 | function (RegisterUrlRulesEvent $event) { |
||
| 353 | Craft::debug( |
||
| 354 | 'UrlManager::EVENT_REGISTER_CP_URL_RULES', |
||
| 355 | __METHOD__ |
||
| 356 | ); |
||
| 357 | // Register our Control Panel routes |
||
| 358 | $event->rules = array_merge( |
||
| 359 | $event->rules, |
||
| 360 | $this->customAdminCpRoutes() |
||
| 361 | ); |
||
| 362 | } |
||
| 363 | ); |
||
| 364 | // Handler: Dashboard::EVENT_REGISTER_WIDGET_TYPES |
||
| 365 | Event::on( |
||
| 366 | Dashboard::class, |
||
| 367 | Dashboard::EVENT_REGISTER_WIDGET_TYPES, |
||
| 368 | function (RegisterComponentTypesEvent $event) { |
||
| 369 | $event->types[] = MetricsWidget::class; |
||
| 370 | } |
||
| 371 | ); |
||
| 372 | // Handler: UserPermissions::EVENT_REGISTER_PERMISSIONS |
||
| 373 | Event::on( |
||
| 374 | UserPermissions::class, |
||
| 375 | UserPermissions::EVENT_REGISTER_PERMISSIONS, |
||
| 376 | function (RegisterUserPermissionsEvent $event) { |
||
| 377 | Craft::debug( |
||
| 378 | 'UserPermissions::EVENT_REGISTER_PERMISSIONS', |
||
| 379 | __METHOD__ |
||
| 380 | ); |
||
| 381 | // Register our custom permissions |
||
| 382 | $event->permissions[Craft::t('webperf', 'Webperf')] = $this->customAdminCpPermissions(); |
||
| 383 | } |
||
| 384 | ); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Handle site requests. We do it only after we receive the event |
||
| 389 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
| 390 | * before our event listeners kick in |
||
| 391 | */ |
||
| 392 | protected function handleSiteRequest() |
||
| 393 | { |
||
| 394 | // Don't include the beacon for response codes >= 400 |
||
| 395 | $response = Craft::$app->getResponse(); |
||
| 396 | if ($response->statusCode < 400) { |
||
| 397 | // Handler: View::EVENT_END_PAGE |
||
| 398 | Event::on( |
||
| 399 | View::class, |
||
| 400 | View::EVENT_END_PAGE, |
||
| 401 | function () { |
||
| 402 | Craft::debug( |
||
| 403 | 'View::EVENT_END_PAGE', |
||
| 404 | __METHOD__ |
||
| 405 | ); |
||
| 406 | $view = Craft::$app->getView(); |
||
| 407 | // The page is done rendering, include our beacon |
||
| 408 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
| 409 | switch (self::$renderType) { |
||
| 410 | case 'html': |
||
| 411 | Webperf::$plugin->beacons->includeHtmlBeacon(); |
||
| 412 | self::$beaconIncluded = true; |
||
| 413 | break; |
||
| 414 | case 'amp-html': |
||
| 415 | Webperf::$plugin->beacons->includeAmpHtmlScript(); |
||
| 416 | break; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | } |
||
| 420 | ); |
||
| 421 | // Handler: View::EVENT_END_BODY |
||
| 422 | Event::on( |
||
| 423 | View::class, |
||
| 424 | View::EVENT_END_BODY, |
||
| 425 | function () { |
||
| 426 | Craft::debug( |
||
| 427 | 'View::EVENT_END_BODY', |
||
| 428 | __METHOD__ |
||
| 429 | ); |
||
| 430 | $view = Craft::$app->getView(); |
||
| 431 | // The page is done rendering, include our beacon |
||
| 432 | if (Webperf::$settings->includeBeacon && $view->getIsRenderingPageTemplate()) { |
||
| 433 | switch (self::$renderType) { |
||
| 434 | case 'html': |
||
| 435 | break; |
||
| 436 | case 'amp-html': |
||
| 437 | Webperf::$plugin->beacons->includeAmpHtmlBeacon(); |
||
| 438 | self::$beaconIncluded = true; |
||
| 439 | break; |
||
| 440 | } |
||
| 441 | } |
||
| 442 | } |
||
| 443 | ); |
||
| 444 | // Handler: Application::EVENT_AFTER_REQUEST |
||
| 445 | Event::on( |
||
| 446 | Application::class, |
||
| 447 | Application::EVENT_AFTER_REQUEST, |
||
| 448 | function () { |
||
| 449 | Craft::debug( |
||
| 450 | 'Application::EVENT_AFTER_REQUEST', |
||
| 451 | __METHOD__ |
||
| 452 | ); |
||
| 453 | // If the beacon wasn't included, allow for the Craft timings |
||
| 454 | if (!self::$beaconIncluded) { |
||
| 455 | $this->setRequestUrl(true); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | ); |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Handle Control Panel requests. We do it only after we receive the event |
||
| 464 | * EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run |
||
| 465 | * before our event listeners kick in |
||
| 466 | */ |
||
| 467 | protected function handleAdminCpRequest() |
||
| 468 | { |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Clear all the caches! |
||
| 473 | */ |
||
| 474 | public function clearAllCaches() |
||
| 475 | { |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @inheritdoc |
||
| 480 | */ |
||
| 481 | protected function createSettingsModel() |
||
| 482 | { |
||
| 483 | return new Settings(); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @inheritdoc |
||
| 488 | */ |
||
| 489 | protected function settingsHtml(): string |
||
| 490 | { |
||
| 491 | return Craft::$app->view->renderTemplate( |
||
| 492 | 'webperf/settings', |
||
| 493 | [ |
||
| 494 | 'settings' => $this->getSettings() |
||
| 495 | ] |
||
| 496 | ); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Return the custom frontend routes |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | protected function customFrontendRoutes(): array |
||
| 505 | { |
||
| 506 | return [ |
||
| 507 | // Beacon |
||
| 508 | '/webperf/metrics/beacon' => 'webperf/metrics/beacon', |
||
| 509 | // Render |
||
| 510 | '/webperf/render/amp-iframe' => 'webperf/render/amp-iframe', |
||
| 511 | // Tables |
||
| 512 | '/webperf/tables/pages-index' => 'webperf/tables/pages-index', |
||
| 513 | '/webperf/tables/page-detail' => 'webperf/tables/page-detail', |
||
| 514 | // Charts |
||
| 515 | '/webperf/charts/dashboard-stats-average/<column:{handle}>' |
||
| 516 | => 'webperf/charts/dashboard-stats-average', |
||
| 517 | '/webperf/charts/dashboard-stats-average/<column:{handle}>/<siteId:\d+>' |
||
| 518 | => 'webperf/charts/dashboard-stats-average', |
||
| 519 | |||
| 520 | '/webperf/charts/dashboard-slowest-pages/<column:{handle}>/<limit:\d+>' |
||
| 521 | => 'webperf/charts/dashboard-slowest-pages', |
||
| 522 | '/webperf/charts/dashboard-slowest-pages/<column:{handle}>/<limit:\d+>/<siteId:\d+>' |
||
| 523 | => 'webperf/charts/dashboard-slowest-pages', |
||
| 524 | |||
| 525 | '/webperf/charts/pages-area-chart' |
||
| 526 | => 'webperf/charts/pages-area-chart', |
||
| 527 | '/webperf/charts/pages-area-chart/<siteId:\d+>' |
||
| 528 | => 'webperf/charts/pages-area-chart', |
||
| 529 | |||
| 530 | '/webperf/recommendations/list' |
||
| 531 | => 'webperf/recommendations/list', |
||
| 532 | '/webperf/recommendations/list/<siteId:\d+>' |
||
| 533 | => 'webperf/recommendations/list', |
||
| 534 | |||
| 535 | '/webperf/charts/widget/<days>' => 'webperf/charts/widget', |
||
| 536 | ]; |
||
| 537 | } |
||
| 538 | /** |
||
| 539 | * Return the custom Control Panel routes |
||
| 540 | * |
||
| 541 | * @return array |
||
| 542 | */ |
||
| 543 | protected function customAdminCpRoutes(): array |
||
| 544 | { |
||
| 545 | return [ |
||
| 546 | 'webperf' => 'webperf/sections/dashboard', |
||
| 547 | 'webperf/dashboard' => 'webperf/sections/dashboard', |
||
| 548 | 'webperf/dashboard/<siteHandle:{handle}>' => 'webperf/sections/dashboard', |
||
| 549 | |||
| 550 | 'webperf/pages' => 'webperf/sections/pages-index', |
||
| 551 | 'webperf/pages/<siteHandle:{handle}>' => 'webperf/sections/pages-index', |
||
| 552 | |||
| 553 | 'webperf/page-detail' => 'webperf/sections/page-detail', |
||
| 554 | 'webperf/page-detail/<siteHandle:{handle}>' => 'webperf/sections/page-detail', |
||
| 555 | |||
| 556 | 'webperf/settings' => 'webperf/settings/plugin-settings', |
||
| 557 | ]; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Returns the custom Control Panel user permissions. |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | protected function customAdminCpPermissions(): array |
||
| 585 | ], |
||
| 586 | ]; |
||
| 587 | } |
||
| 588 | } |
||
| 589 |