| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the WindPress package. |
| 5 |
* |
| 6 |
* (c) Joshua Gugun Siagian <suabahasa@gmail.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
declare (strict_types=1); |
| 12 |
namespace WindPress\WindPress\Utils; |
| 13 |
|
| 14 |
use WindPressDeps\Symfony\Component\Stopwatch\Stopwatch; |
| 15 |
use Throwable; |
| 16 |
/** |
| 17 |
* Debug tools for the plugin. |
| 18 |
* |
| 19 |
* @since 3.0.0 |
| 20 |
*/ |
| 21 |
class Debug |
| 22 |
{ |
| 23 |
/** |
| 24 |
* The stopwatch instance. |
| 25 |
*/ |
| 26 |
private static Stopwatch $stopwatch; |
| 27 |
/** |
| 28 |
* Get the stopwatch instance. |
| 29 |
*/ |
| 30 |
public static function stopwatch(): Stopwatch |
| 31 |
{ |
| 32 |
if (!isset(self::$stopwatch)) { |
| 33 |
self::$stopwatch = new Stopwatch(\true); |
| 34 |
} |
| 35 |
return self::$stopwatch; |
| 36 |
} |
| 37 |
public static function shutdown() |
| 38 |
{ |
| 39 |
if (isset(self::$stopwatch)) { |
| 40 |
// self::shutdown_stopwatch(); |
| 41 |
} |
| 42 |
} |
| 43 |
public static function shutdown_stopwatch() |
| 44 |
{ |
| 45 |
$stopwatchEvent = self::stopwatch()->getSectionEvents('__root__'); |
| 46 |
if ($stopwatchEvent === []) { |
| 47 |
return; |
| 48 |
} |
| 49 |
$log = '=== ' . gmdate('Y-m-d H:i:s', time()) . ' ===' . \PHP_EOL; |
| 50 |
foreach ($stopwatchEvent as $ev) { |
| 51 |
$log .= (string) $ev . \PHP_EOL; |
| 52 |
} |
| 53 |
$log .= \PHP_EOL; |
| 54 |
$path = wp_upload_dir()['basedir'] . '/windpress/debug/stopwatch.log'; |
| 55 |
try { |
| 56 |
\WindPress\WindPress\Utils\Common::save_file($log, $path, \FILE_APPEND); |
| 57 |
} catch (Throwable $throwable) { |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|