Skip to content

Commit cfcdce5

Browse files
Refactored VarDumper api.
1 parent 27114e1 commit cfcdce5

File tree

3 files changed

+138
-112
lines changed

3 files changed

+138
-112
lines changed

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
<properties>
4848
<property name="prefixes" type="array">
4949
<element value="KnowTheCode\DebugToolkit"/>
50+
<element value="debugtoolkit_"/>
51+
<element value="_genesis"/>
5052
</property>
5153
</properties>
5254
</rule>

src/class-vardumper.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/**
3+
* VarDumper Helpers - a collection of helper functionality.
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+
namespace KnowTheCode\DebugToolkit;
13+
14+
use Closure;
15+
use InvalidArgumentException;
16+
use Symfony\Component\VarDumper\Cloner\VarCloner;
17+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
18+
use Symfony\Component\VarDumper\VarDumper;
19+
20+
/**
21+
* Class VarDumper_Helpers
22+
* @package KnowTheCode\DebugToolkit
23+
*/
24+
class VarDumper_Helpers {
25+
26+
/**
27+
* Array of VarDumper configuration parameters.
28+
*
29+
* @var array
30+
*/
31+
private static $config;
32+
33+
/**
34+
* VarDumper handler.
35+
*
36+
* @var Closure
37+
*/
38+
protected static $handler;
39+
40+
/**
41+
* Run the var dumper.
42+
*
43+
* @since 1.0.0
44+
* @access private
45+
*
46+
* @param mixed $var Variable to dump.
47+
*/
48+
public static function dump( $var ) {
49+
static::set_styles();
50+
VarDumper::dump( $var );
51+
}
52+
53+
/**
54+
* Run the var dumper.
55+
*
56+
* @since 1.0.0
57+
* @access private
58+
*
59+
* @param mixed $var Variable to dump.
60+
*/
61+
public static function dump_and_die( $var ) {
62+
static::dump( $var );
63+
die();
64+
}
65+
66+
/**
67+
* Sets the VarDumper HTML Dumper's styles.
68+
*
69+
* @since 1.0.0
70+
*
71+
* @return null bails out when cli or handler is already set.
72+
*/
73+
private static function set_styles() {
74+
// Bail out.
75+
if ( in_array( PHP_SAPI, [ 'cli', 'phpdbg' ], true ) ) {
76+
return;
77+
}
78+
79+
if ( null !== static::$handler ) {
80+
return;
81+
}
82+
83+
$cloner = new VarCloner();
84+
$dumper = new HtmlDumper();
85+
86+
$dumper->setStyles( static::get_config( 'styles' ) );
87+
static::$handler = function( $var ) use ( $cloner, $dumper ) {
88+
$dumper->dump( $cloner->cloneVar( $var ) );
89+
};
90+
VarDumper::setHandler( static::$handler );
91+
}
92+
93+
/**
94+
* Gets the VarDumper's configuration or a specific configuration parameter.
95+
*
96+
* @since 1.0.0
97+
*
98+
* @param string $key Optional. Parameter's key to return.
99+
*
100+
* @return mixed
101+
* @throw InvalidArgumentException If the key does not exist, an error is thrown.
102+
*/
103+
private static function get_config( $key = '' ) {
104+
if ( empty( static::$config ) ) {
105+
/**
106+
* Filter the VarDumper configuration parameters.
107+
*
108+
* @since 1.0.0
109+
*
110+
* @param array Array of configuration parameters for the VarDump component.
111+
*/
112+
static::$config = apply_filters( 'debugtoolkit_set_html_dumper_config', (array) require _get_plugin_root_dir() . '/config/var-dumper.php' );
113+
}
114+
115+
if ( empty( $key ) ) {
116+
return static::$config;
117+
}
118+
119+
if ( ! array_key_exists( $key, static::$config ) ) {
120+
$message = __( 'The key [$s] does not exist in the config:', 'devtoolkit' );
121+
$message = sprintf( $message, $key );
122+
throw new InvalidArgumentException(
123+
esc_html( $message . ': ' ) . print_r( static::$config, true )
124+
);
125+
}
126+
127+
return static::$config[ $key ];
128+
}
129+
}

src/vardumper-functions.php

Lines changed: 7 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
* @license GNU-2.0+
1616
*/
1717

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;
18+
use KnowTheCode\DebugToolkit\VarDumper_Helpers;
19+
20+
require_once __DIR__ . '/class-vardumper.php';
2221

2322
if ( ! function_exists( 'vdump' ) ) {
2423
/**
@@ -27,11 +26,9 @@
2726
* @since 1.0.0
2827
*
2928
* @param mixed $var Variable to dump.
30-
*
31-
* @throws InvalidArgumentException If no arguments are passed, throws an error.
3229
*/
3330
function vdump( $var ) {
34-
_dump_var( $var, __FUNCTION__ );
31+
VarDumper_Helpers::dump( $var );
3532
}
3633
}
3734

@@ -42,11 +39,9 @@ function vdump( $var ) {
4239
* @since 1.0.0
4340
*
4441
* @param mixed $var Variable to dump.
45-
*
46-
* @throws InvalidArgumentException If no arguments are passed, throws an error.
4742
*/
4843
function vd( $var ) {
49-
_dump_var( $var, __FUNCTION__ );
44+
VarDumper_Helpers::dump( $var, __FUNCTION__ );
5045
}
5146
}
5247

@@ -57,12 +52,9 @@ function vd( $var ) {
5752
* @since 1.0.0
5853
*
5954
* @param mixed $var Variable to dump.
60-
*
61-
* @throws InvalidArgumentException If no arguments are passed, throws an error.
6255
*/
6356
function vdd( $var ) {
64-
_dump_var( $var, __FUNCTION__ );
65-
die();
57+
VarDumper_Helpers::dump_and_die( $var );
6658
}
6759
}
6860

@@ -73,105 +65,8 @@ function vdd( $var ) {
7365
* @since 1.0.0
7466
*
7567
* @param mixed $var Variable to dump.
76-
*
77-
* @throws InvalidArgumentException If no arguments are passed, throws an error.
7868
*/
7969
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;
70+
VarDumper_Helpers::dump_and_die( $var );
12671
}
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 ];
17772
}

0 commit comments

Comments
 (0)