Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Profiler][Validator] Add a validator panel in profiler
- Loading branch information
Showing
11 changed files
with
551 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Symfony/Bundle/FrameworkBundle/Resources/config/validator_debug.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<defaults public="false" /> | ||
|
||
<service id="debug.validator" decorates="validator" decoration-priority="255" class="Symfony\Component\Validator\Validator\TraceableValidator"> | ||
<argument type="service" id="debug.validator.inner" /> | ||
</service> | ||
|
||
<!-- DataCollector --> | ||
<service id="data_collector.validator" class="Symfony\Component\Validator\DataCollector\ValidatorDataCollector"> | ||
<argument type="service" id="debug.validator"/> | ||
<tag name="data_collector" template="@WebProfiler/Collector/validator.html.twig" id="validator" priority="320" /> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/validator.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{% extends '@WebProfiler/Profiler/layout.html.twig' %} | ||
|
||
{% block toolbar %} | ||
{% if collector.violationsCount > 0 or collector.calls|length %} | ||
{% set status_color = collector.violationsCount ? 'red' : '' %} | ||
{% set icon %} | ||
{{ include('@WebProfiler/Icon/validator.svg') }} | ||
<span class="sf-toolbar-value"> | ||
{{ collector.violationsCount }} | ||
</span> | ||
{% endset %} | ||
|
||
{% set text %} | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Validator calls</b> | ||
<span class="sf-toolbar-status">{{ collector.calls|length }}</span> | ||
</div> | ||
<div class="sf-toolbar-info-piece"> | ||
<b>Number of violations</b> | ||
<span class="sf-toolbar-status {{- collector.violationsCount > 0 ? ' sf-toolbar-status-red' }}">{{ collector.violationsCount }}</span> | ||
</div> | ||
{% endset %} | ||
|
||
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: profiler_url, status: status_color }) }} | ||
{% endif %} | ||
{% endblock %} | ||
|
||
{% block menu %} | ||
<span class="label {{- collector.violationsCount ? ' label-status-error' }} {{ collector.calls is empty ? 'disabled' }}"> | ||
<span class="icon">{{ include('@WebProfiler/Icon/validator.svg') }}</span> | ||
<strong>Validator</strong> | ||
{% if collector.violationsCount > 0 %} | ||
<span class="count"> | ||
<span>{{ collector.violationsCount }}</span> | ||
</span> | ||
{% endif %} | ||
</span> | ||
{% endblock %} | ||
|
||
{% block panel %} | ||
<h2>Validator calls</h2> | ||
|
||
{% for call in collector.calls %} | ||
<div class="sf-validator sf-reset"> | ||
<span class="metadata">In | ||
{% set caller = call.caller %} | ||
{% if caller.line %} | ||
{% set link = caller.file|file_link(caller.line) %} | ||
{% if link %} | ||
<a href="{{ link }}" title="{{ caller.file }}">{{ caller.name }}</a> | ||
{% else %} | ||
<abbr title="{{ caller.file }}">{{ caller.name }}</abbr> | ||
{% endif %} | ||
{% else %} | ||
{{ caller.name }} | ||
{% endif %} | ||
line <a class="text-small sf-toggle" data-toggle-selector="#sf-trace-{{ loop.index0 }}">{{ caller.line }}</a> (<a class="text-small sf-toggle" data-toggle-selector="#sf-context-{{ loop.index0 }}">context</a>): | ||
</span> | ||
|
||
<div class="sf-validator-compact hidden" id="sf-trace-{{ loop.index0 }}"> | ||
<div class="trace"> | ||
{{ caller.file|file_excerpt(caller.line) }} | ||
</div> | ||
</div> | ||
|
||
<div class="sf-validator-compact hidden sf-validator-context" id="sf-context-{{ loop.index0 }}"> | ||
{{ profiler_dump(call.context, maxDepth=1) }} | ||
</div> | ||
|
||
{% if call.violations|length %} | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Path</th> | ||
<th>Message</th> | ||
<th>Invalid value</th> | ||
<th>Violation</th> | ||
</tr> | ||
</thead> | ||
{% for violation in call.violations %} | ||
<tr> | ||
<td>{{ violation.propertyPath }}</td> | ||
<td>{{ violation.message }}</td> | ||
<td>{{ profiler_dump(violation.seek('invalidValue')) }}</td> | ||
<td>{{ profiler_dump(violation) }}</td> | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
{% else %} | ||
No violations | ||
{% endif %} | ||
</div> | ||
{% else %} | ||
<div class="empty"> | ||
<p>No calls to the validator were collected during this request.</p> | ||
</div> | ||
{% endfor %} | ||
{% endblock %} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Icon/validator.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/Symfony/Component/Validator/DataCollector/ValidatorDataCollector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\DataCollector; | ||
|
||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollector; | ||
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; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
*/ | ||
class ValidatorDataCollector extends DataCollector implements LateDataCollectorInterface | ||
{ | ||
private $validator; | ||
private $cloner; | ||
|
||
public function __construct(TraceableValidator $validator) | ||
{ | ||
$this->validator = $validator; | ||
$this->data = array( | ||
'calls' => array(), | ||
'violations_count' => 0, | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function collect(Request $request, Response $response, \Exception $exception = null) | ||
{ | ||
// Everything is collected once, on kernel terminate. | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function lateCollect() | ||
{ | ||
$collected = $this->validator->getCollectedData(); | ||
$this->data['calls'] = $this->cloneVar($collected); | ||
$this->data['violations_count'] += array_reduce($collected, function ($previous, $item) { | ||
return $previous += count($item['violations']); | ||
}, 0); | ||
} | ||
|
||
public function getCalls() | ||
{ | ||
return $this->data['calls']; | ||
} | ||
|
||
public function getViolationsCount() | ||
{ | ||
return $this->data['violations_count']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'validator'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function cloneVar($var) | ||
{ | ||
if ($var instanceof Data) { | ||
return $var; | ||
} | ||
|
||
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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/Validator/Tests/DataCollector/ValidatorDataCollectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\DataCollector; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\ConstraintViolationList; | ||
use Symfony\Component\Validator\DataCollector\ValidatorDataCollector; | ||
use Symfony\Component\Validator\Validator\TraceableValidator; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
class ValidatorDataCollectorTest extends TestCase | ||
{ | ||
public function testCollectsValidatorCalls() | ||
{ | ||
$originalValidator = $this->createMock(ValidatorInterface::class); | ||
$validator = new TraceableValidator($originalValidator); | ||
|
||
$collector = new ValidatorDataCollector($validator); | ||
|
||
$violations = new ConstraintViolationList(array( | ||
$this->createMock(ConstraintViolation::class), | ||
$this->createMock(ConstraintViolation::class), | ||
)); | ||
$originalValidator->method('validate')->willReturn($violations); | ||
|
||
$validator->validate(new \stdClass()); | ||
|
||
$collector->lateCollect(); | ||
|
||
$calls = $collector->getCalls(); | ||
|
||
$this->assertCount(1, $calls); | ||
$this->assertSame(2, $collector->getViolationsCount()); | ||
|
||
$call = $calls[0]; | ||
|
||
$this->assertArrayHasKey('caller', $call); | ||
$this->assertArrayHasKey('context', $call); | ||
$this->assertArrayHasKey('violations', $call); | ||
$this->assertCount(2, $call['violations']); | ||
} | ||
|
||
protected function createMock($classname) | ||
{ | ||
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock(); | ||
} | ||
} |
Oops, something went wrong.