PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / lib / src / Debug / Debugger.php

Debugger.php in JCH Optimize trunk, at lib/src/Debug/Debugger.php

152 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/core
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2026 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\Core\Debug;
15
16 use _JchOptimizeVendor\V91\Psr\Container\ContainerInterface;
17 use _JchOptimizeVendor\V91\Psr\Log\LoggerInterface;
18 use JchOptimize\Container\ContainerFactory;
19 use JchOptimize\Core\Platform\PathsInterface;
20
21 use function defined;
22 use function error_get_last;
23 use function ob_get_clean;
24 use function register_shutdown_function;
25 use function set_error_handler;
26 use function var_dump;
27
28 use const E_ALL;
29
30 defined('_JCH_EXEC') or die('Restricted access');
31
32 abstract class Debugger
33 {
34 private static ContainerInterface $container;
35 private static bool $dieOnError = false;
36
37 public static function printr(mixed $var, ?string $label = null, bool $condition = true): void
38 {
39 if ($condition) {
40 self::debug('printr', $var, $label);
41 }
42 }
43
44 private static function debug(string $method, mixed $var, ?string $label = null): void
45 {
46 if (!isset(self::$container) || !self::$container->has(LoggerInterface::class)) {
47 return;
48 }
49
50 /** @var LoggerInterface $logger */
51 /** @noinspection PhpUnhandledExceptionInspection */
52 $logger = self::$container->get(LoggerInterface::class);
53
54 if (is_null($label)) {
55 $name = '';
56 } else {
57 $name = $label . ': ';
58 }
59
60 switch ($method) {
61 case 'vdump':
62 ob_start();
63 /** @psalm-suppress ForbiddenCode */
64 var_dump($var);
65 $logger->debug($name . ob_get_clean());
66
67 break;
68 case 'printr':
69 default:
70 $logger->debug($name . print_r($var, true));
71
72 break;
73 }
74 }
75
76 public static function vdump(mixed $var, ?string $label = null, bool $condition = true): void
77 {
78 if ($condition) {
79 self::debug('vdump', $var, $label);
80 }
81 }
82
83 public static function attachErrorHandler(bool $dieOnError = false): void
84 {
85 self::$dieOnError = $dieOnError;
86
87 set_error_handler([Debugger::class, 'debuggerErrorHandler'], E_ALL);
88 register_shutdown_function([Debugger::class, 'debuggerCatchLastErrors']);
89 }
90
91 public static function debuggerErrorHandler(
92 int $errno,
93 string $errstr,
94 string $errfile = '',
95 int $errline = 0
96 ): bool {
97 if (!isset(self::$container) || !self::$container->has(LoggerInterface::class)) {
98 return false;
99 }
100
101 /** @var LoggerInterface $logger */
102 /** @noinspection PhpUnhandledExceptionInspection */
103 $logger = self::$container->get(LoggerInterface::class);
104
105 $msg = 'Error no: ' . $errno
106 . ', Message: ' . $errstr
107 . ' in file: ' . $errfile
108 . ' at line: ' . $errline . "\n";
109
110 $logger->error($msg);
111
112 return !self::$dieOnError;
113 }
114
115 public static function debuggerCatchLastErrors(): void
116 {
117 if (!isset(self::$container) || !self::$container->has(LoggerInterface::class)) {
118 return;
119 }
120
121 $error = error_get_last();
122
123 if ($error !== null) {
124 $msg = 'Error type: ' . $error['type']
125 . ', Message: ' . $error['message']
126 . ' in file: ' . $error['file']
127 . ' at line: ' . $error['line'] . "\n";
128
129 /** @var LoggerInterface $logger */
130 /** @noinspection PhpUnhandledExceptionInspection */
131 $logger = self::$container->get(LoggerInterface::class);
132 $logger->error($msg);
133 }
134 }
135
136 public static function setErrorLogging(int $error_level = E_ALL): void
137 {
138 error_reporting($error_level);
139 @ini_set('log_errors', 'On');
140 @ini_set(
141 'error_log',
142 ContainerFactory::getInstance()->get(PathsInterface::class)->getLogsPath() . '/php-errors.log'
143 );
144 @ini_set('display_errors', 'Off');
145 }
146
147 public static function setContainer(ContainerInterface $container): void
148 {
149 self::$container = $container;
150 }
151 }
152