[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace DebugBar\DataFormatter; 4 5 use DebugBar\DataCollector\AssetProvider; 6 use DebugBar\DataFormatter\VarDumper\DebugBarHtmlDumper; 7 use DebugBar\DataFormatter\VarDumper\SeekingData; 8 use Symfony\Component\VarDumper\Cloner\Data; 9 use Symfony\Component\VarDumper\Cloner\VarCloner; 10 11 /** 12 * Clones and renders variables in HTML format using the Symfony VarDumper component. 13 * 14 * Cloning is decoupled from rendering, so that dumper users can have the fastest possible cloning 15 * performance, while delaying rendering until it is actually needed. 16 */ 17 class DebugBarVarDumper implements AssetProvider 18 { 19 protected static $defaultClonerOptions = array(); 20 21 protected static $defaultDumperOptions = array( 22 'expanded_depth' => 0, 23 'styles' => array( 24 // NOTE: 'default' CSS is also specified in debugbar.css 25 'default' => 'word-wrap: break-word; white-space: pre-wrap; word-break: normal', 26 'num' => 'font-weight:bold; color:#1299DA', 27 'const' => 'font-weight:bold', 28 'str' => 'font-weight:bold; color:#3A9B26', 29 'note' => 'color:#1299DA', 30 'ref' => 'color:#7B7B7B', 31 'public' => 'color:#000000', 32 'protected' => 'color:#000000', 33 'private' => 'color:#000000', 34 'meta' => 'color:#B729D9', 35 'key' => 'color:#3A9B26', 36 'index' => 'color:#1299DA', 37 'ellipsis' => 'color:#A0A000', 38 ), 39 ); 40 41 protected $clonerOptions; 42 43 protected $dumperOptions; 44 45 /** @var VarCloner */ 46 protected $cloner; 47 48 /** @var DebugBarHtmlDumper */ 49 protected $dumper; 50 51 /** 52 * Gets the VarCloner instance with configuration options set. 53 * 54 * @return VarCloner 55 */ 56 protected function getCloner() 57 { 58 if (!$this->cloner) { 59 $clonerOptions = $this->getClonerOptions(); 60 if (isset($clonerOptions['casters'])) { 61 $this->cloner = new VarCloner($clonerOptions['casters']); 62 } else { 63 $this->cloner = new VarCloner(); 64 } 65 if (isset($clonerOptions['additional_casters'])) { 66 $this->cloner->addCasters($clonerOptions['additional_casters']); 67 } 68 if (isset($clonerOptions['max_items'])) { 69 $this->cloner->setMaxItems($clonerOptions['max_items']); 70 } 71 if (isset($clonerOptions['max_string'])) { 72 $this->cloner->setMaxString($clonerOptions['max_string']); 73 } 74 // setMinDepth was added to Symfony 3.4: 75 if (isset($clonerOptions['min_depth']) && method_exists($this->cloner, 'setMinDepth')) { 76 $this->cloner->setMinDepth($clonerOptions['min_depth']); 77 } 78 } 79 return $this->cloner; 80 } 81 82 /** 83 * Gets the DebugBarHtmlDumper instance with configuration options set. 84 * 85 * @return DebugBarHtmlDumper 86 */ 87 protected function getDumper() 88 { 89 if (!$this->dumper) { 90 $this->dumper = new DebugBarHtmlDumper(); 91 $dumperOptions = $this->getDumperOptions(); 92 if (isset($dumperOptions['styles'])) { 93 $this->dumper->setStyles($dumperOptions['styles']); 94 } 95 } 96 return $this->dumper; 97 } 98 99 /** 100 * Gets the array of non-default VarCloner configuration options. 101 * 102 * @return array 103 */ 104 public function getClonerOptions() 105 { 106 if ($this->clonerOptions === null) { 107 $this->clonerOptions = self::$defaultClonerOptions; 108 } 109 return $this->clonerOptions; 110 } 111 112 /** 113 * Merges an array of non-default VarCloner configuration options with the existing non-default 114 * options. 115 * 116 * Configuration options are: 117 * - casters: a map of VarDumper Caster objects to use instead of the default casters. 118 * - additional_casters: a map of VarDumper Caster objects to use in addition to the default 119 * casters. 120 * - max_items: maximum number of items to clone beyond the minimum depth. 121 * - max_string: maximum string size 122 * - min_depth: minimum tree depth to clone before counting items against the max_items limit. 123 * (Requires Symfony 3.4; ignored on older versions.) 124 * 125 * @param array $options 126 */ 127 public function mergeClonerOptions($options) 128 { 129 $this->clonerOptions = $options + $this->getClonerOptions(); 130 $this->cloner = null; 131 } 132 133 /** 134 * Resets the array of non-default VarCloner configuration options without retaining any of the 135 * existing non-default options. 136 * 137 * Configuration options are: 138 * - casters: a map of VarDumper Caster objects to use instead of the default casters. 139 * - additional_casters: a map of VarDumper Caster objects to use in addition to the default 140 * casters. 141 * - max_items: maximum number of items to clone beyond the minimum depth. 142 * - max_string: maximum string size 143 * - min_depth: minimum tree depth to clone before counting items against the max_items limit. 144 * (Requires Symfony 3.4; ignored on older versions.) 145 * 146 * @param array $options 147 */ 148 public function resetClonerOptions($options = null) 149 { 150 $this->clonerOptions = ($options ?: array()) + self::$defaultClonerOptions; 151 $this->cloner = null; 152 } 153 154 /** 155 * Gets the array of non-default HtmlDumper configuration options. 156 * 157 * @return array 158 */ 159 public function getDumperOptions() 160 { 161 if ($this->dumperOptions === null) { 162 $this->dumperOptions = self::$defaultDumperOptions; 163 } 164 return $this->dumperOptions; 165 } 166 167 /** 168 * Merges an array of non-default HtmlDumper configuration options with the existing non-default 169 * options. 170 * 171 * Configuration options are: 172 * - styles: a map of CSS styles to include in the assets, as documented in 173 * HtmlDumper::setStyles. 174 * - expanded_depth: the tree depth to initially expand. 175 * (Requires Symfony 3.2; ignored on older versions.) 176 * - max_string: maximum string size. 177 * (Requires Symfony 3.2; ignored on older versions.) 178 * - file_link_format: link format for files; %f expanded to file and %l expanded to line 179 * (Requires Symfony 3.2; ignored on older versions.) 180 * 181 * @param array $options 182 */ 183 public function mergeDumperOptions($options) 184 { 185 $this->dumperOptions = $options + $this->getDumperOptions(); 186 $this->dumper = null; 187 } 188 189 /** 190 * Resets the array of non-default HtmlDumper configuration options without retaining any of the 191 * existing non-default options. 192 * 193 * Configuration options are: 194 * - styles: a map of CSS styles to include in the assets, as documented in 195 * HtmlDumper::setStyles. 196 * - expanded_depth: the tree depth to initially expand. 197 * (Requires Symfony 3.2; ignored on older versions.) 198 * - max_string: maximum string size. 199 * (Requires Symfony 3.2; ignored on older versions.) 200 * - file_link_format: link format for files; %f expanded to file and %l expanded to line 201 * (Requires Symfony 3.2; ignored on older versions.) 202 * 203 * @param array $options 204 */ 205 public function resetDumperOptions($options = null) 206 { 207 $this->dumperOptions = ($options ?: array()) + self::$defaultDumperOptions; 208 $this->dumper = null; 209 } 210 211 /** 212 * Captures the data from a variable and serializes it for later rendering. 213 * 214 * @param mixed $data The variable to capture. 215 * @return string Serialized variable data. 216 */ 217 public function captureVar($data) 218 { 219 return serialize($this->getCloner()->cloneVar($data)); 220 } 221 222 /** 223 * Gets the display options for the HTML dumper. 224 * 225 * @return array 226 */ 227 protected function getDisplayOptions() 228 { 229 $displayOptions = array(); 230 $dumperOptions = $this->getDumperOptions(); 231 // Only used by Symfony 3.2 and newer: 232 if (isset($dumperOptions['expanded_depth'])) { 233 $displayOptions['maxDepth'] = $dumperOptions['expanded_depth']; 234 } 235 // Only used by Symfony 3.2 and newer: 236 if (isset($dumperOptions['max_string'])) { 237 $displayOptions['maxStringLength'] = $dumperOptions['max_string']; 238 } 239 // Only used by Symfony 3.2 and newer: 240 if (isset($dumperOptions['file_link_format'])) { 241 $displayOptions['fileLinkFormat'] = $dumperOptions['file_link_format']; 242 } 243 return $displayOptions; 244 } 245 246 /** 247 * Renders previously-captured data from captureVar to HTML and returns it as a string. 248 * 249 * @param string $capturedData Captured data from captureVar. 250 * @param array $seekPath Pass an array of keys to traverse if you only want to render a subset 251 * of the data. 252 * @return string HTML rendering of the variable. 253 */ 254 public function renderCapturedVar($capturedData, $seekPath = array()) 255 { 256 $data = unserialize($capturedData); 257 // The seek method was added in Symfony 3.2; emulate the behavior via SeekingData for older 258 // Symfony versions. 259 if (!method_exists($data, 'seek')) { 260 $data = new SeekingData($data->getRawData()); 261 } 262 263 foreach ($seekPath as $key) { 264 $data = $data->seek($key); 265 } 266 267 return $this->dump($data); 268 } 269 270 /** 271 * Captures and renders the data from a variable to HTML and returns it as a string. 272 * 273 * @param mixed $data The variable to capture and render. 274 * @return string HTML rendering of the variable. 275 */ 276 public function renderVar($data) 277 { 278 return $this->dump($this->getCloner()->cloneVar($data)); 279 } 280 281 /** 282 * Returns assets required for rendering variables. 283 * 284 * @return array 285 */ 286 public function getAssets() { 287 $dumper = $this->getDumper(); 288 $dumper->resetDumpHeader(); // this will cause the default dump header to regenerate 289 return array( 290 'inline_head' => array( 291 'html_var_dumper' => $dumper->getDumpHeaderByDebugBar(), 292 ), 293 ); 294 } 295 296 /** 297 * Helper function to dump a Data object to HTML. 298 * 299 * @param Data $data 300 * @return string 301 */ 302 protected function dump(Data $data) 303 { 304 $dumper = $this->getDumper(); 305 $output = fopen('php://memory', 'r+b'); 306 $dumper->setOutput($output); 307 $dumper->setDumpHeader(''); // we don't actually want a dump header 308 // NOTE: Symfony 3.2 added the third $extraDisplayOptions parameter. Older versions will 309 // safely ignore it. 310 $dumper->dump($data, null, $this->getDisplayOptions()); 311 $result = stream_get_contents($output, -1, 0); 312 fclose($output); 313 return $result; 314 } 315 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |