Skip to content

Commit

Permalink
added callback to be executed after running node
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelog committed Mar 28, 2012
1 parent 1b71b4c commit f5bd8f7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/mg/PAGI/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ class Node
*/
private $_executeBeforeRun = null;

/**
* Execute after running this node.
* @var \Closure
*/
private $_executeAfterRun = null;

/**
* Execute after a validation has failed.
* @var \Closure
Expand Down Expand Up @@ -1153,6 +1159,19 @@ public function executeBeforeRun(\Closure $callback)
return $this;
}

/**
* Executes after running the node.
*
* @param \closure $callback
*
* @return Node
*/
public function executeAfterRun(\Closure $callback)
{
$this->_executeAfterRun = $callback;
return $this;
}

/**
* Executes after the 1st failed validation.
*
Expand Down Expand Up @@ -1227,6 +1246,11 @@ public function run()
$this->beforeOnInputFailed();
$callback($this);
}
if ($this->_executeAfterRun !== null) {
$callback = $this->_executeAfterRun;
$callback($this);
}

$this->logDebug($this);
return $this;
}
Expand Down
16 changes: 16 additions & 0 deletions test/node/Test_Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ public function can_execute_before_run()
$this->assertTrue($object->flag);
}

/**
* @test
*/
public function can_execute_after_run()
{
$object = new stdClass;
$object->flag = false;
$node = $this->createNode()
->executeAfterRun(function(Node $node) use ($object) {
$object->flag = true;
})
->run()
;
$this->assertTrue($object->flag);
}

/**
* @test
*/
Expand Down

0 comments on commit f5bd8f7

Please sign in to comment.