Skip to content

Commit

Permalink
[PageFlow] introduced the ViewStateInterface and ActionStateInterface…
Browse files Browse the repository at this point in the history
… interfaces (Issue piece#4)
  • Loading branch information
iteman committed Aug 12, 2013
1 parent d5f820e commit 323ca61
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 25 deletions.
22 changes: 6 additions & 16 deletions src/Piece/Flow/PageFlow/PageFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
use Stagehand\FSM\State\StateInterface;
use Symfony\Component\HttpFoundation\ParameterBag;

use Piece\Flow\PageFlow\State\ViewStateInterface;

/**
* A web flow engine for handling page flows of web applications.
*
Expand Down Expand Up @@ -74,7 +76,6 @@ class PageFlow implements PageFlowInterface

protected $fsm;
protected $id;
protected $views = array();

/**
* @var \Symfony\Component\HttpFoundation\ParameterBag
Expand Down Expand Up @@ -108,21 +109,10 @@ public function __sleep()
return array(
'id',
'fsm',
'views',
'attributes',
);
}

/**
* @param string $stateID
* @param string $view
* @since Method available since Release 2.0.0
*/
public function addView($stateID, $view)
{
$this->views[$stateID] = $view;
}

/**
* @param \Stagehand\FSM\StateMachine\StateMachine $fsm
* @since Method available since Release 2.0.0
Expand All @@ -145,11 +135,11 @@ public function getCurrentView()
if (!$this->isActive()) return null;

$state = $this->isInFinalState() ? $this->getPreviousState() : $this->getCurrentState();
if (!array_key_exists($state->getStateID(), $this->views)) {
throw new IncompleteTransitionException(sprintf('An invalid transition detected. The state [ %s ] does not have a view. Maybe the state [ %s ] is an action state. Check the definition for [ %s ].', $state->getID(), $state->getID(), $this->getID()));
if ($state instanceof ViewStateInterface) {
return $state->getView();
} else {
throw new IncompleteTransitionException(sprintf('An invalid transition detected. The state [ %s ] does not have a view. Maybe the state [ %s ] is an action state. Check the definition for [ %s ].', $state->getStateID(), $state->getStateID(), $this->getID()));
}

return $this->views[ $state->getStateID() ];
}

public function getID()
Expand Down
45 changes: 36 additions & 9 deletions src/Piece/Flow/PageFlow/PageFlowGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@

namespace Piece\Flow\PageFlow;

use Stagehand\FSM\Event\DoEvent;
use Stagehand\FSM\Event\EntryEvent;
use Stagehand\FSM\Event\EventInterface;
use Stagehand\FSM\Event\ExitEvent;
use Stagehand\FSM\StateMachine\StateMachineBuilder;
use Stagehand\FSM\State\StateInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Yaml;

use Piece\Flow\PageFlow\State\ActionState;
use Piece\Flow\PageFlow\State\ViewState;

/**
* @package Piece_Flow
* @copyright 2007-2008, 2012-2013 KUBO Atsuhiro <[email protected]>
Expand Down Expand Up @@ -92,17 +98,26 @@ public function generate()
throw new ProtectedStateException("The state [ {$definition['firstState']} ] cannot be used in flow definitions.");
}

$states = array_merge(
$definition['viewState'],
$definition['actionState'],
empty($definition['lastState']) ? array() : array($definition['lastState'])
);
foreach ($states as $state) {
foreach ($definition['viewState'] as $state) {
if (in_array($state['name'], array(StateInterface::STATE_INITIAL, StateInterface::STATE_FINAL))) {
throw new ProtectedStateException("The state [ {$state['name']} ] cannot be used in flow definitions.");
}

$this->fsmBuilder->addState($state['name']);
$this->addState(new ViewState($state['name']));
}
foreach ($definition['actionState'] as $state) {
if (in_array($state['name'], array(StateInterface::STATE_INITIAL, StateInterface::STATE_FINAL))) {
throw new ProtectedStateException("The state [ {$state['name']} ] cannot be used in flow definitions.");
}

$this->addState(new ActionState($state['name']));
}
if (!empty($definition['lastState'])) {
if (in_array($definition['lastState']['name'], array(StateInterface::STATE_INITIAL, StateInterface::STATE_FINAL))) {
throw new ProtectedStateException("The state [ {$definition['lastState']['name']} ] cannot be used in flow definitions.");
}

$this->addState(new ViewState($definition['lastState']['name']));
}

if (empty($definition['initial'])) {
Expand All @@ -118,7 +133,7 @@ public function generate()
$this->fsmBuilder->setEndState($definition['lastState']['name'], PageFlowInterface::EVENT_END, $this->wrapAction($definition['final']));
}
$this->configureViewState($definition['lastState']);
$this->pageFlow->addView($definition['lastState']['name'], $definition['lastState']['view']);
$this->fsmBuilder->getStateMachine()->getState($definition['lastState']['name'])->setView($definition['lastState']['view']);
}

$this->configureViewStates($definition['viewState']);
Expand All @@ -139,7 +154,6 @@ protected function configureViewStates(array $states)
{
foreach ($states as $state) {
$this->configureViewState($state);
$this->pageFlow->addView($state['name'], $state['view']);
}
}

Expand Down Expand Up @@ -229,6 +243,7 @@ protected function wrapAction(array $action = null)
*/
protected function configureViewState(array $state)
{
$this->fsmBuilder->getStateMachine()->getState($state['name'])->setView($state['view']);
$this->configureState($state);
}

Expand Down Expand Up @@ -268,6 +283,18 @@ protected function readDefinition()
array('definition17' => Yaml::parse($this->pageFlowRegistry->getFileName($this->pageFlow->getID())))
);
}

/**
* @param \Stagehand\FSM\State\StateInterface $state
* @since Method available since Release 2.0.0
*/
protected function addState(StateInterface $state)
{
$state->setEntryEvent(new EntryEvent());
$state->setExitEvent(new ExitEvent());
$state->setDoEvent(new DoEvent());
$this->fsmBuilder->getStateMachine()->addState($state);
}
}

/*
Expand Down
62 changes: 62 additions & 0 deletions src/Piece/Flow/PageFlow/State/ActionState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
* PHP version 5.3
*
* Copyright (c) 2013 KUBO Atsuhiro <[email protected]>,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Piece_Flow
* @copyright 2013 KUBO Atsuhiro <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since File available since Release 2.0.0
*/

namespace Piece\Flow\PageFlow\State;

use Stagehand\FSM\State\State;

/**
* @package Piece_Flow
* @copyright 2013 KUBO Atsuhiro <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since Class available since Release 2.0.0
*/
class ActionState extends State implements ActionStateInterface
{
}

/*
* Local Variables:
* mode: php
* coding: iso-8859-1
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* indent-tabs-mode: nil
* End:
*/
62 changes: 62 additions & 0 deletions src/Piece/Flow/PageFlow/State/ActionStateInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
* PHP version 5.3
*
* Copyright (c) 2013 KUBO Atsuhiro <[email protected]>,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Piece_Flow
* @copyright 2013 KUBO Atsuhiro <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since File available since Release 2.0.0
*/

namespace Piece\Flow\PageFlow\State;

use Stagehand\FSM\State\StateInterface;

/**
* @package Piece_Flow
* @copyright 2013 KUBO Atsuhiro <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since Class available since Release 2.0.0
*/
interface ActionStateInterface extends StateInterface
{
}

/*
* Local Variables:
* mode: php
* coding: iso-8859-1
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* indent-tabs-mode: nil
* End:
*/
82 changes: 82 additions & 0 deletions src/Piece/Flow/PageFlow/State/ViewState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
* PHP version 5.3
*
* Copyright (c) 2013 KUBO Atsuhiro <[email protected]>,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package Piece_Flow
* @copyright 2013 KUBO Atsuhiro <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since File available since Release 2.0.0
*/

namespace Piece\Flow\PageFlow\State;

use Stagehand\FSM\State\State;

/**
* @package Piece_Flow
* @copyright 2013 KUBO Atsuhiro <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @since Class available since Release 2.0.0
*/
class ViewState extends State implements ViewStateInterface
{
/**
* @var string
*/
protected $view;

/**
* @param string $view
*/
public function setView($view)
{
$this->view = $view;
}

/**
* {@inheritDoc}
*/
public function getView()
{
return $this->view;
}
}

/*
* Local Variables:
* mode: php
* coding: iso-8859-1
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* indent-tabs-mode: nil
* End:
*/
Loading

0 comments on commit 323ca61

Please sign in to comment.