3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * Adds profiler output to the HTTP response.
24 * The least sophisticated profiler output class possible, view source! :)
29 class ProfilerOutputText
extends ProfilerOutput
{
30 /** @var float Min real time display threshold */
33 /** @var bool Whether to use visible text or a comment (for HTML responses) */
36 public function __construct( Profiler
$collector, array $params ) {
37 parent
::__construct( $collector, $params );
38 $this->thresholdMs
= $params['thresholdMs'] ??
1.0;
39 $this->visible
= $params['visible'] ??
false;
42 public function logsToOutput() {
46 public function log( array $stats ) {
49 // Filter out really tiny entries
50 $min = $this->thresholdMs
;
51 $stats = array_filter( $stats, static function ( $a ) use ( $min ) {
52 return $a['real'] > $min;
54 // Sort descending by time elapsed
55 usort( $stats, static function ( $a, $b ) {
56 return $b['real'] <=> $a['real'];
60 static function ( $item ) use ( &$out ) {
61 $out .= sprintf( "%6.2f%% %3.3f %6d - %s\n",
62 $item['%real'], $item['real'], $item['calls'], $item['name'] );
66 $contentType = $this->collector
->getContentType();
68 print $this->formatHtmlComment( $out ) . "\n";
69 } elseif ( $contentType === 'text/html' ) {
70 if ( $this->visible
) {
71 print '<pre>' . htmlspecialchars( $out ) . '</pre>';
73 print $this->formatHtmlComment( $out ) . "\n";
75 } elseif ( $contentType === 'text/javascript' ||
$contentType === 'text/css' ) {
76 print "\n" . $this->formatBlockComment( $out ) . "\n";
80 private function formatHtmlComment( string $text ): string {
81 // Escape any closing "-->"
82 return "<!--\n" . htmlspecialchars( $text ) . "\n-->";
85 private function formatBlockComment( string $text ): string {
86 // Escape any closing "*/"
87 $encText = str_replace( '*/', '* /', $text );
88 return "/*\n$encText\n*/";