Skip to content

Commit

Permalink
[PageFlow][Continuation] Added the PageFlowRepository class to manage…
Browse files Browse the repository at this point in the history
… PageFlow objects. (Issue piece#4)
  • Loading branch information
iteman committed Sep 18, 2012
1 parent beb8e90 commit 11c9ba8
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 103 deletions.
30 changes: 13 additions & 17 deletions src/Piece/Flow/Continuation/ContinuationServer.php
Expand Up @@ -39,7 +39,7 @@

use Piece\Flow\Core\MethodInvocationException;
use Piece\Flow\PageFlow\ActionInvoker;
use Piece\Flow\PageFlow\PageFlowFactory;
use Piece\Flow\PageFlow\PageFlowRepository;

/**
* The continuation server.
Expand All @@ -53,7 +53,6 @@
class ContinuationServer
{
protected $flowDefinitions = array();
protected $cacheDirectory;
protected $flowExecutionTicketCallback;
protected $flowIDCallback;
protected $eventNameCallback;
Expand All @@ -71,27 +70,28 @@ class ContinuationServer
protected $actionInvoker;

/**
* @var \Piece\Flow\PageFlow\PageFlowFactory
* @var \Piece\Flow\PageFlow\PageFlowRepository
* @since Property available since Release 2.0.0
*/
protected $pageFlowFactory;
protected $pageFlowRepository;

private static $activeInstances = array();
private static $shutdownRegistered = false;

/**
* @param \Piece\Flow\PageFlow\PageFlowRepository $pageFlowRepository
* @param integer $enableGC
* @param integer $gcExpirationTime
*/
public function __construct($enableGC = false, $gcExpirationTime = 1440)
public function __construct(PageFlowRepository $pageFlowRepository, $enableGC = false, $gcExpirationTime = 1440)
{
if ($enableGC) {
$this->gc = new GC($gcExpirationTime);
$this->enableGC = true;
}

$this->flowExecution = new FlowExecution();
$this->pageFlowFactory = new PageFlowFactory();
$this->pageFlowRepository = $pageFlowRepository;
}

/**
Expand All @@ -106,6 +106,7 @@ public function addFlow($flowID, $source, $isExclusive = false)
$this->flowDefinitions[$flowID] = array('source' => $source,
'isExclusive' => $isExclusive
);
$this->pageFlowRepository->add($source);
}

/**
Expand Down Expand Up @@ -179,16 +180,6 @@ public function setFlowExecutionTicketCallback($callback)
$this->flowExecutionTicketCallback = $callback;
}

/**
* Sets the cache directory for the flow definitions.
*
* @param string $cacheDirectory
*/
public function setCacheDirectory($cacheDirectory)
{
$this->cacheDirectory = $cacheDirectory;
}

/**
* Shutdown the continuation server for next events.
*/
Expand Down Expand Up @@ -365,7 +356,12 @@ protected function startFlowExecution($payload)
throw new FlowNotFoundException("The flow ID [ {$this->activeFlowID} ] not found in the flow definitions.");
}

$flow = $this->pageFlowFactory->create($this->flowDefinitions[$this->activeFlowID]['source'], $this->actionInvoker);
$flow = $this->pageFlowRepository->findByDefinitionFile($this->flowDefinitions[$this->activeFlowID]['source']);
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']));
}

$flow->setActionInvoker($this->actionInvoker);

while (true) {
$flowExecutionTicket = $this->generateFlowExecutionTicket();
Expand Down
14 changes: 14 additions & 0 deletions src/Piece/Flow/PageFlow/PageFlow.php
Expand Up @@ -87,6 +87,20 @@ class PageFlow
*/
protected $actionInvoker;

/**
* @return array
* @since Method available since Release 2.0.0
*/
public function __sleep()
{
return array(
'fsm',
'views',
'attributes',
'lastState',
);
}

/**
* @param string $stateID
* @param string $view
Expand Down
140 changes: 140 additions & 0 deletions src/Piece/Flow/PageFlow/PageFlowCache.php
@@ -0,0 +1,140 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
* PHP version 5.3
*
* Copyright (c) 2012 KUBO Atsuhiro <kubo@iteman.jp>,
* 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 <kubo@iteman.jp>
* @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;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;

/**
* @package Piece_Flow
* @copyright 2012 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 2.0.0
*/
class PageFlowCache
{
/**
* @var string
*/
protected $cacheFile;

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

/**
* @var \Symfony\Component\Config\ConfigCache
*/
protected $configCache;

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

/**
* @param string $definitionFile
* @param string $cacheDir
* @param boolean $clearCacheOnDestruction
*/
public function __construct($definitionFile, $cacheDir, $clearCacheOnDestruction)
{
$this->definitionFile = $definitionFile;
$this->cacheFile = $cacheDir . '/' . sha1($this->definitionFile) . '.cache';
$this->configCache = new ConfigCache($this->cacheFile, true);
$this->clearCacheOnDestruction = $clearCacheOnDestruction;
}

public function __destruct()
{
if ($this->clearCacheOnDestruction && file_exists($this->cacheFile)) {
unlink($this->cacheFile);
unlink($this->cacheFile . '.meta');
}
}

/**
* @return boolean
*/
public function isFresh()
{
return $this->configCache->isFresh();
}

/**
* @return \Piece\Flow\PageFlow\PageFlow
*/
public function read()
{
return unserialize(serialize(unserialize(require $this->cacheFile)));
}

/**
* @param \Piece\Flow\PageFlow\PageFlow $pageFlow
*/
public function write(PageFlow $pageFlow)
{
$pageFlowClass = new \ReflectionObject($pageFlow);
$this->configCache->write($this->createContents(addslashes(serialize($pageFlow))), array(
new FileResource($this->definitionFile),
new FileResource($pageFlowClass->getFileName()),
));
}

/**
* @param string $data
* @return string
*/
protected function createContents($data)
{
return '<?php return "' . $data . '";' . PHP_EOL;
}
}

/*
* 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:
*/
88 changes: 88 additions & 0 deletions src/Piece/Flow/PageFlow/PageFlowCacheFactory.php
@@ -0,0 +1,88 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */

/**
* PHP version 5.3
*
* Copyright (c) 2012 KUBO Atsuhiro <kubo@iteman.jp>,
* 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 <kubo@iteman.jp>
* @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 <kubo@iteman.jp>
* @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 PageFlowCacheFactory
{
/**
* @var string
*/
protected $cacheDir;

/**
* @var string
*/
protected $clearCacheOnDestruction = false;

/**
* @param string $cacheDir
* @param boolean $clearCacheOnDestruction
*/
public function __construct($cacheDir, $clearCacheOnDestruction = false)
{
$this->cacheDir = $cacheDir;
$this->clearCacheOnDestruction = $clearCacheOnDestruction;
}

/**
* @param string $definitionFile
* @return \Piece\Flow\PageFlow\PageFlowCache
*/
public function create($definitionFile)
{
return new PageFlowCache($definitionFile, $this->cacheDir, $this->clearCacheOnDestruction);
}
}

/*
* 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:
*/
8 changes: 2 additions & 6 deletions src/Piece/Flow/PageFlow/PageFlowFactory.php
Expand Up @@ -48,16 +48,12 @@ class PageFlowFactory
{
/**
* @param string $definitionFile
* @param \Piece\Flow\PageFlow\ActionInvoker $actionInvoker
* @return \Piece\Flow\PageFlow\PageFlow
*/
public function create($definitionFile, ActionInvoker $actionInvoker)
public function create($definitionFile)
{
$pageFlowGenerator = new PageFlowGenerator($definitionFile);
$pageFlow = $pageFlowGenerator->generate();
$pageFlow->setActionInvoker($actionInvoker);

return $pageFlow;
return $pageFlowGenerator->generate();
}
}

Expand Down

0 comments on commit 11c9ba8

Please sign in to comment.