From 770b76bb86034dcf5cb0a23e79d4d005c1286893 Mon Sep 17 00:00:00 2001 From: Marcelo Mira Date: Wed, 23 Mar 2016 00:14:13 -0300 Subject: [PATCH] Property aware callbacks Now every callback has access to same properties as can method. This allow the possibility of bringing context to transition callbacks. --- Listener/AbstractSubscriber.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Listener/AbstractSubscriber.php b/Listener/AbstractSubscriber.php index c8584c9..5347dd0 100644 --- a/Listener/AbstractSubscriber.php +++ b/Listener/AbstractSubscriber.php @@ -55,7 +55,7 @@ public function onSetInitialState(StateMachineEvent $event) public function onTestTransition(TransitionEvent $event) { $object = $event->getStateMachine()->getObject(); - $result = $this->callCallback($object, 'can', $event->getTransition()->getName()); + $result = $this->callCallback($object, 'can', $event); if ($result === false) { $event->reject(); @@ -68,13 +68,13 @@ public function onTestTransition(TransitionEvent $event) public function onPreTransition(TransitionEvent $event) { $object = $event->getStateMachine()->getObject(); - $this->callCallback($object, 'pre', $event->getTransition()->getName()); + $this->callCallback($object, 'pre', $event); } public function onPostTransition(TransitionEvent $event) { $object = $event->getStateMachine()->getObject(); - $this->callCallback($object, 'post', $event->getTransition()->getName()); + $this->callCallback($object, 'post', $event); } /** @@ -85,8 +85,9 @@ public function onPostTransition(TransitionEvent $event) * @param string $transitionName * @return mixed */ - protected function callCallback($object, $callbackPrefix, $transitionName) + protected function callCallback($object, $callbackPrefix, $event) { + $transitionName = is_string($event) ? 'initialize' : $event->getTransition()->getName(); $camelCasedName = Inflector::camelize($transitionName); $methodName = $callbackPrefix . $camelCasedName; @@ -98,6 +99,6 @@ protected function callCallback($object, $callbackPrefix, $transitionName) return; } - return call_user_func(array($this, $methodName), $object); + return call_user_func(array($this, $methodName), $object, $event->getProperties()); } }