add instance cache, remove unnecessary execution paths (#288)

fix wrong merge
This commit is contained in:
Dario Nuevo 2016-08-19 14:57:35 +02:00 committed by David Porter
parent 1296583112
commit 0f87b314cd
2 changed files with 21 additions and 14 deletions

View File

@ -23,7 +23,7 @@ class Factory
* @var SchemaStorage * @var SchemaStorage
*/ */
protected $schemaStorage; protected $schemaStorage;
/** /**
* @var UriRetriever $uriRetriever * @var UriRetriever $uriRetriever
*/ */
@ -56,6 +56,11 @@ class Factory
'validator' => 'JsonSchema\Validator', 'validator' => 'JsonSchema\Validator',
); );
/**
* @var array<ConstraintInterface>
*/
private $instanceCache = array();
/** /**
* @param SchemaStorage $schemaStorage * @param SchemaStorage $schemaStorage
* @param UriRetrieverInterface $uriRetriever * @param UriRetrieverInterface $uriRetriever
@ -78,7 +83,7 @@ class Factory
{ {
return $this->uriRetriever; return $this->uriRetriever;
} }
public function getSchemaStorage() public function getSchemaStorage()
{ {
return $this->schemaStorage; return $this->schemaStorage;
@ -124,12 +129,15 @@ class Factory
public function createInstanceFor($constraintName) public function createInstanceFor($constraintName)
{ {
if (array_key_exists($constraintName, $this->constraintMap)) { if (array_key_exists($constraintName, $this->constraintMap)) {
return new $this->constraintMap[$constraintName]( if (!isset($this->instanceCache[$constraintName])) {
$this->checkMode, $this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
$this->schemaStorage, $this->checkMode,
$this->uriRetriever, $this->schemaStorage,
$this $this->uriRetriever,
); $this
);
}
return clone $this->instanceCache[$constraintName];
} }
throw new InvalidArgumentException('Unknown constraint ' . $constraintName); throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
} }

View File

@ -105,11 +105,6 @@ class ObjectConstraint extends Constraint
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires'); $this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires');
} }
if (!$definition) {
// normal property verification
$this->checkUndefined($value, new \stdClass(), $path, $i);
}
$property = $this->getProperty($element, $i, new UndefinedConstraint()); $property = $this->getProperty($element, $i, new UndefinedConstraint());
if (is_object($property)) { if (is_object($property)) {
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path); $this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
@ -129,7 +124,11 @@ class ObjectConstraint extends Constraint
foreach ($objectDefinition as $i => $value) { foreach ($objectDefinition as $i => $value) {
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined')); $property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
$definition = $this->getProperty($objectDefinition, $i); $definition = $this->getProperty($objectDefinition, $i);
$this->checkUndefined($property, $definition, $path, $i);
if (is_object($definition)) {
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
$this->checkUndefined($property, $definition, $path, $i);
}
} }
} }