Skip to content

Commit

Permalink
perf: defer instantiation of validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Oct 25, 2023
1 parent 52c2222 commit d6adeb3
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ abstract class BaseModel
/**
* Our validator instance.
*
* @var ValidationInterface
* @var ValidationInterface|null
*/
protected $validation;

Expand Down Expand Up @@ -326,10 +326,6 @@ public function __construct(?ValidationInterface $validation = null)
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;

/**
* @var ValidationInterface|null $validation
*/
$validation ??= Services::validation(null, false);
$this->validation = $validation;

$this->initialize();
Expand Down Expand Up @@ -1153,6 +1149,10 @@ public function replace(?array $data = null, bool $returnSQL = false)
*/
public function errors(bool $forceDB = false)
{
if ($this->validation === null) {
return $this->doErrors();
}

// Do we have validation errors?
if (! $forceDB && ! $this->skipValidation && ($errors = $this->validation->getErrors())) {
return $errors;
Expand Down Expand Up @@ -1421,6 +1421,8 @@ public function setValidationRule(string $field, $fieldRules)
// ValidationRules can be either a string, which is the group name,
// or an array of rules.
if (is_string($rules)) {
$this->ensureValidation();

[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);

$this->validationRules = $rules;
Expand Down Expand Up @@ -1455,9 +1457,13 @@ public function cleanRules(bool $choice = false)
*/
public function validate($data): bool
{
if ($this->skipValidation || empty($data)) {
return true;
}

$rules = $this->getValidationRules();

if ($this->skipValidation || empty($rules) || empty($data)) {
if (empty($rules)) {
return true;
}

Expand All @@ -1474,6 +1480,8 @@ public function validate($data): bool
return true;
}

$this->ensureValidation();

$this->validation->reset()->setRules($rules, $this->validationMessages);

return $this->validation->run($data, null, $this->DBGroup);
Expand All @@ -1492,6 +1500,8 @@ public function getValidationRules(array $options = []): array
// ValidationRules can be either a string, which is the group name,
// or an array of rules.
if (is_string($rules)) {
$this->ensureValidation();

[$rules, $customErrors] = $this->validation->loadRuleGroup($rules);

$this->validationMessages += $customErrors;
Expand All @@ -1506,6 +1516,13 @@ public function getValidationRules(array $options = []): array
return $rules;
}

protected function ensureValidation(): void
{
if ($this->validation === null) {
$this->validation = Services::validation(null, false);
}
}

/**
* Returns the model's validation messages, so they
* can be used elsewhere, if needed.
Expand Down

0 comments on commit d6adeb3

Please sign in to comment.