Skip to content

[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

Merged
merged 14 commits into from
Nov 5, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The Components
stopwatch
templating/index
translation/index
var_dumper
yaml/index

.. include:: /components/map.rst.inc
4 changes: 4 additions & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
* :doc:`/components/translation/usage`
* :doc:`/components/translation/custom_formats`

* **VarDumper**

* :doc:`/components/var_dumper`

* :doc:`/components/yaml/index`

* :doc:`/components/yaml/introduction`
Expand Down
134 changes: 134 additions & 0 deletions components/var_dumper.rst
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.
Copy link
Member

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?

... 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()``,
Copy link
Member

Choose a reason for hiding this comment

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

...

Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this phrase: filter out Doctrine noise but I wonder if Doctrine creators could be offended. Is it any other way to say the same but more gently?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest filter out Doctrine internals

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command line with colors --> ANSI-colored command ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does not output a command

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then: colored command line

- 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inspect

- ability to operate in the context of an output buffering handler.
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use an API doc link here: :method:VarDumper::dump() <Symfony\Component\VarDumper\VarDumper::dump>``

Copy link
Member

Choose a reason for hiding this comment

The 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)``:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use an api doc link here too: :method:Symfony\Component\VarDumper\VarDumper::setHandler``.

calls to ``dump()`` will then be forwarded to the ``$callable`` given as first argument.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comma before "given as first argument"


Advanced usage
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea

Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably mean afterwards instead of afterwise


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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The component already has a many casters for -> The component already includes many casters for


If you want to build your how Caster, you can register one before cloning a PHP variable.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to build your how Caster -> If you want to build your own Caster

Casters are registered using either a Cloner's constructor or its ``addCasters()`` method::

$myCasters = array(...);
$cloner = new PhpCloner($myCasters);

or::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would replace this with a code comment // or


$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.
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// ... populate/alter $array to your needs


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.
Copy link
Member

Choose a reason for hiding this comment

The 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