minor #23517 [Profiler][Validator] ValidatorDataCollector: use new DataCollector::getCasters() method (ogizanagi)

This PR was merged into the 3.4 branch.

Discussion
----------

[Profiler][Validator] ValidatorDataCollector: use new DataCollector::getCasters() method

| Q             | A
| ------------- | ---
| Branch?       | 3.4 <!-- see comment below -->
| Bug fix?      | no
| New feature?  | no <!-- don't forget updating src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget updating UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/pull/23465#discussion_r126382240 <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

~~First commit targets 3.3; see https://github.com/symfony/symfony/pull/23516.~~

I didn't re-used the `ConstraintViolationInterface` caster used in the form collector, as it's the purpose of the validator collector to show the constraints data.

Commits
-------

c725a700cf [Profiler][Validator] ValidatorDataCollector: use new DataCollector::getCasters() method
This commit is contained in:
Fabien Potencier 2017-07-17 12:35:31 +02:00
commit aa27974920
2 changed files with 23 additions and 27 deletions

View File

@ -19,8 +19,7 @@ use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\Validator\Validator\TraceableValidator;
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Cloner\Stub;
/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
@ -28,7 +27,6 @@ use Symfony\Component\VarDumper\Cloner\VarCloner;
class ValidatorDataCollector extends DataCollector implements LateDataCollectorInterface
{
private $validator;
private $cloner;
public function __construct(TraceableValidator $validator)
{
@ -77,29 +75,26 @@ class ValidatorDataCollector extends DataCollector implements LateDataCollectorI
return 'validator';
}
/**
* {@inheritdoc}
*/
protected function cloneVar($var)
protected function getCasters()
{
if ($var instanceof Data) {
return $var;
}
return parent::getCasters() + array(
\Exception::class => function (\Exception $e, array $a, Stub $s) {
foreach (array("\0Exception\0previous", "\0Exception\0trace") as $k) {
if (isset($a[$k])) {
unset($a[$k]);
++$s->cut;
}
}
if (null === $this->cloner) {
$this->cloner = new VarCloner();
$this->cloner->setMaxItems(-1);
$this->cloner->addCasters(array(
FormInterface::class => function (FormInterface $f, array $a) {
return array(
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
Caster::PREFIX_VIRTUAL.'data' => $f->getData(),
);
},
));
}
return $this->cloner->cloneVar($var, Caster::EXCLUDE_VERBOSE);
return $a;
},
FormInterface::class => function (FormInterface $f, array $a) {
return array(
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
Caster::PREFIX_VIRTUAL.'data' => $f->getData(),
);
},
);
}
}

View File

@ -22,8 +22,8 @@
},
"require-dev": {
"symfony/http-foundation": "~2.8|~3.0|~4.0",
"symfony/http-kernel": "~2.8|~3.0|~4.0.0",
"symfony/var-dumper": "~3.3|~4.0.0",
"symfony/http-kernel": "^3.3.5|~4.0",
"symfony/var-dumper": "~3.3|~4.0",
"symfony/intl": "^2.8.18|^3.2.5|~4.0",
"symfony/yaml": "~3.3|~4.0",
"symfony/config": "~2.8|~3.0|~4.0",
@ -38,6 +38,7 @@
"conflict": {
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
"symfony/dependency-injection": "<3.3",
"symfony/http-kernel": "<3.3.5",
"symfony/yaml": "<3.3"
},
"suggest": {