@@ -17,8 +17,8 @@ Installation
17
17
18
18
You can install the component in 2 different ways:
19
19
20
- * :doc: `Install it via Composer </components/using_components >` (``symfony/var-dumper `` on `Packagist `_);
21
- * Use the official Git repository (https://github.com/symfony/VarDumper ).
20
+ - :doc: `Install it via Composer </components/using_components >` (``symfony/var-dumper `` on `Packagist `_);
21
+ - Use the official Git repository (https://github.com/symfony/var-dumper ).
22
22
23
23
The dump() function
24
24
-------------------
@@ -29,19 +29,20 @@ on the current PHP SAPI.
29
29
30
30
The advantages of this function are:
31
31
32
- - per object and resource types specialized view to e.g. filter out
33
- Doctrine internals while dumping a single proxy entity, or get more
34
- insight on opened files with :phpfunction: `stream_get_meta_data() `.
35
- - configurable output formats: HTML or colored command line output.
36
- - ability to dump internal references, either soft ones (objects or
37
- resources) or hard ones (``=& `` on arrays or objects properties).
38
- Repeated occurrences of the same object/array/resource won't appear
39
- again and again anymore. Moreover, you'll be able to inspect the
40
- reference structure of your data.
41
- - ability to operate in the context of an output buffering handler.
42
-
43
- ``dump() `` is just a thin wrapper for :method: `VarDumper::dump()
44
- <Symfony\\ Component\\ VarDumper\\ VarDumper::dump> ` so can you also use it directly.
32
+ - per object and resource types specialized view to e.g. filter out
33
+ Doctrine internals while dumping a single proxy entity, or get more
34
+ insight on opened files with :phpfunction: `stream_get_meta_data() `.
35
+ - configurable output formats: HTML or colored command line output.
36
+ - ability to dump internal references, either soft ones (objects or
37
+ resources) or hard ones (``=& `` on arrays or objects properties).
38
+ Repeated occurrences of the same object/array/resource won't appear
39
+ again and again anymore. Moreover, you'll be able to inspect the
40
+ reference structure of your data.
41
+ - ability to operate in the context of an output buffering handler.
42
+
43
+ ``dump() `` is just a thin wrapper for
44
+ :method: `VarDumper::dump() <Symfony\\ Component\\ VarDumper\\ VarDumper::dump> `
45
+ so can you also use it directly.
45
46
You can change the behavior of this function by calling
46
47
:method: `VarDumper::setHandler($callable) <Symfony\\ Component\\ VarDumper\\ VarDumper::setHandler> `:
47
48
calls to ``dump() `` will then be forwarded to ``$callable ``, given as first argument.
@@ -53,12 +54,13 @@ Cloners
53
54
~~~~~~~
54
55
55
56
A cloner is used to create an intermediate representation of any PHP variable.
56
- Its output is a :class: `Data < Symfony\\ Component\\ VarDumper\\ Cloner\\ Data> `
57
+ Its output is a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
57
58
object that wraps this representation. A cloner also applies limits when
58
59
creating the representation, so that the corresponding Data object could
59
60
represent only a subset of the cloned variable.
60
61
61
- You can create a Data object this way::
62
+ You can create a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
63
+ object this way::
62
64
63
65
$cloner = new PhpCloner();
64
66
$data = $cloner->cloneVar($myVar);
@@ -74,9 +76,10 @@ Casters
74
76
~~~~~~~
75
77
76
78
Objects and resources nested in a PHP variable are casted to arrays in the
77
- intermediate Data representation. You can tweak the array representation for
78
- each object/resource by hooking a Caster into this process. The component
79
- already includes many casters for base PHP classes and other common classes.
79
+ intermediate :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data `
80
+ representation. You can tweak the array representation for each object/resource
81
+ by hooking a Caster into this process. The component already includes many
82
+ casters for base PHP classes and other common classes.
80
83
81
84
If you want to build your own Caster, you can register one before cloning
82
85
a PHP variable. Casters are registered using either a Cloner's constructor
@@ -107,14 +110,14 @@ interfaces, the parents classes and then the main class. Several casters
107
110
can also be registered for the same resource type/class/interface.
108
111
They are called in registration order.
109
112
110
- Casters are responsible for returning the properties of the object orresource
113
+ Casters are responsible for returning the properties of the object or resource
111
114
being cloned in an array. They are callables that accept four arguments:
112
115
113
- - the object or resource being casted,
114
- - an array modelled for objects after PHP's native ``(array) `` cast operator,
115
- - a :class: `Stub <Sumfony \\ Component\\ VarDumper\\ Cloner\\ Stub> ` object
116
- representing the main properties of the object (class, type, etc.),
117
- - true/false when the caster is called nested is a structure or not.
116
+ - the object or resource being casted,
117
+ - an array modelled for objects after PHP's native ``(array) `` cast operator,
118
+ - a :class: `Symfony \\ Component\\ VarDumper\\ Cloner\\ Stub ` object
119
+ representing the main properties of the object (class, type, etc.),
120
+ - true/false when the caster is called nested is a structure or not.
118
121
119
122
Here is a simple caster not doing anything::
120
123
@@ -138,11 +141,65 @@ for virtual properties and ``\0+\0`` for dynamic ones (runtime added
138
141
properties not in the class declaration).
139
142
140
143
.. note ::
141
-
142
144
Although you can, it is best advised not to alter the state of an object
143
145
while casting it in a Caster.
144
146
145
147
Dumpers
146
148
~~~~~~~
147
149
150
+ A dumper is responsible for outputting a string representation of a PHP variable,
151
+ using a :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data ` object as input.
152
+ The destination and the formatting of this output vary with dumpers.
153
+
154
+ This component comes with an :class: `Symfony\\ Component\\ VarDumper\\ Dumper\\ HtmlDumper `
155
+ for HTML output and a :class: `Symfony\\ Component\\ VarDumper\\ Dumper\\ CliDumper `
156
+ for optionally colored command line output.
157
+
158
+ For example, if you want to dump some ``$variable ``, just do::
159
+
160
+ $cloner = new PhpCloner();
161
+ $dumper = new CliDumper();
162
+
163
+ $dumper->dump($cloner->cloneVar($variable));
164
+
165
+ By using the first argument of the constructor, you can select the output
166
+ stream where the dump will be written. By default, the ``CliDumper `` writes
167
+ on ``php://stdout `` and the ``HtmlDumper `` on ``php://output ``, but any PHP
168
+ stream (resource or URL) is acceptable.
169
+
170
+ Instead of a stream destination, you can also pass it a ``callable `` that
171
+ will be called repeatedly for each line generated by a dumper. This
172
+ callable can be configured using the first argument of a dumper's constructor,
173
+ but also using the
174
+ :method: `Symfony\\ Component\\ VarDumper\\ Dumper\\ AbstractDumper::setLineDumper `
175
+ method or using the second argument of the
176
+ :method: `Symfony\\ Component\\ VarDumper\\ Dumper\\ AbstractDumper::dump ` method.
177
+
178
+ For example, to get a dump in a variable, you can do::
179
+
180
+ $cloner = new PhpCloner();
181
+ $dumper = new CliDumper();
182
+ $output = '';
183
+
184
+ $dumper->dump(
185
+ $cloner->cloneVar($variable),
186
+ function ($line, $depth) use (&$output) {
187
+ // A negative depth means "end of dump"
188
+ if ($depth >= 0) {
189
+ // Adds a two spaces indentation to the line
190
+ $output .= str_repeat(' ', $depth).$line."\n";
191
+ }
192
+ }
193
+ );
194
+
195
+ // $output is now populated with the dump representation of $variable
196
+
197
+ Dumpers implement the :class: `Symfony\\ Component\\ VarDumper\\ Dumper\\ DataDumperInterface `
198
+ interface that specifies the
199
+ :method: `dump(Data $data) <Symfony\\ Component\\ VarDumper\\ Dumper\\ DataDumperInterface::dump> `
200
+ method. They also typically implement the
201
+ :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ DumperInterface ` that frees
202
+ them from re-implementing the logic required to walk through a
203
+ :class: `Symfony\\ Component\\ VarDumper\\ Cloner\\ Data ` object's internal structure.
204
+
148
205
.. _Packagist : https://packagist.org/packages/symfony/var-dumper
0 commit comments