Skip to content

Commit

Permalink
[Continuation][PageFlow] introduced the event system to dispatch syst…
Browse files Browse the repository at this point in the history
…em events to listeners (Issue piece#4)
  • Loading branch information
iteman committed Aug 22, 2013
1 parent 9784c3c commit 8fdb19f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
24 changes: 21 additions & 3 deletions src/Piece/Flow/Continuation/ContinuationServer.php
Expand Up @@ -4,7 +4,7 @@
/**
* PHP version 5.3
*
* Copyright (c) 2006-2008, 2012 KUBO Atsuhiro <kubo@iteman.jp>,
* Copyright (c) 2006-2008, 2012-2013 KUBO Atsuhiro <kubo@iteman.jp>,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -29,21 +29,23 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Piece_Flow
* @copyright 2006-2008, 2012 KUBO Atsuhiro <kubo@iteman.jp>
* @copyright 2006-2008, 2012-2013 KUBO Atsuhiro <kubo@iteman.jp>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since File available since Release 1.14.0
*/

namespace Piece\Flow\Continuation;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use Piece\Flow\PageFlow\ActionInvokerInterface;

/**
* The continuation server.
*
* @package Piece_Flow
* @copyright 2006-2008, 2012 KUBO Atsuhiro <kubo@iteman.jp>
* @copyright 2006-2008, 2012-2013 KUBO Atsuhiro <kubo@iteman.jp>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since Class available since Release 1.14.0
Expand All @@ -65,6 +67,12 @@ class ContinuationServer
*/
protected $continuationContextProvider;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
* @since Property available since Release 2.0.0
*/
protected $eventDispatcher;

/**
* @var \Piece\Flow\Continuation\PageFlowInstance
* @since Property available since Release 2.0.0
Expand Down Expand Up @@ -178,6 +186,15 @@ public function setContinuationContextProvider(ContinuationContextProvider $cont
$this->continuationContextProvider = $continuationContextProvider;
}

/**
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @since Method available since Release 2.0.0
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

/**
* @return \Piece\Flow\Continuation\PageFlowInstance
* @since Method available since Release 2.0.0
Expand Down Expand Up @@ -267,6 +284,7 @@ protected function createPageFlowInstance($payload)

$pageFlowInstance->setActionInvoker($this->actionInvoker);
$pageFlowInstance->setPayload($payload);
$pageFlowInstance->setEventDispatcher($this->eventDispatcher);

return $pageFlowInstance;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Piece/Flow/Continuation/PageFlowInstance.php
Expand Up @@ -37,6 +37,8 @@

namespace Piece\Flow\Continuation;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use Piece\Flow\PageFlow\ActionInvokerInterface;
use Piece\Flow\PageFlow\PageFlow;
use Piece\Flow\PageFlow\PageFlowInterface;
Expand Down Expand Up @@ -152,6 +154,15 @@ public function setPayload($payload)
{
$this->pageFlow->setPayload($payload);
}

/**
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @since Method available since Release 2.0.0
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->pageFlow->setEventDispatcher($eventDispatcher);
}
}

/*
Expand Down
39 changes: 33 additions & 6 deletions src/Piece/Flow/PageFlow/PageFlow.php
Expand Up @@ -42,8 +42,12 @@
namespace Piece\Flow\PageFlow;

use Stagehand\FSM\Event\EventInterface;
use Stagehand\FSM\Event\TransitionEventInterface;
use Stagehand\FSM\StateMachine\StateMachine;
use Stagehand\FSM\StateMachine\StateMachineEvent;
use Stagehand\FSM\StateMachine\StateMachineEvents;
use Stagehand\FSM\State\StateInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\ParameterBag;

use Piece\Flow\PageFlow\State\ViewStateInterface;
Expand Down Expand Up @@ -82,14 +86,18 @@ class PageFlow implements PageFlowInterface
*/
protected $attributes;

protected $receivedValidEvent;

/**
* @var \Piece\Flow\PageFlow\ActionInvokerInterface
* @since Property available since Release 2.0.0
*/
protected $actionInvoker;

/**
* @var \Stagehand\FSM\Event\TransitionEventInterface
* @since Property available since Release 2.0.0
*/
protected $lastTransitionEvent;

/**
* @param string $id
* @since Method available since Release 2.0.0
Expand Down Expand Up @@ -142,6 +150,28 @@ public function getActionInvoker()
return $this->actionInvoker;
}

/**
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @since Method available since Release 2.0.0
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$self = $this;
$eventDispatcher->addListener(
StateMachineEvents::EVENT_PROCESS,
function (StateMachineEvent $event) use ($self) {
if ($event->getStateMachine() === $self->fsm) {
if ($event->getEvent() instanceof TransitionEventInterface) {
$self->lastTransitionEvent = $event->getEvent();
} else {
$self->lastTransitionEvent = null;
}
}
}
);
$this->fsm->setEventDispatcher($eventDispatcher);
}

public function getCurrentView()
{
if (!$this->isActive()) return null;
Expand All @@ -164,7 +194,6 @@ public function getID()
*/
public function start()
{
$this->receivedValidEvent = true;
$this->fsm->start();
}

Expand All @@ -185,8 +214,6 @@ public function triggerEvent($eventID)
$eventID = self::EVENT_PROTECTED;
}

$this->receivedValidEvent = !is_null($this->fsm->getCurrentState()->getEvent($eventID));

$this->fsm->triggerEvent($eventID, false);

if ($this->fsm->getCurrentState()->isEndState()) {
Expand Down Expand Up @@ -232,7 +259,7 @@ public function isInFinalState()
*/
public function validateReceivedEvent()
{
return $this->receivedValidEvent;
return !is_null($this->lastTransitionEvent);
}

/**
Expand Down
16 changes: 13 additions & 3 deletions test/Piece/Flow/Continuation/ContinuationServerTest.php
Expand Up @@ -4,7 +4,7 @@
/**
* PHP version 5.3
*
* Copyright (c) 2006-2008, 2012 KUBO Atsuhiro <kubo@iteman.jp>,
* Copyright (c) 2006-2008, 2012-2013 KUBO Atsuhiro <kubo@iteman.jp>,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -29,22 +29,24 @@
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Piece_Flow
* @copyright 2006-2008, 2012 KUBO Atsuhiro <kubo@iteman.jp>
* @copyright 2006-2008, 2012-2013 KUBO Atsuhiro <kubo@iteman.jp>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since File available since Release 1.14.0
*/

namespace Piece\Flow\Continuation;

use Symfony\Component\EventDispatcher\EventDispatcher;

use Piece\Flow\PageFlow\EventContext;
use Piece\Flow\PageFlow\PageFlowCacheFactory;
use Piece\Flow\PageFlow\PageFlowRegistry;
use Piece\Flow\PageFlow\PageFlowRepository;

/**
* @package Piece_Flow
* @copyright 2006-2008, 2012 KUBO Atsuhiro <kubo@iteman.jp>
* @copyright 2006-2008, 2012-2013 KUBO Atsuhiro <kubo@iteman.jp>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since Class available since Release 1.14.0
Expand Down Expand Up @@ -114,6 +116,7 @@ public function startsPageFlowInstancesForAnExclusivePageFlow()
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -146,6 +149,7 @@ public function startsPageFlowInstancesForANonExclusivePageFlow()
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -179,6 +183,7 @@ public function startsPageFlowInstancesForMultiplePageFlows()
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -211,6 +216,7 @@ public function continuesAPageFlowInstance()
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -242,6 +248,7 @@ public function raisesAnExceptionWhenAnUnexpectedPageFlowIdIsSpecifiedForTheSeco
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -270,6 +277,7 @@ public function findsThePageFlowInstanceByAPageFlowId($exclusive)
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -310,6 +318,7 @@ public function raisesAnExceptionWhenThePageFlowInstanceHasExpired($expirationTi
$continuationServer = new ContinuationServer($pageFlowInstanceRepository, new GC($expirationTime, $clock));
$continuationServer->setActionInvoker(\Phake::mock('Piece\Flow\PageFlow\ActionInvokerInterface'));
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'Counter';
$this->eventID = null;
Expand Down Expand Up @@ -358,6 +367,7 @@ public function validatesTheLastReceivedEvent()
$continuationServer = new ContinuationServer($pageFlowInstanceRepository);
$continuationServer->setActionInvoker($this->createCounterActionInvoker());
$continuationServer->setContinuationContextProvider($this->continuationContextProvider);
$continuationServer->setEventDispatcher(new EventDispatcher());

$this->pageFlowID = 'CheckLastEvent';
$this->eventID = 'nonExistingEvent';
Expand Down

0 comments on commit 8fdb19f

Please sign in to comment.