Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3033bc7

Browse files
[VarDumper] Adding semantics with metadata
1 parentf78d06b commit3033bc7

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

‎components/var_dumper.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
The VarDumper Component
66
=======================
77

8-
The VarDumper component provides mechanisms forwalking through any
9-
arbitrary PHPvariable. Built on top, it provides a better ``dump()``
10-
functionthat you can use instead of:phpfunction:`var_dump`.
8+
The VarDumper component provides mechanisms forextracting the state out of
9+
any PHPvariables. Built on top, it provides a better ``dump()`` function
10+
that you can use instead of:phpfunction:`var_dump`.
1111

1212
Installation
1313
------------

‎components/var_dumper/advanced.rst‎

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ A cloner is used to create an intermediate representation of any PHP variable.
3434
Its output is a:class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
3535
object that wraps this representation.
3636

37-
You can create a:class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
38-
object this way::
37+
You can create a ``Data`` object this way::
3938

4039
use Symfony\Component\VarDumper\Cloner\VarCloner;
4140

@@ -45,8 +44,11 @@ object this way::
4544
// see the example at the top of this page
4645
// $dumper->dump($data);
4746

48-
A cloner also applies limits when creating the representation, so that the
49-
corresponding Data object could represent only a subset of the cloned variable.
47+
Whatever the cloned data structure, resulting ``Data`` objects are always
48+
serializable.
49+
50+
A cloner applies limits when creating the representation, so that one
51+
can represent only a subset of the cloned variable.
5052
Before calling:method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::cloneVar`,
5153
you can configure these limits:
5254

@@ -154,6 +156,17 @@ Another option for doing the same could be::
154156

155157
// $output is now populated with the dump representation of $variable
156158

159+
..tip::
160+
161+
You can pass ``true`` to the second argument of the
162+
:method:`Symfony\\Component\\VarDumper\\Dumper\\AbstractDumper::dump`
163+
method to make it return the dump as a string::
164+
165+
$output = $dumper->dump($cloner->cloneVar($variable), true);
166+
167+
..versionadded::3.2
168+
The ability to return a string was introduced in Symfony 3.2.
169+
157170
Dumpers implement the:class:`Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface`
158171
interface that specifies the
159172
:method:`dump(Data $data) <Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface::dump>`
@@ -167,7 +180,7 @@ Casters
167180

168181
Objects and resources nested in a PHP variable are "cast" to arrays in the
169182
intermediate:class:`Symfony\\Component\\VarDumper\\Cloner\\Data`
170-
representation. You cantweak the array representation for each object/resource
183+
representation. You cancustomize the array representation for each object/resource
171184
by hooking a Caster into this process. The component already includes many
172185
casters for base PHP classes and other common classes.
173186

@@ -203,17 +216,21 @@ can also be registered for the same resource type/class/interface.
203216
They are called in registration order.
204217

205218
Casters are responsible for returning the properties of the object or resource
206-
being cloned in an array. They are callables that acceptfour arguments:
219+
being cloned in an array. They are callables that acceptfive arguments:
207220

208-
* the object or resource being casted,
209-
* an array modelled for objects after PHP's native ``(array)`` cast operator,
221+
* the object or resource being casted;
222+
* an array modelled for objects after PHP's native ``(array)`` cast operator;
210223
* a:class:`Symfony\\Component\\VarDumper\\Cloner\\Stub` object
211-
representing the main properties of the object (class, type, etc.),
212-
* true/false when the caster is called nested in a structure or not.
224+
representing the main properties of the object (class, type, etc.);
225+
* true/false when the caster is called nested in a structure or not;
226+
* A bit field of:class:`Symfony\\Component\\VarDumper\\Caster\\Caster```::EXCLUDE_*``
227+
constants.
213228

214229
Here is a simple caster not doing anything::
215230

216-
function myCaster($object, $array, $stub, $isNested)
231+
use Symfony\Component\VarDumper\Cloner\Stub;
232+
233+
function myCaster($object, $array, Stub $stub, $isNested, $filter)
217234
{
218235
// ... populate/alter $array to your needs
219236

@@ -240,3 +257,50 @@ properties not in the class declaration).
240257
..tip::
241258

242259
Before writing your own casters, you should check the existing ones.
260+
261+
Adding Semantics with Metadata
262+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263+
264+
..versionadded::3.2
265+
As of Symfony 3.2, casters can attach metadata attributes to
266+
:class:`Symfony\\Component\\VarDumper\\Cloner\\Stub` objects to inform
267+
dumpers about the precise type of the dumped values.
268+
269+
Since casters are hooked on specific classes or interfaces, they know about the
270+
objects they manipulate. By altering the ``$stub`` object (the third argument of
271+
any caster), one can transfer this knowledge to the resulting ``Data`` object,
272+
thus to dumpers. To help you do this (see the source code for how it works),
273+
the component comes with a set of wrappers for common additional semantics. You
274+
can use:
275+
276+
*:class:`Symfony\\Component\\VarDumper\\Caster\\ConstStub` to wrap a value that is
277+
best represented by a PHP constant;
278+
*:class:`Symfony\\Component\\VarDumper\\Caster\\ClassStub` to wrap a PHP identifier
279+
(*i.e.* a class name, a method name, an interface, *etc.*);
280+
*:class:`Symfony\\Component\\VarDumper\\Caster\\CutStub` to replace big noisy
281+
objects/strings/*etc.* by ellipses;
282+
*:class:`Symfony\\Component\\VarDumper\\Caster\\CutArrayStub` to keep only some
283+
useful keys of an array;
284+
*:class:`Symfony\\Component\\VarDumper\\Caster\\EnumStub` to wrap a set of virtual
285+
values (*i.e.* values that do not exist as properties in the original PHP data
286+
structure, but are worth listing alongside with real ones);
287+
*:class:`Symfony\\Component\\VarDumper\\Caster\\LinkStub` to wrap strings that can
288+
be turned into links by dumpers;
289+
*:class:`Symfony\\Component\\VarDumper\\Caster\\TraceStub` and their
290+
*:class:`Symfony\\Component\\VarDumper\\Caster\\FrameStub` and
291+
*:class:`Symfony\\Component\\VarDumper\\Caster\\ArgsStub` relatives to wrap PHP
292+
traces (used by:class:`Symfony\\Component\\VarDumper\\Caster\\ExceptionCaster`).
293+
294+
For example, if you know that your ``Product`` objects have a ``brochure`` property
295+
that holds a file name or a URL, you can wrap them in a ``LinkStub`` to tell
296+
``HtmlDumper`` to make them clickable::
297+
298+
use Symfony\Component\VarDumper\Caster\LinkStub;
299+
use Symfony\Component\VarDumper\Cloner\Stub;
300+
301+
function ProductCaster(Product $object, $array, Stub $stub, $isNested, $filter = 0)
302+
{
303+
$array['brochure'] = new LinkStub($array['brochure']);
304+
305+
return $array;
306+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp