Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
[TASK] Use custom hash for steps
Browse files Browse the repository at this point in the history
Prevents serialization of closures.
  • Loading branch information
romm committed Apr 29, 2019
1 parent d08b8bc commit 1c2636f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Classes/Condition/Processor/ConditionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function getActivationConditionTreeForValidator(Validator $validator)
*/
public function getActivationConditionTreeForStep(StepDefinition $stepDefinition)
{
$key = 'step-' . serialize($stepDefinition);
$key = 'step-' . $stepDefinition->hash();

if (false === array_key_exists($key, $this->stepTree)) {
$this->stepTree[$key] = $this->getConditionTree($stepDefinition->getActivation());
Expand All @@ -140,7 +140,7 @@ public function getActivationConditionTreeForStep(StepDefinition $stepDefinition
*/
public function getActivationConditionTreeForSubstep(SubstepDefinition $substep)
{
$key = 'substep-' . serialize($substep);
$key = 'substep-' . $substep->hash();

if (false === array_key_exists($key, $this->substepTree)) {
$this->substepTree[$key] = $this->getConditionTree($substep->getActivation());
Expand Down
30 changes: 30 additions & 0 deletions Classes/Form/Definition/Step/Step/StepDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,34 @@ public function hasDivergence()
{
return false === empty($this->divergence);
}

/**
* @return string
*/
public function hash()
{
return serialize([
$this->step,
(function () {
if (!$this->activation) {
return null;
}

return [
$this->activation->getExpression(),
$this->activation->getAllConditions(),
];
})(),
$this->next ? $this->next->hash() : null,
(function () {
if (!$this->divergence) {
return null;
}

return array_map(function (DivergenceStepDefinition $divergenceStepDefinition) {
return $divergenceStepDefinition->hash();
}, $this->divergence);
})(),
]);
}
}
31 changes: 31 additions & 0 deletions Classes/Form/Definition/Step/Step/Substep/SubstepDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
use Romm\Formz\Form\Definition\Condition\Activation;
use Romm\Formz\Form\Definition\Condition\ActivationInterface;
use Romm\Formz\Form\Definition\Step\Step\DivergenceStepDefinition;
use Romm\Formz\Form\Definition\Step\Step\Step;

class SubstepDefinition extends AbstractFormDefinitionComponent
Expand Down Expand Up @@ -136,4 +137,34 @@ public function hasDivergence()
{
return false === empty($this->divergence);
}

/**
* @return string
*/
public function hash()
{
return serialize([
$this->substep,
(function () {
if (!$this->activation) {
return null;
}

return [
$this->activation->getExpression(),
$this->activation->getAllConditions(),
];
})(),
$this->next ? $this->next->hash() : null,
(function () {
if (!$this->divergence) {
return null;
}

return array_map(function (DivergenceStepDefinition $divergenceStepDefinition) {
return $divergenceStepDefinition->hash();
}, $this->divergence);
})(),
]);
}
}

0 comments on commit 1c2636f

Please sign in to comment.