Skip to content

Commit 8f45068

Browse files
Added VarDumper functions.
Functions: vdump(), vd(), vdd(), vddd() Includes styling option for the HTML Dumper.
1 parent 008d73a commit 8f45068

File tree

5 files changed

+220
-5
lines changed

5 files changed

+220
-5
lines changed

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
"config": {
2424
"sort-order": true
2525
},
26+
"autoload": {
27+
"files": [
28+
"src/functions.php",
29+
"src/class-admin-bar.php"
30+
],
31+
"psr-4": {
32+
"KnowTheCode\\DebugToolkit\\": "src"
33+
},
34+
"exclude-from-classmap": [
35+
"/tests/"
36+
]
37+
},
2638
"autoload-dev": {
2739
"psr-4": {
2840
"KnowTheCode\\DebugToolkit\\Tests\\": "tests/"

composer.lock

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

config/var-dumper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* VarDumper Runtime Configuration Parameters.
4+
*
5+
* @package KnowTheCode\DebugToolkit
6+
* @since 1.0.0
7+
* @author hellofromTonya
8+
* @link https://github.com/KnowTheCode/debug-toolkit
9+
* @license GNU-2.0+
10+
*/
11+
12+
return [
13+
'styles' => [
14+
'default' => 'background-color:#1c202c; color:#f2f2f2; font-size:18px; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: break-all;',
15+
'num' => 'font-weight:bold; color:#a8def7',
16+
'note' => 'color:#a8def7',
17+
'index' => 'color:#a8def7',
18+
'str' => 'font-weight:bold; color:#6FDD16',
19+
'key' => 'color:#6FDD16',
20+
'meta' => 'color:#E4EC42',
21+
'public' => 'color:#E4EC42',
22+
'protected' => 'color:#E4EC42',
23+
'private' => 'color:#E4EC42',
24+
],
25+
];

debug-toolkit.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,23 @@ function render_php_version_admin_notice() {
4444
* Gets the plugin's root directory.
4545
*
4646
* @since 1.0.0
47+
* @access private
4748
*
4849
* @return string
4950
*/
5051
function _get_plugin_root_dir() {
5152
return __DIR__;
5253
}
5354

55+
// Load the files and Composer.
56+
require_once __DIR__ . '/vendor/autoload.php';
57+
5458
/**
5559
* Load the Admin Bar.
5660
*
5761
* @since 1.0.0
5862
*/
5963
function load_admin_bar() {
60-
require_once __DIR__ . '/src/class-admin-bar.php';
61-
6264
/**
6365
* Filter the admin bar configuration parameters.
6466
*
@@ -71,5 +73,4 @@ function load_admin_bar() {
7173
( new Admin_Bar( $config ) )->init();
7274
}
7375

74-
require_once __DIR__ . '/vendor/autoload.php';
7576
load_admin_bar();

src/vardumper-functions.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
/**
3+
* VarDumper Functions - used in your code:
4+
* vdump()
5+
* vd()
6+
* vdd()
7+
* vddd()
8+
*
9+
* Note: The `v` prefix separates it from the Kint version.
10+
*
11+
* @package KnowTheCode\DebugToolkit
12+
* @since 1.0.0
13+
* @author hellofromTonya
14+
* @link https://github.com/KnowTheCode/debug-toolkit
15+
* @license GNU-2.0+
16+
*/
17+
18+
use Symfony\Component\VarDumper\Cloner\VarCloner;
19+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
20+
use Symfony\Component\VarDumper\VarDumper;
21+
use function KnowTheCode\DebugToolkit\_get_plugin_root_dir;
22+
23+
if ( ! function_exists( 'vdump' ) ) {
24+
/**
25+
* Dumps the given variable(s).
26+
*
27+
* @since 1.0.0
28+
*
29+
* @param mixed $var Variable to dump.
30+
*
31+
* @throws InvalidArgumentException If no arguments are passed, throws an error.
32+
*/
33+
function vdump( $var ) {
34+
_dump_var( $var, __FUNCTION__ );
35+
}
36+
}
37+
38+
if ( ! function_exists( 'vd' ) ) {
39+
/**
40+
* Dumps the given variable(s).
41+
*
42+
* @since 1.0.0
43+
*
44+
* @param mixed $var Variable to dump.
45+
*
46+
* @throws InvalidArgumentException If no arguments are passed, throws an error.
47+
*/
48+
function vd( $var ) {
49+
_dump_var( $var, __FUNCTION__ );
50+
}
51+
}
52+
53+
if ( ! function_exists( 'vdd' ) ) {
54+
/**
55+
* Dumps the given variable(s) and then ends the execution of the program.
56+
*
57+
* @since 1.0.0
58+
*
59+
* @param mixed $var Variable to dump.
60+
*
61+
* @throws InvalidArgumentException If no arguments are passed, throws an error.
62+
*/
63+
function vdd( $var ) {
64+
_dump_var( $var, __FUNCTION__ );
65+
die();
66+
}
67+
}
68+
69+
if ( ! function_exists( 'vddd' ) ) {
70+
/**
71+
* Dumps the given variable(s) and then ends the execution of the program.
72+
*
73+
* @since 1.0.0
74+
*
75+
* @param mixed $var Variable to dump.
76+
*
77+
* @throws InvalidArgumentException If no arguments are passed, throws an error.
78+
*/
79+
function vddd( $var ) {
80+
_dump_var( $var, __FUNCTION__ );
81+
die();
82+
}
83+
}
84+
85+
/**
86+
* Run the var dumper.
87+
*
88+
* @since 1.0.0
89+
* @access private
90+
*
91+
* @param mixed $var Variable to dump.
92+
* @param string $func_name Name of the calling function.
93+
*
94+
* @throws InvalidArgumentException If no arguments are passed, throws an error.
95+
*/
96+
function _dump_var( $var, $func_name ) {
97+
98+
if ( empty( $var ) ) {
99+
$message = __( 'No variable passed to the VarDumper function', 'devtoolkit' );
100+
throw new InvalidArgumentException( esc_html( "{$message} {$func_name}" ) );
101+
die();
102+
}
103+
104+
_set_html_dumper_styles();
105+
VarDumper::dump( $var );
106+
}
107+
108+
/**
109+
* Sets the VarDumper HTML Dumper's styles.
110+
*
111+
* @since 1.0.0
112+
* @access private
113+
*
114+
* @return null bails out when cli or handler is already set.
115+
*/
116+
function _set_html_dumper_styles() {
117+
static $handler = null;
118+
119+
// Bail out.
120+
if ( in_array( PHP_SAPI, [ 'cli', 'phpdbg' ], true ) ) {
121+
return;
122+
}
123+
124+
if ( null !== $handler ) {
125+
return;
126+
}
127+
128+
$cloner = new VarCloner();
129+
$dumper = new HtmlDumper();
130+
131+
$dumper->setStyles( _get_vardumper_config( 'styles' ) );
132+
$handler = function( $var ) use ( $cloner, $dumper ) {
133+
$dumper->dump( $cloner->cloneVar( $var ) );
134+
};
135+
136+
VarDumper::setHandler( $handler );
137+
}
138+
139+
/**
140+
* Gets the VarDumper's configuration or a specific configuration parameter.
141+
*
142+
* @since 1.0.0
143+
* @access private
144+
*
145+
* @param string $key Optional. Parameter's key to return.
146+
*
147+
* @return mixed
148+
* @throw InvalidArgumentException If the key does not exist, an error is thrown.
149+
*/
150+
function _get_vardumper_config( $key = '' ) {
151+
static $config = null;
152+
153+
if ( empty( $config ) ) {
154+
/**
155+
* Filter the VarDumper configuration parameters.
156+
*
157+
* @since 1.0.0
158+
*
159+
* @param array Array of configuration parameters for the VarDump component.
160+
*/
161+
$config = apply_filters( 'set_html_dumper_config', (array) require _get_plugin_root_dir() . '/config/var-dumper.php' );
162+
}
163+
164+
if ( empty( $key ) ) {
165+
return $config;
166+
}
167+
168+
if ( ! array_key_exists( $key, $config ) ) {
169+
$message = __( 'The key [$s] does not exist in the config:', 'devtoolkit' );
170+
$message = sprintf( $message, $key );
171+
throw new InvalidArgumentException(
172+
esc_html( $message . ': ' ) . print_r( $config, true )
173+
);
174+
}
175+
176+
return $config[ $key ];
177+
}

0 commit comments

Comments
 (0)