[Validator] Reduced number of method calls on the execution context

This commit is contained in:
Bernhard Schussek 2014-03-17 17:55:15 +01:00
parent 73c9cc5806
commit 2f23d9725b
4 changed files with 29 additions and 74 deletions

View File

@ -139,24 +139,10 @@ class ExecutionContext implements ExecutionContextInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setValue($value) public function setNode($value, MetadataInterface $metadata = null, $propertyPath)
{ {
$this->value = $value; $this->value = $value;
}
/**
* {@inheritdoc}
*/
public function setMetadata(MetadataInterface $metadata = null)
{
$this->metadata = $metadata; $this->metadata = $metadata;
}
/**
* {@inheritdoc}
*/
public function setPropertyPath($propertyPath)
{
$this->propertyPath = (string) $propertyPath; $this->propertyPath = (string) $propertyPath;
} }

View File

@ -102,32 +102,14 @@ interface ExecutionContextInterface extends LegacyExecutionContextInterface
/** /**
* Sets the currently validated value. * Sets the currently validated value.
* *
* @param mixed $value The validated value * @param mixed $value The validated value
* @param MetadataInterface $metadata The validation metadata
* @param string $propertyPath The property path to the current value
* *
* @internal Used by the validator engine. Should not be called by user * @internal Used by the validator engine. Should not be called by user
* code. * code.
*/ */
public function setValue($value); public function setNode($value, MetadataInterface $metadata = null, $propertyPath);
/**
* Sets the current validation metadata.
*
* @param MetadataInterface $metadata The validation metadata
*
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setMetadata(MetadataInterface $metadata = null);
/**
* Sets the property path leading to the current value.
*
* @param string $propertyPath The property path to the current value
*
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setPropertyPath($propertyPath);
/** /**
* Sets the currently validated group. * Sets the currently validated group.

View File

@ -69,9 +69,7 @@ class NodeValidationVisitor extends AbstractVisitor
return true; return true;
} }
$context->setValue($node->value); $context->setNode($node->value, $node->metadata, $node->propertyPath);
$context->setMetadata($node->metadata);
$context->setPropertyPath($node->propertyPath);
if ($node instanceof ClassNode) { if ($node instanceof ClassNode) {
$this->replaceDefaultGroup($node); $this->replaceDefaultGroup($node);
@ -171,43 +169,34 @@ class NodeValidationVisitor extends AbstractVisitor
*/ */
private function validateNodeForGroup(Node $node, $group, ExecutionContextInterface $context, $objectHash) private function validateNodeForGroup(Node $node, $group, ExecutionContextInterface $context, $objectHash)
{ {
try { $context->setGroup($group);
$context->setGroup($group);
foreach ($node->metadata->findConstraints($group) as $constraint) { foreach ($node->metadata->findConstraints($group) as $constraint) {
// Prevent duplicate validation of constraints, in the case // Prevent duplicate validation of constraints, in the case
// that constraints belong to multiple validated groups // that constraints belong to multiple validated groups
if (null !== $objectHash) { if (null !== $objectHash) {
$constraintHash = spl_object_hash($constraint); $constraintHash = spl_object_hash($constraint);
if ($node instanceof ClassNode) { if ($node instanceof ClassNode) {
if ($context->isClassConstraintValidated($objectHash, $constraintHash)) { if ($context->isClassConstraintValidated($objectHash, $constraintHash)) {
continue; continue;
}
$context->markClassConstraintAsValidated($objectHash, $constraintHash);
} elseif ($node instanceof PropertyNode) {
$propertyName = $node->metadata->getPropertyName();
if ($context->isPropertyConstraintValidated($objectHash, $propertyName, $constraintHash)) {
continue;
}
$context->markPropertyConstraintAsValidated($objectHash, $propertyName, $constraintHash);
} }
}
$validator = $this->validatorFactory->getInstance($constraint); $context->markClassConstraintAsValidated($objectHash, $constraintHash);
$validator->initialize($context); } elseif ($node instanceof PropertyNode) {
$validator->validate($node->value, $constraint); $propertyName = $node->metadata->getPropertyName();
if ($context->isPropertyConstraintValidated($objectHash, $propertyName, $constraintHash)) {
continue;
}
$context->markPropertyConstraintAsValidated($objectHash, $propertyName, $constraintHash);
}
} }
$context->setGroup(null); $validator = $this->validatorFactory->getInstance($constraint);
} catch (\Exception $e) { $validator->initialize($context);
// Should be put into a finally block once we switch to PHP 5.5 $validator->validate($node->value, $constraint);
$context->setGroup(null);
throw $e;
} }
} }

View File

@ -581,9 +581,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
*/ */
public function validateNode($value, $valueHash, $container, $containerHash, MetadataInterface $metadata = null, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context) public function validateNode($value, $valueHash, $container, $containerHash, MetadataInterface $metadata = null, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
{ {
$context->setValue($value); $context->setNode($value, $metadata, $propertyPath);
$context->setMetadata($metadata);
$context->setPropertyPath($propertyPath);
// if group (=[<G1,G2>,G3,G4]) contains group sequence (=<G1,G2>) // if group (=[<G1,G2>,G3,G4]) contains group sequence (=<G1,G2>)
// then call traverse() with each entry of the group sequence and abort // then call traverse() with each entry of the group sequence and abort