From cd8e9972a9ae78f0a8b7d77034110a8043a3cd17 Mon Sep 17 00:00:00 2001 From: ArrayIterator Date: Sun, 15 Oct 2023 16:52:42 +0700 Subject: [PATCH] patch updates & fix --- .../Aggregator/DatabaseAggregator.php | 14 + src/Benchmark/Aggregator/EventAggregator.php | 14 + src/Benchmark/Aggregator/KernelAggregator.php | 14 + .../Aggregator/MiddlewareAggregator.php | 14 + .../Aggregator/ServicesAggregator.php | 14 + src/Benchmark/Waterfall.php | 485 ++++++++++-------- src/HttpKernel/Traits/HttpKernelInitTrait.php | 13 + .../Adapter/Gettext/PoMoAdapter.php | 20 + src/Middleware/AbstractMiddleware.php | 24 +- src/Middleware/ErrorMiddleware.php | 2 - src/Util/Filter/HtmlAttributes.php | 78 ++- 11 files changed, 470 insertions(+), 222 deletions(-) diff --git a/src/Benchmark/Aggregator/DatabaseAggregator.php b/src/Benchmark/Aggregator/DatabaseAggregator.php index 238e2eb..0872fbb 100644 --- a/src/Benchmark/Aggregator/DatabaseAggregator.php +++ b/src/Benchmark/Aggregator/DatabaseAggregator.php @@ -4,10 +4,24 @@ namespace ArrayAccess\TrayDigita\Benchmark\Aggregator; use ArrayAccess\TrayDigita\Benchmark\Aggregate\AbstractAggregator; +use ArrayAccess\TrayDigita\L10n\Translations\Interfaces\TranslatorInterface; +use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; class DatabaseAggregator extends AbstractAggregator { protected string $name = 'Database'; protected string $groupName = 'database'; + + private bool $translated = false; + + public function getName(): string + { + if ($this->translated) { + return $this->name; + } + $this->translated = true; + return $this->name = ContainerHelper::use(TranslatorInterface::class) + ?->translate('Database', context: 'benchmark')??$this->name; + } } diff --git a/src/Benchmark/Aggregator/EventAggregator.php b/src/Benchmark/Aggregator/EventAggregator.php index 7eefcc7..9a2401b 100644 --- a/src/Benchmark/Aggregator/EventAggregator.php +++ b/src/Benchmark/Aggregator/EventAggregator.php @@ -5,6 +5,8 @@ use ArrayAccess\TrayDigita\Benchmark\Aggregate\AbstractAggregator; use ArrayAccess\TrayDigita\Benchmark\Interfaces\RecordInterface; +use ArrayAccess\TrayDigita\L10n\Translations\Interfaces\TranslatorInterface; +use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; class EventAggregator extends AbstractAggregator { @@ -30,6 +32,18 @@ class EventAggregator extends AbstractAggregator 'assetsJsCssQueue' => true, ]; + private bool $translated = false; + + public function getName(): string + { + if ($this->translated) { + return $this->name; + } + $this->translated = true; + return $this->name = ContainerHelper::use(TranslatorInterface::class) + ?->translate('Event', context: 'benchmark')??$this->name; + } + public function addBlacklistedGroup(string $name): void { $this->blackListed[$name] = true; diff --git a/src/Benchmark/Aggregator/KernelAggregator.php b/src/Benchmark/Aggregator/KernelAggregator.php index 9a226aa..34d2121 100644 --- a/src/Benchmark/Aggregator/KernelAggregator.php +++ b/src/Benchmark/Aggregator/KernelAggregator.php @@ -5,6 +5,8 @@ use ArrayAccess\TrayDigita\Benchmark\Aggregate\AbstractAggregator; use ArrayAccess\TrayDigita\Benchmark\Interfaces\RecordInterface; +use ArrayAccess\TrayDigita\L10n\Translations\Interfaces\TranslatorInterface; +use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; class KernelAggregator extends AbstractAggregator { @@ -17,6 +19,18 @@ class KernelAggregator extends AbstractAggregator 'kernel' => true, ]; + private bool $translated = false; + + public function getName(): string + { + if ($this->translated) { + return $this->name; + } + $this->translated = true; + return $this->name = ContainerHelper::use(TranslatorInterface::class) + ?->translate('Kernel', context: 'benchmark')??$this->name; + } + public function accepted(RecordInterface $record): bool { return isset($this->accepted[$record->getGroup()->getName()]); diff --git a/src/Benchmark/Aggregator/MiddlewareAggregator.php b/src/Benchmark/Aggregator/MiddlewareAggregator.php index 82c3914..3f67e72 100644 --- a/src/Benchmark/Aggregator/MiddlewareAggregator.php +++ b/src/Benchmark/Aggregator/MiddlewareAggregator.php @@ -5,6 +5,8 @@ use ArrayAccess\TrayDigita\Benchmark\Aggregate\AbstractAggregator; use ArrayAccess\TrayDigita\Benchmark\Interfaces\RecordInterface; +use ArrayAccess\TrayDigita\L10n\Translations\Interfaces\TranslatorInterface; +use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; class MiddlewareAggregator extends AbstractAggregator { @@ -17,6 +19,18 @@ class MiddlewareAggregator extends AbstractAggregator 'middlewareDispatcher' => true, ]; + private bool $translated = false; + + public function getName(): string + { + if ($this->translated) { + return $this->name; + } + $this->translated = true; + return $this->name = ContainerHelper::use(TranslatorInterface::class) + ?->translate('Middleware', context: 'benchmark')??$this->name; + } + public function accepted(RecordInterface $record): bool { return isset($this->accepted[$record->getGroup()->getName()]); diff --git a/src/Benchmark/Aggregator/ServicesAggregator.php b/src/Benchmark/Aggregator/ServicesAggregator.php index 7a168ff..bdc7b34 100644 --- a/src/Benchmark/Aggregator/ServicesAggregator.php +++ b/src/Benchmark/Aggregator/ServicesAggregator.php @@ -5,6 +5,8 @@ use ArrayAccess\TrayDigita\Benchmark\Aggregate\AbstractAggregator; use ArrayAccess\TrayDigita\Benchmark\Interfaces\RecordInterface; +use ArrayAccess\TrayDigita\L10n\Translations\Interfaces\TranslatorInterface; +use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper; class ServicesAggregator extends AbstractAggregator { @@ -26,6 +28,18 @@ class ServicesAggregator extends AbstractAggregator 'assetsJsCssQueue' => true, ]; + private bool $translated = false; + + public function getName(): string + { + if ($this->translated) { + return $this->name; + } + $this->translated = true; + return $this->name = ContainerHelper::use(TranslatorInterface::class) + ?->translate('Service', context: 'benchmark')??$this->name; + } + public function addAcceptedGroup(string $name): void { $this->accepted[$name] = true; diff --git a/src/Benchmark/Waterfall.php b/src/Benchmark/Waterfall.php index 89e2e10..4dfeacb 100644 --- a/src/Benchmark/Waterfall.php +++ b/src/Benchmark/Waterfall.php @@ -1,5 +1,4 @@ 'waterfall-profiler-severity-critical', - SeverityInterface::WARNING => 'waterfall-profiler-severity-warning', - SeverityInterface::NOTICE => 'waterfall-profiler-severity-notice', - SeverityInterface::INFO => 'waterfall-profiler-severity-info', - SeverityInterface::NONE => 'waterfall-profiler-severity-none', + SeverityInterface::CRITICAL => 'severity-critical', + SeverityInterface::WARNING => 'severity-warning', + SeverityInterface::NOTICE => 'severity-notice', + SeverityInterface::INFO => 'severity-info', + SeverityInterface::NONE => 'severity-none', ]; + protected string $prefix = 'waterfall-profiler-'; + + protected array $severityHtmlClassesName = []; + + /** + * @var string + */ + protected string $unknownSeverityClassName; + private ProfilerInterface $profiler; private ?MetadataFormatterInterface $formatter = null; - public function __construct(ProfilerInterface $profiler) - { + public function __construct( + ProfilerInterface $profiler, + ContainerInterface $container = null + ) { $this->profiler = $profiler; + if ($container) { + $this->setContainer($container); + } + $this->unknownSeverityClassName = $this->prefix . self::UNKNOWN_SEVERITY_CLASSNAME; + foreach (self::SEVERITY_HTML_CLASS_NAMES as $severityName => $severityClass) { + $this->severityHtmlClassesName[$severityName] = $this->prefix . $severityClass; + } } public function getFormatter(): ?MetadataFormatterInterface @@ -84,37 +107,22 @@ public function getProfiler() : Profiler * * @return string */ - public static function getSeverityHtmlClassName( + public function getSeverityHtmlClassName( int $type ) : string { - return self::SEVERITY_HTML_CLASS_NAMES[$type]??self::UNKNOWN_SEVERITY_CLASSNAME; + return $this->severityHtmlClassesName[$type]??$this->unknownSeverityClassName; } - private function filterAttribute(array $attributes) : array + private function filterAttribute(array $attributes) : string { - $attributes = array_filter( - $attributes, - static fn ($e) => is_string($e) && preg_match('~^[a-z](?:[a-z0-9_-]*[a-z0-9])?$~i', $e), - ARRAY_FILTER_USE_KEY - ); if (isset($attributes['class'])) { + if (!is_array($attributes['class'])) { + $attributes['class'] = [$attributes['class']]; + } $attributes['class'] = implode(' ', $this->filterClass($attributes['class'])); } $attributes = array_filter($attributes, 'is_scalar'); - $attribute = []; - foreach ($attributes as $key => $v) { - $v = (string) $v; - if ($v === '') { - $attribute[] = $key; - continue; - } - $attribute[] = sprintf( - '%s="%s"', - htmlspecialchars($key), - htmlspecialchars($v) - ); - } - return $attribute; + return HtmlAttributes::buildAttributes($attributes); } private function createOpenTag( @@ -128,7 +136,7 @@ private function createOpenTag( return sprintf( '<%s%s>%s', $tag, - !empty($attributes) ? " " .implode(' ', $attributes) : '', + $attributes ? " " .$attributes : '', $content !== '' ? ($encode ? htmlentities(html_entity_decode($content)) : $content) : '' ) . ($closeTag ? $this->closeTag($tag) : ''); } @@ -136,10 +144,11 @@ private function createOpenTag( private function filterClass(array $classes) : array { $classes = array_unique(array_filter($classes, static fn ($e) => is_string($e) && $e !== '')); + $prefix = $this->prefix; return array_map( - static fn ($e) => str_starts_with($e, 'waterfall-profiler-') + static fn ($e) => str_starts_with($e, $prefix) ? $e - : "waterfall-profiler-$e", + : "$prefix$e", $classes ); } @@ -235,7 +244,7 @@ public function createHtmlStructure(bool $showEmpty = false) : string // just start $html = $this->createOpenTag('div', $this->appendAttributeClass( 'section-wrapper', - ['data-waterfall-profiler-status' => 'closed'] + ["data-{$this->prefix}status" => 'closed'] )); /* ---------------------------------------------------------------- @@ -269,7 +278,12 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'item', [], ( - $this->createOpenTag('span', [], 'Memory', true) + $this->createOpenTag( + 'span', + [], + $this->translate('Memory', context: 'benchmark'), + true + ) . $this->createOpenTag( 'span', [], @@ -281,7 +295,10 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Memory usage' + 'title' => $this->translate( + 'Memory usage', + context: 'benchmark' + ) ] ), Consolidation::sizeFormat($memory_get_usage, 2), @@ -294,7 +311,10 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Real memory usage' + 'title' => $this->translate( + 'Real memory usage', + context: 'benchmark' + ) ] ), Consolidation::sizeFormat($memory_get_real_usage, 2), @@ -307,7 +327,10 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Peak memory usage' + 'title' => $this->translate( + 'Peak memory usage', + context: 'benchmark' + ) ] ), Consolidation::sizeFormat($memory_get_peak_usage, 2), @@ -320,7 +343,10 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Real Peak memory usage' + 'title' => $this->translate( + 'Real Peak memory usage', + context: 'benchmark' + ) ] ), Consolidation::sizeFormat($memory_get_real_peak_usage, 2), @@ -336,7 +362,10 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Benchmark memory usage' + 'title' => $this->translate( + 'Benchmark memory usage', + context: 'benchmark' + ) ] ), Consolidation::sizeFormat($benchmarkMemoryUsage, 2), @@ -363,7 +392,12 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'item', [], ( - $this->createOpenTag('span', [], 'Duration', true) + $this->createOpenTag( + 'span', + [], + $this->translate('Duration', context: 'benchmark'), + true + ) . $this->createOpenTag( 'span', [], @@ -375,7 +409,7 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Total duration' + 'title' => $this->translate('Total duration', context: 'benchmark') ] ), round(((microtime(true) * 1000) - $float), 2) .' ms', @@ -388,7 +422,7 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Benchmark duration' + 'title' => $this->translate('Benchmark duration', context: 'benchmark') ] ), round($profilerDurations, 2) . ' ms', @@ -401,7 +435,7 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'info' ], [ - 'title' => 'Application rendering duration' + 'title' => $this->translate('Application rendering duration', context: 'benchmark') ] ), round($firstRender, 2) . ' ms', @@ -417,7 +451,7 @@ public function createHtmlStructure(bool $showEmpty = false) : string $html .= $this->columnRow( 'item', [ - 'title' => 'Total Benchmark' + 'title' => $this->translate('Total Benchmark', context: 'benchmark') ], ( $this->createOpenTag('span', [], 'Total', true) @@ -444,9 +478,9 @@ public function createHtmlStructure(bool $showEmpty = false) : string ['item', 'active'], [ 'data-target' => 'all', - 'title' => 'Benchmarks' + 'title' => $this->translate('Benchmarks', context: 'benchmark') ], - 'Benchmarks', + $this->translate('Benchmarks', context: 'benchmark'), true ); foreach ($this->getProfiler()->getAggregators() as $key => $aggregator) { @@ -504,7 +538,7 @@ public function createHtmlStructure(bool $showEmpty = false) : string 'input', [ 'type' => 'search', - 'placeholder' => 'Filter', + 'placeholder' => $this->translate('Filter', context: 'benchmark'), 'autocomplete' => 'off', 'name' => 'waterfall-search', 'class' => [ @@ -593,7 +627,7 @@ public function createSection( $this->columnRow( ['item'], [], - 'NO BENCHMARKS', + $this->translate('NO BENCHMARKS', context: 'benchmark'), true ), true, @@ -660,7 +694,7 @@ public function createSection( $info = $this->createOpenTag( 'div', $this->appendAttributeClass( - ['info-section', self::getSeverityHtmlClassName($severity)], + ['info-section', $this->getSeverityHtmlClassName($severity)], ['data-info-id' => $key], ), $info, @@ -730,7 +764,7 @@ public function createSection( $this->appendAttributeClass( [ 'waterfall-bar', - self::getSeverityHtmlClassName($severity) + $this->getSeverityHtmlClassName($severity) ], [ 'style' => sprintf( @@ -771,14 +805,17 @@ public function createSection( ['item', 'item-group'], [ 'title' => !$aggregator ? sprintf( - 'Total Groups: %d', + $this->translate('Total Groups: %d', context: 'benchmark'), $groupCount - ) : sprintf('Group : %s', $aggregator->getGroupName()) + ) : sprintf($this->translate( + 'Group : %s', + context: 'benchmark' + ), $aggregator->getGroupName()) ], $this->createOpenTag( 'span', [], - 'Group', + $this->translate('Group', context: 'benchmark'), true ). ( !$aggregator @@ -797,11 +834,16 @@ public function createSection( ['item', 'item-memory'], [ 'title' => sprintf( - 'Total Memory: %s', + $this->translate('Total Memory: %s', context: 'benchmark'), Consolidation::sizeFormat($memoryUsage, 2) ) ], - $this->createOpenTag('span', [], 'Memory', true), + $this->createOpenTag( + 'span', + [], + $this->translate('Memory', context: 'benchmark'), + true + ), true, false ); @@ -810,11 +852,19 @@ public function createSection( ['item', 'item-duration'], [ 'title' => sprintf( - 'Total Benchmarks Duration: %s', + $this->translate( + 'Total Benchmarks Duration: %s', + context: 'benchmark' + ), round($theDurations, 2) .' ms' ) ], - $this->createOpenTag('span', [], 'Duration', true), + $this->createOpenTag( + 'span', + [], + $this->translate('Duration', context: 'benchmark'), + true + ), true, false ); @@ -822,7 +872,12 @@ public function createSection( $html .= $this->columnRow( ['item', 'item-waterfall'], [], - $this->createOpenTag('span', [], 'Waterfall', true), + $this->createOpenTag( + 'span', + [], + $this->translate('Waterfall', context: 'benchmark'), + true + ), true, false, ); @@ -840,13 +895,16 @@ private function renderJs(string $html) : string $html, JSON_UNESCAPED_SLASHES ); + $scriptId = $this->prefix . 'waterfall-toolbar-script'; + $toolbarId = $this->prefix . 'waterfall-toolbar'; // @codingStandardsIgnoreStart return << +