Skip to content

Commit

Permalink
Changed the type for identifying a page flow from the absolute path o…
Browse files Browse the repository at this point in the history
…f a definition file to a page flow ID and the base directory of definition files. (Issue piece#4)
  • Loading branch information
iteman committed Sep 18, 2012
1 parent d13a26f commit 926f3a1
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 211 deletions.
13 changes: 5 additions & 8 deletions src/Piece/Flow/Continuation/ContinuationServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,12 @@ public function __construct(PageFlowRepository $pageFlowRepository, GC $gc = nul
* Adds a flow definition to the Continuation object.
*
* @param string $flowID
* @param string $source
* @param boolean $isExclusive
*/
public function addFlow($flowID, $source, $isExclusive = false)
public function addFlow($flowID, $isExclusive = false)
{
$this->flowDefinitions[$flowID] = array('source' => $source,
'isExclusive' => $isExclusive
);
$this->pageFlowRepository->add($source);
$this->flowDefinitions[$flowID] = array('isExclusive' => $isExclusive);
$this->pageFlowRepository->add($flowID);
}

/**
Expand Down Expand Up @@ -339,9 +336,9 @@ protected function startFlowExecution($payload)
throw new FlowNotFoundException("The flow ID [ {$this->activeFlowID} ] not found in the flow definitions.");
}

$flow = $this->pageFlowRepository->findByDefinitionFile($this->flowDefinitions[$this->activeFlowID]['source']);
$flow = $this->pageFlowRepository->findByID($this->activeFlowID);
if (is_null($flow)) {
throw new FlowNotFoundException(sprintf('The page flow for the definition file [ %s ] is not found in the repository.', $this->flowDefinitions[$this->activeFlowID]['source']));
throw new FlowNotFoundException(sprintf('The page flow for ID [ %s ] is not found in the repository.', $this->activeFlowID));
}

$flow->setActionInvoker($this->actionInvoker);
Expand Down
1 change: 0 additions & 1 deletion src/Piece/Flow/PageFlow/Definition17Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$treeBuilder->root('definition17')
->children()
->scalarNode('name')->defaultNull()->cannotBeEmpty()->end()
->scalarNode('firstState')->isRequired()->cannotBeEmpty()->end()
->arrayNode('viewState')
->isRequired()
Expand Down
14 changes: 12 additions & 2 deletions src/Piece/Flow/PageFlow/PageFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class PageFlow
const EVENT_PROTECTED = '__protected';

protected $fsm;
protected $id;
protected $views;
protected $attributes = array();
protected $lastState;
Expand All @@ -87,6 +88,15 @@ class PageFlow
*/
protected $actionInvoker;

/**
* @param string $id
* @since Method available since Release 2.0.0
*/
public function __construct($id)
{
$this->id = $id;
}

/**
* @return array
* @since Method available since Release 2.0.0
Expand Down Expand Up @@ -159,7 +169,7 @@ public function getView()
}

if (!array_key_exists($viewIndex, $this->views)) {
throw new InvalidTransitionException("A invalid transition detected. The state [ $viewIndex ] does not have a view. Maybe The state [ $viewIndex ] is an action state. Check the definition of the flow [ {$this->getID()} ].");
throw new InvalidTransitionException("A invalid transition detected. The state [ $viewIndex ] does not have a view. Maybe The state [ $viewIndex ] is an action state. Check the definition of the flow [ {$this->id} ].");
}

return $this->views[$viewIndex];
Expand All @@ -172,7 +182,7 @@ public function getView()
*/
public function getID()
{
return $this->fsm->getID();
return $this->id;
}

/**
Expand Down
19 changes: 16 additions & 3 deletions src/Piece/Flow/PageFlow/PageFlowFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,25 @@
class PageFlowFactory
{
/**
* @param string $definitionFile
* @var \Piece\Flow\PageFlow\PageFlowRegistry
*/
protected $pageFlowRegistry;

/**
* @param \Piece\Flow\PageFlow\PageFlowRegistry $pageFlowRegistry
*/
public function __construct(PageFlowRegistry $pageFlowRegistry)
{
$this->pageFlowRegistry = $pageFlowRegistry;
}

/**
* @param string $id
* @return \Piece\Flow\PageFlow\PageFlow
*/
public function create($definitionFile)
public function create($id)
{
$pageFlowGenerator = new PageFlowGenerator($definitionFile);
$pageFlowGenerator = new PageFlowGenerator($id, $this->pageFlowRegistry);
return $pageFlowGenerator->generate();
}
}
Expand Down
32 changes: 11 additions & 21 deletions src/Piece/Flow/PageFlow/PageFlowGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,26 @@ class PageFlowGenerator
protected $pageFlow;

/**
* @var \Stagehand\FSM\FSMBuilder
* @var \Piece\Flow\PageFlow\PageFlowRegistry
* @since Property available since Release 2.0.0
*/
protected $fsmBuilder;
protected $pageFlowRegistry;

/**
* @param string $definitionFile
* @var \Stagehand\FSM\FSMBuilder
* @since Property available since Release 2.0.0
*/
protected $definitionFile;
protected $fsmBuilder;

/**
* @param string $definitionFile
* @throws \Piece\Flow\PageFlow\FileNotFoundException
* @param string $id
* @param \Piece\Flow\PageFlow\PageFlowRegistry $pageFlowRegistry
*/
public function __construct($definitionFile)
public function __construct($id, PageFlowRegistry $pageFlowRegistry)
{
if (!file_exists($definitionFile)) {
throw new FileNotFoundException(sprintf('The flow definition file [ %s ] is not found.', $definitionFile));
}

$this->definitionFile = $definitionFile;
$this->pageFlow = new PageFlow();
$this->fsmBuilder = new FSMBuilder();
$this->pageFlow = new PageFlow($id);
$this->pageFlowRegistry = $pageFlowRegistry;
$this->fsmBuilder = new FSMBuilder($this->pageFlow->getID());
}

/**
Expand All @@ -98,12 +94,6 @@ public function generate()
throw new ProtectedStateException("The state [ {$definition['firstState']} ] cannot be used in flow definitions.");
}

if (is_null($definition['name'])) {
$this->fsmBuilder->setID(realpath($this->definitionFile));
} else {
$this->fsmBuilder->setID($definition['name']);
}

$this->fsmBuilder->setFirstState($definition['firstState']);

if (!empty($definition['lastState'])) {
Expand Down Expand Up @@ -277,7 +267,7 @@ protected function readDefinition()
$processor = new Processor();
return $processor->processConfiguration(
new Definition17Configuration(),
array('definition17' => Yaml::parse($this->definitionFile))
array('definition17' => Yaml::parse($this->pageFlowRegistry->getFileName($this->pageFlow->getID())))
);
}
}
Expand Down
88 changes: 88 additions & 0 deletions src/Piece/Flow/PageFlow/PageFlowRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
* PHP version 5.3
*
* Copyright (c) 2012 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 2012 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;

/**
* @package Piece_Flow
* @copyright 2012 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 PageFlowRegistry
{
/**
* @var string
*/
protected $baseDir;

/**
* @var string
*/
protected $extension;

/**
* @param string $baseDir
* @param string $extension
*/
public function __construct($baseDir, $extension)
{
$this->baseDir = $baseDir;
$this->extension = $extension;
}

/**
* @param string $id
* @return string
*/
public function getFileName($id)
{
return $this->baseDir . '/' . $id . $this->extension;
}
}

/*
* 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:
*/
42 changes: 21 additions & 21 deletions src/Piece/Flow/PageFlow/PageFlowRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,52 @@ class PageFlowRepository
protected $pageFlowFactory;

/**
* @var array
* @var \Piece\Flow\PageFlow\PageFlowRegistry
*/
protected $pageFlowRegistry;

/**
* @var arrayn
*/
protected $pageFlows = array();

/**
* @param \Piece\Flow\PageFlow\PageFlowRegistry $pageFlowRegistry
* @param \Piece\Flow\PageFlow\PageFlowCacheFactory $pageFlowCacheFactory
*/
public function __construct(PageFlowCacheFactory $pageFlowCacheFactory)
public function __construct(PageFlowRegistry $pageFlowRegistry, PageFlowCacheFactory $pageFlowCacheFactory)
{
$this->pageFlowRegistry = $pageFlowRegistry;
$this->pageFlowCacheFactory = $pageFlowCacheFactory;
$this->pageFlowFactory = new PageFlowFactory();
$this->pageFlowFactory = new PageFlowFactory($this->pageFlowRegistry);
}

/**
* @param string $definitionFile
* @param string $id
* @throws \Piece\Flow\PageFlow\FileNotFoundException
*/
public function add($definitionFile)
public function add($id)
{
$absoluteDefinitionFile = realpath($definitionFile);
if (!$absoluteDefinitionFile) {
throw new FileNotFoundException(sprintf('The page flow definition file [ %s ] is not found or not readable.', $definitionFile));
if (!file_exists($this->pageFlowRegistry->getFileName($id))) {
throw new FileNotFoundException(sprintf('The page flow definition file [ %s ] is not found.', $this->pageFlowRegistry->getFileName($id)));
}

$pageFlowCache = $this->pageFlowCacheFactory->create($absoluteDefinitionFile);
$pageFlowCache = $this->pageFlowCacheFactory->create($this->pageFlowRegistry->getFileName($id));
if (!$pageFlowCache->isFresh()) {
$pageFlowCache->write($this->pageFlowFactory->create($absoluteDefinitionFile));
$pageFlowCache->write($this->pageFlowFactory->create($id));
}

$this->pageFlows[$absoluteDefinitionFile] = $pageFlowCache;
$this->pageFlows[$id] = $pageFlowCache;
}

/**
* @param string $definitionFile
* @param string $id
* @return \Piece\Flow\PageFlow\PageFlow
* @throws \Piece\Flow\PageFlow\FileNotFoundException
*/
public function findByDefinitionFile($definitionFile)
public function findByID($id)
{
$absoluteDefinitionFile = realpath($definitionFile);
if (!$absoluteDefinitionFile) {
throw new FileNotFoundException(sprintf('The page flow definition file [ %s ] is not found or not readable.', $definitionFile));
}

if (array_key_exists($absoluteDefinitionFile, $this->pageFlows)) {
return $this->pageFlows[$absoluteDefinitionFile]->read();
if (array_key_exists($id, $this->pageFlows)) {
return $this->pageFlows[$id]->read();
} else {
return null;
}
Expand Down
Loading

0 comments on commit 926f3a1

Please sign in to comment.