Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[VarDumper] Adding semantics with metadata#6946
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,9 +5,9 @@ | ||
| The VarDumper Component | ||
| ======================= | ||
| The VarDumper component provides mechanisms forextracting the state out of | ||
Member 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.
MemberAuthor 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. state = content + context/metadata | ||
| any PHPvariables. Built on top, it provides a better ``dump()`` function | ||
| that you can use instead of:phpfunction:`var_dump`. | ||
| Installation | ||
| ------------ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -34,8 +34,7 @@ A cloner is used to create an intermediate representation of any PHP variable. | ||
| Its output is a :class:`Symfony\\Component\\VarDumper\\Cloner\\Data` | ||
| object that wraps this representation. | ||
| You can create a ``Data`` object this way:: | ||
| use Symfony\Component\VarDumper\Cloner\VarCloner; | ||
| @@ -45,8 +44,11 @@ object this way:: | ||
| // see the example at the top of this page | ||
| // $dumper->dump($data); | ||
| Whatever the cloned data structure, resulting ``Data`` objects are always | ||
| serializable. | ||
| A cloner applies limits when creating the representation, so that one | ||
| can represent only a subset of the cloned variable. | ||
| Before calling :method:`Symfony\\Component\\VarDumper\\Cloner\\VarCloner::cloneVar`, | ||
| you can configure these limits: | ||
| @@ -154,6 +156,17 @@ Another option for doing the same could be:: | ||
| // $output is now populated with the dump representation of $variable | ||
| .. tip:: | ||
Member 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. this empty line has to be removed | ||
| You can pass ``true`` to the second argument of the | ||
| :method:`Symfony\\Component\\VarDumper\\Dumper\\AbstractDumper::dump` | ||
| method to make it return the dump as a string:: | ||
| $output = $dumper->dump($cloner->cloneVar($variable), true); | ||
Member 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. please rewrite this like: ..tip:: You can pass ``true`` to the second argument of the:method:`Symfony\\Component\\VarDumper\\Dumper\\AbstractDumper::dump` method to make it return the dump as a string:: $output = $dumper->dump($cloner->cloneVar($variable), true); ..versionadded::3.2 The ability to return as string was introduced in Symfony 3.2. | ||
| .. versionadded:: 3.2 | ||
| The ability to return a string was introduced in Symfony 3.2. | ||
| Dumpers implement the :class:`Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface` | ||
| interface that specifies the | ||
| :method:`dump(Data $data) <Symfony\\Component\\VarDumper\\Dumper\\DataDumperInterface::dump>` | ||
| @@ -167,7 +180,7 @@ Casters | ||
| Objects and resources nested in a PHP variable are "cast" to arrays in the | ||
| intermediate :class:`Symfony\\Component\\VarDumper\\Cloner\\Data` | ||
| representation. You cancustomize the array representation for each object/resource | ||
| by hooking a Caster into this process. The component already includes many | ||
| casters for base PHP classes and other common classes. | ||
| @@ -203,17 +216,21 @@ 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 acceptfive arguments: | ||
| * the object or resource being casted; | ||
| * an array modelled for objects after PHP's native ``(array)`` cast operator; | ||
| * a :class:`Symfony\\Component\\VarDumper\\Cloner\\Stub` object | ||
| representing the main properties of the object (class, type, etc.); | ||
| * true/false when the caster is called nested in a structure or not; | ||
| * A bit field of :class:`Symfony\\Component\\VarDumper\\Caster\\Caster```::EXCLUDE_*`` | ||
| constants. | ||
| Here is a simple caster not doing anything:: | ||
| use Symfony\Component\VarDumper\Cloner\Stub; | ||
| function myCaster($object, $array, Stub $stub, $isNested, $filter) | ||
| { | ||
| // ... populate/alter $array to your needs | ||
| @@ -240,3 +257,50 @@ properties not in the class declaration). | ||
| .. tip:: | ||
| Before writing your own casters, you should check the existing ones. | ||
| Adding Semantics with Metadata | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| .. versionadded:: 3.2 | ||
| As of Symfony 3.2, casters can attach metadata attributes to | ||
| :class:`Symfony\\Component\\VarDumper\\Cloner\\Stub` objects to inform | ||
| dumpers about the precise type of the dumped values. | ||
| Since casters are hooked on specific classes or interfaces, they know about the | ||
| objects they manipulate. By altering the ``$stub`` object (the third argument of | ||
| any caster), one can transfer this knowledge to the resulting ``Data`` object, | ||
| thus to dumpers. To help you do this (see the source code for how it works), | ||
| the component comes with a set of wrappers for common additional semantics. You | ||
| can use: | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\ConstStub` to wrap a value that is | ||
| best represented by a PHP constant; | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\ClassStub` to wrap a PHP identifier | ||
| (*i.e.* a class name, a method name, an interface, *etc.*); | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\CutStub` to replace big noisy | ||
| objects/strings/*etc.* by ellipses; | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\CutArrayStub` to keep only some | ||
| useful keys of an array; | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\EnumStub` to wrap a set of virtual | ||
| values (*i.e.* values that do not exist as properties in the original PHP data | ||
| structure, but are worth listing alongside with real ones); | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\LinkStub` to wrap strings that can | ||
| be turned into links by dumpers; | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\TraceStub` and their | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\FrameStub` and | ||
| * :class:`Symfony\\Component\\VarDumper\\Caster\\ArgsStub` relatives to wrap PHP | ||
| traces (used by :class:`Symfony\\Component\\VarDumper\\Caster\\ExceptionCaster`). | ||
| For example, if you know that your ``Product`` objects have a ``brochure`` property | ||
| that holds a file name or a URL, you can wrap them in a ``LinkStub`` to tell | ||
| ``HtmlDumper`` to make them clickable:: | ||
| use Symfony\Component\VarDumper\Caster\LinkStub; | ||
| use Symfony\Component\VarDumper\Cloner\Stub; | ||
| function ProductCaster(Product $object, $array, Stub $stub, $isNested, $filter = 0) | ||
| { | ||
| $array['brochure'] = new LinkStub($array['brochure']); | ||
| return $array; | ||
| } | ||