|
| 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