Skip to content

Commit a014a8d

Browse files
committed
Add debug functions
1 parent a65a165 commit a014a8d

File tree

6 files changed

+435
-1
lines changed

6 files changed

+435
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Fabian Wiget
3+
Copyright (c) 2003-2018 Fabian Wiget
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "fabacino/php-debug-functions",
3+
"description": "Debug functions for PHP",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Fabian Wiget",
9+
"email": "fabacino@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"monolog/monolog": "^1.23"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Fabacino\\Debug\\": "src"
18+
},
19+
"files": [
20+
"src/debug-functions.php"
21+
]
22+
}
23+
}

composer.lock

Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Debug.php

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
3+
/**
4+
* Settings for use in debug functions.
5+
*
6+
* @copyright Fabian Wiget <fabacino@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+
12+
namespace Fabacino\Debug;
13+
14+
use Monolog\Logger;
15+
use Monolog\Formatter\LineFormatter;
16+
use Monolog\Handler\NullHandler;
17+
use Monolog\Handler\StreamHandler;
18+
19+
class Debug
20+
{
21+
/**
22+
* Available flags for tweaking the output.
23+
*/
24+
const USE_VARDUMP = 1;
25+
const USE_HTMLENTITIES = 2;
26+
27+
/**
28+
* Default values for Monolog options.
29+
*/
30+
private const CHANNEL_NAME = 'fabacino/debug';
31+
private const OUTPUT_FORMAT = "%datetime%: %message%\n";
32+
private const DATE_FORMAT = null;
33+
34+
/**
35+
* Default flags for tweaking the output.
36+
*
37+
* @var int
38+
*/
39+
private $defaultFlags;
40+
41+
/**
42+
* Monolog logger.
43+
*
44+
* @var Logger
45+
*/
46+
private $Logger;
47+
48+
/**
49+
* Singleton instance.
50+
*
51+
* @var Debug
52+
*/
53+
private static $Instance;
54+
55+
/**
56+
* Constructor.
57+
*
58+
* @param int $defaultFlags Default flags for tweaking the output.
59+
* @param Logger $Logger The monolog logger.
60+
*
61+
* @return void
62+
*/
63+
private function __construct(int $defaultFlags, Logger $Logger)
64+
{
65+
$this->defaultFlags = $defaultFlags;
66+
$this->Logger = $Logger;
67+
}
68+
69+
/**
70+
* Get singleton instance.
71+
*
72+
* @return Debug
73+
*/
74+
public static function getInstance(): Debug
75+
{
76+
if (self::$Instance === null) {
77+
static::init(0, self::initLogger());
78+
}
79+
return self::$Instance;
80+
}
81+
82+
/**
83+
* Initialize singleton instance.
84+
*
85+
* The following settings are available:
86+
*
87+
* - use_vardump: Use vardump for debug output? (boolean, default = false)
88+
* - use_htmlentities: Use htmlentities for debug output? (boolean, default = false)
89+
* - log_file: The log file. (string, default = null)
90+
* - monolog: Settings for Monolog logger. (array, default = [])
91+
* - channel_name: The name of the channel. (string, default = self::CHANNEL_NAME)
92+
* - output_format: The output format. (string, default = self::OUTPUT_FORMAT)
93+
* - date_format: The date format. (string, default = self::DATE_FORMAT)
94+
*
95+
* The settings `log_file` and `monolog` are only used when calling `logValue()`.
96+
*
97+
* @param array $settings Settings.
98+
*
99+
* @return void
100+
*/
101+
public static function init(array $settings = []): void
102+
{
103+
$defaultFlags = 0;
104+
if (isset($settings['use_vardump']) && $settings['use_vardump']) {
105+
$defaultFlags |= self::USE_VARDUMP;
106+
}
107+
if (isset($settings['use_htmlentities']) && $settings['use_htmlentities']) {
108+
$defaultFlags |= self::USE_HTMLENTITIES;
109+
}
110+
111+
self::$Instance = new static($defaultFlags, self::initLogger($settings));
112+
}
113+
114+
/**
115+
* Create Monolog logger.
116+
*
117+
* @param array $settings Settings.
118+
*
119+
* @return Logger
120+
* @see init()
121+
*/
122+
private static function initLogger(array $settings = []): Logger
123+
{
124+
if (isset($settings['monolog']) && is_array($settings['monolog'])) {
125+
$monologSettings = $settings['monolog'];
126+
} else {
127+
$monologSettings = [];
128+
}
129+
130+
if (isset($settings['log_file'])) {
131+
// Use default values if not specified otherwise.
132+
if (array_key_exists('output_format', $monologSettings)) {
133+
$output = $monologSettings['output_format'];
134+
} else {
135+
$output = self::OUTPUT_FORMAT;
136+
}
137+
$dateFormat = $monologSettings['date_format'] ?? self::DATE_FORMAT;
138+
139+
$Stream = new StreamHandler($settings['log_file'], Logger::DEBUG);
140+
$Stream->setFormatter(new LineFormatter($output, $dateFormat, true));
141+
} else {
142+
// We didn't get any file to log.
143+
$Stream = new NullHandler(Logger::DEBUG);
144+
}
145+
146+
$Logger = new Logger($monologSettings['channel_name'] ?? self::CHANNEL_NAME);
147+
$Logger->pushHandler($Stream);
148+
return $Logger;
149+
}
150+
151+
/**
152+
* Print debug value
153+
*
154+
* @param mixed $var The variable to analyse.
155+
* @param int|null $flags Flags for tweaking the output.
156+
*
157+
* @return void
158+
*/
159+
public function printValue($var, int $flags = null): void
160+
{
161+
if ($flags === null) {
162+
$flags = $this->defaultFlags;
163+
}
164+
165+
$output = static::debugValue($var, $flags);
166+
if (PHP_SAPI !== 'cli') {
167+
$output = "<pre>{$output}</pre>";
168+
}
169+
echo $output;
170+
}
171+
172+
/**
173+
* Return debug value.
174+
*
175+
* @param mixed $var The variable to analyse.
176+
* @param int|null $flags Flags for tweaking the output.
177+
*
178+
* @return mixed
179+
*/
180+
public function debugValue($var, int $flags = null)
181+
{
182+
if ($flags === null) {
183+
$flags = $this->defaultFlags;
184+
}
185+
186+
ob_start();
187+
if (($flags & self::USE_VARDUMP) === self::USE_VARDUMP) {
188+
var_dump($var);
189+
} elseif (is_array($var) || is_object($var)) {
190+
print_r($var);
191+
} else {
192+
echo $var;
193+
}
194+
$output = ob_get_contents();
195+
ob_end_clean();
196+
197+
if (($flags & self::USE_HTMLENTITIES) === self::USE_HTMLENTITIES) {
198+
$output = htmlentities($output, ENT_NOQUOTES);
199+
}
200+
201+
return $output;
202+
}
203+
204+
/**
205+
* Log debug value.
206+
*
207+
* @param mixed $var The variable to analyse.
208+
* @param int|null $flags Flags for tweaking the output.
209+
*
210+
* @return mixed
211+
*/
212+
public function logValue($var, int $flags = null): void
213+
{
214+
$this->Logger->addDebug(static::debugValue($var, $flags));
215+
}
216+
}

0 commit comments

Comments
 (0)