-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[WIP] var-dumper component #4243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
26edf7a
5e0b9bd
7dc6e5c
036edcb
350e0c7
2fc3811
b5b45bc
ee2d7e4
cb0ee89
6a3e170
edb0ff9
c578fe7
23712fb
041e858
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
.. index:: | ||
single: VarDumper | ||
single: Components; VarDumper | ||
|
||
The VarDumper Component | ||
======================= | ||
|
||
The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. | ||
Built on top, it provides a better ``dump()`` function, that you can use instead of ``var_dump()``, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would stop the quick intro after this line and then put the list as normal text. E.g. The VarDumper component provides mechanisms for walking through any arbitrary PHP variable.
Built on top, it provides a better ``dump()`` function that you can use instead of :phpfunction:`var_dump`.
The advantages of the ``dump()`` function are:
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestion (see also what I wrote here). |
||
*better* meaning: | ||
|
||
- per object and resource types specialized view to e.g. filter out Doctrine noise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love this phrase: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest |
||
while dumping a single proxy entity, or get more insight on opened files with | ||
``stream_get_meta_data()``. | ||
- configurable output format: HTML, command line with colors or JSON. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does not output a command There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then: |
||
- ability to dump internal references, either soft ones (objects or resources) | ||
or hard ones (``=&`` on arrays or objects properties). Repeated occurrences of | ||
the same object/array/resource won't appear again and again anymore. Moreover, | ||
you'll be able to inspected the reference structure of your data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inspect |
||
- ability to operate in the context of an output buffering handler. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the whole introduction is too long. In the other components, we usually have one introductional sentence which is rendered as a block quote. What about indenting onl the first sentence and start with "Built on top" at the first character of the next paragraph (which then might have to be reworded a bit)? |
||
|
||
.. versionadded:: 2.6 | ||
The VarDumper component was introduced in Symfony 2.6. | ||
|
||
Installation | ||
------------ | ||
|
||
You can install the component in 2 different ways: | ||
|
||
* :doc:`Install it via Composer </components/using_components>` (``symfony/var-dumper`` on `Packagist`_); | ||
* Use the official Git repository (https://github.com/symfony/VarDumper). | ||
|
||
The dump() function | ||
------------------- | ||
|
||
The VarDumper component creates a global ``dump()`` function that is auto-configured out of the box: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "auto-configured" and "out of the box" mean the same thing, don't they? Maybe just write "[...] that is ready to be used out of the box."? |
||
HTML or CLI output is automatically selected based on the current PHP SAPI. | ||
|
||
``dump()`` is just a thin wrapper for ``\Symfony\Component\VarDumper\VarDumper::dump()`` so can you also use it directly. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use an API doc link here: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] so can you also use [...] -> [...]. You can also use [...] |
||
You can change the behavior of this function by calling ``\Symfony\Component\VarDumper\VarDumper::setHandler($callable)``: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use an api doc link here too: |
||
calls to ``dump()`` will then be forwarded to the ``$callable`` given as first argument. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a comma before "given as first argument" |
||
|
||
Advanced usage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, the advanced usage should go in a separate file. And the basic usage should give more details (a screenshot of the output for instance) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Usage" has to be capitalized |
||
-------------- | ||
|
||
Cloners | ||
~~~~~~~ | ||
|
||
A cloner is used to create an intermediate representation of any PHP variable. | ||
Its output is a Data object that wraps this representation. | ||
A cloner also applies limits when creating the representation, so that the corresponding | ||
Data object could represent only a subset of the cloned variable. | ||
|
||
You can create a Data object this way:: | ||
|
||
$cloner = new PhpCloner(); | ||
$data = $cloner->cloneVar($myVar); | ||
|
||
Before cloning, you can configure the limits with:: | ||
|
||
$cloner->setMaxItems($number); | ||
$cloner->setMaxString($number); | ||
|
||
These limits will be applied when calling ``->cloneVar()`` afterwise. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably mean |
||
|
||
Casters | ||
~~~~~~~ | ||
|
||
Objects and resources nested in a PHP variable are casted to arrays in the intermediate Data representation. | ||
You can tweak the array representation for each object/resource by hooking a Caster into this process. | ||
The component already has a many casters for base PHP classes and other common classes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
If you want to build your how Caster, you can register one before cloning a PHP variable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Casters are registered using either a Cloner's constructor or its ``addCasters()`` method:: | ||
|
||
$myCasters = array(...); | ||
$cloner = new PhpCloner($myCasters); | ||
|
||
or:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would replace this with a code comment |
||
|
||
$cloner->addCasters($myCasters); | ||
|
||
The provided ``$myCasters`` argument is an array that maps a class, an interface or a resource type to a callable:: | ||
|
||
$myCasters = array( | ||
'FooClass' => $myFooClassCallableCaster, | ||
':bar resource' => $myBarResourceCallableCaster, | ||
); | ||
|
||
As you can notice, resource types are prefixed by a ``:`` to prevent colliding with a class name. | ||
|
||
Because an object has one main class and potentially many parent classes or interfaces, | ||
many casters can be applied to one object. In this case, casters are called one after the other, | ||
starting from casters bound to the interfaces, the parents classes and then the main class. | ||
Several casters can also be registered for the same resource type/class/interface. | ||
They are called in registration order. | ||
|
||
Casters are responsible for returning the properties of the object or resource being cloned in an array. | ||
They are callables that accept four arguments:: | ||
|
||
/** | ||
* A caster not doing anything. | ||
* | ||
* @param object|resource $object The object or resource being casted. | ||
* @param array $array An array modelled for objects after PHP's native `(array)` cast operator. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. code blocks have a hard limit of 85 characters in the docs |
||
* @param Stub $stub A Cloner\Stub object representing the main properties of $object (class, type, etc.). | ||
* @param bool $isNested True/false when the caster is called nested is a structure or not. | ||
* | ||
* @return array The properties of $object casted in an array. | ||
*/ | ||
function myCaster($origValue, $array, $stub, $isNested) | ||
{ | ||
// Here, populate/alter $array to your needs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return $array; | ||
} | ||
|
||
For objects, the ``$array`` parameter comes pre-populated with PHP's native ``(array)`` casting operator, | ||
or with the return value of ``$object->__debugInfo()`` if the magic method exists. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove the comma before "or" |
||
Then, the return value of one Caster is given as argument to the next Caster in the chain. | ||
|
||
When casting with the ``(array)`` operator, PHP prefixes protected properties with a ``\0*\0`` | ||
and private ones with the class owning the property: e.g. ``\0Foobar\0`` prefixes all private properties | ||
of objects of type Foobar. Casters follow this convention and add two more prefixes: ``\0~\0`` is used for | ||
virtual properties and ``\0+\0`` for dynamic ones (runtime added properties not in the class declaration). | ||
|
||
.. note:: | ||
|
||
Although you can, it is best advised not to alter the state of an object while casting it in a Caster. | ||
|
||
Dumpers | ||
~~~~~~~ | ||
|
||
.. _Packagist: https://packagist.org/packages/symfony/var-dumper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you think that this phrase is too abstract?