improvements-2.x-dev/modules/improvements_metrics/improvements_metrics.module
modules/improvements_metrics/improvements_metrics.module
<?php use Drupal\Core\Render\Markup; use Drupal\Core\Site\Settings; use Drupal\Core\StringTranslation\ByteSizeMarkup; use Drupal\druhels\ArrayHelper; use Drupal\druhels\SeoHelper; use Drupal\improvements\ImprovementsHelper; // @TODO Add tests /** * Implements hook_theme(). */ function improvements_metrics_theme(): array { return [ 'metrics' => [ 'variables' => [ 'metrics' => [], ], ], ]; } /** * Build metrics. */ function build_metrics(array &$build, bool $set = FALSE): void { if ($set) { $build['#metrics'] = [ 'date' => \Drupal::time()->getCurrentTime(), 'microtime' => microtime(TRUE), 'memory' => memory_get_usage(), ]; } elseif ($build) { $build['#metrics']['microtime'] = microtime(TRUE) - $build['#metrics']['microtime']; $build['#metrics']['memory'] = memory_get_usage() - $build['#metrics']['memory']; } } /** * Implements hook_page_attachments(). */ function improvements_metrics_page_attachments(array &$page): void { if (Settings::get('show_metrics')) { SeoHelper::attachMetatag($page, 'meta_build_time', 'page-build-time', ImprovementsHelper::getBuildTime()); } } /** * Implements hook_preprocess(). */ function improvements_metrics_preprocess(array &$vars): void { static $show_metrics; if ($show_metrics === NULL) { $show_metrics = Settings::get('show_metrics'); } $vars['show_metrics'] = $show_metrics; } /** * Preprocess function for metrics.html.twig. */ function improvements_metrics_preprocess_metrics(array &$vars): void { $vars['metrics']['date'] = date('Y-m-d H:i:s P', $vars['metrics']['date'] ?? \Drupal::time()->getCurrentTime()); if (!empty($vars['metrics']['microtime'])) { $vars['metrics']['microtime'] = round($vars['metrics']['microtime'] * 1000, 2) . ' ms'; } if (!empty($vars['metrics']['memory'])) { $vars['metrics']['memory'] = ByteSizeMarkup::create($vars['metrics']['memory'], 'en'); } $vars['metrics'] = ArrayHelper::removeEmptyElements($vars['metrics']); } /** * Preprocess function for block.html.twig. */ function improvements_metrics_preprocess_block(array &$vars): void { if ($vars['show_metrics']) { $vars['title_prefix']['metrics'] = [ '#theme' => 'metrics', '#metrics' => [ 'date' => $vars['content']['#metrics']['date'] ?? \Drupal::time()->getCurrentTime(), 'microtime' => $vars['content']['#metrics']['microtime'] ?? NULL, 'memory' => $vars['content']['#metrics']['memory'] ?? NULL, ], ]; } } /** * Preprocess function for views-view.html.twig. */ function improvements_metrics_preprocess_views_view(array &$vars): void { if ($vars['show_metrics']) { $vars['title_prefix']['metrics'] = [ '#theme' => 'metrics', '#metrics' => [ 'date' => \Drupal::time()->getCurrentTime(), ], ]; } } /** * Preprocess function for node.gtml.twig. */ function improvements_metrics_preprocess_node(array &$vars): void { if ($vars['show_metrics']) { $vars['title_prefix']['metrics'] = [ '#theme' => 'metrics', '#metrics' => [ 'date' => \Drupal::time()->getCurrentTime(), ], ]; } } /** * Preprocess function for commerce-product.html.twig. */ function improvements_metrics_preprocess_commerce_product(array &$vars): void { if (!empty($vars['show_metrics'])) { $vars['title_prefix']['metrics'] = [ '#theme' => 'metrics', '#metrics' => [ 'date' => \Drupal::time()->getCurrentTime(), ], ]; } } /** * Preprocess functiion for form.html.twig. */ function improvements_metrics_preprocess_form(array &$vars): void { if (isset($vars['element']['#metrics'])) { $metrics_build = [ '#theme' => 'metrics', '#metrics' => $vars['element']['#metrics'], ]; $vars['children'] = Markup::create(\Drupal::service('renderer')->render($metrics_build) . "\n" . $vars['children']); } }