Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Refactored Configuration into its own class, integrated IdConverterSt…
Browse files Browse the repository at this point in the history
…rategies.
  • Loading branch information
beberlei committed Apr 6, 2012
1 parent 867d0a7 commit 4fe8657
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 19 deletions.
117 changes: 117 additions & 0 deletions lib/Doctrine/KeyValueStore/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/*
* 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.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\KeyValueStore;

use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\KeyValueStore\Id\NullIdConverter;

/**
* Configure the behavior of the EntityManager
*
* @author Benjamin Eberlei <[email protected]>
*/
class Configuration
{
/**
* @param array
*/
private $config;

/**
* Get mapping driver implementation used with this configuration.
*
* @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
*/
public function getMappingDriverImpl()
{
if ( ! isset($this->config['mappingDriver'])) {
throw KeyValueStoreException::mappingDriverMissing();
}

return $this->config['mappingDriver'];
}

/**
* Set the mapping driver implementation.
*
* @param \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver $driver
* @return \Doctrine\KeyValueStore\Configuration
*/
public function setMappingDriverImpl(MappingDriver $driver)
{
$this->config['mappingDriver'] = $driver;
return $this;
}

/**
* Set the Metadata Mapping cache used with this configuration.
*
* @param \Doctrine\Common\Cache\Cache $cache
* @return \Doctrine\KeyValueStore\Configuration
*/
public function setMetadataCache(Cache $cache)
{
$this->config['metadataCache'] = $cache;
return $this;
}

/**
* Get the metadata mapping cache used with this configuration.
*
* @return \Doctrine\Common\Cache\Cache $cache
*/
public function getMetadataCache()
{
if ( ! isset($this->config['metadataCache'])) {
$this->config['metadataCache'] = new ArrayCache();
}

return $this->config['metadataCache'];
}

/**
* Set the ID Converter Strategy
*
* @param \Doctrine\KeyValueStore\Id\IdConverterStrategy
* @return \Doctrine\KeyValueStore\Configuration
*/
public function setIdConverterStrategy(IdConverterStrategy $strategy)
{
$this->config['idConverter'] = $strategy;
return $this;
}

/**
* Get the Id Converter strategy
*
* @return \Doctrine\KeyValueStore\Id\IdConverterStrategy
*/
public function getIdConverterStrategy()
{
if ( ! isset($this->config['idConverter'])) {
$this->config['idConverter'] = new NullIdConverter();
}

return $this->config['idConverter'];
}
}

41 changes: 36 additions & 5 deletions lib/Doctrine/KeyValueStore/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,29 @@ class EntityManager
*/
private $storageDriver;

public function __construct(Storage $storageDriver, Cache $cache, MappingDriver $mappingDriver, array $idConverters = array())
/**
* Create a new EntityManager
*
* @param Storage $storageDriver
* @param Configuration $config
*/
public function __construct(Storage $storageDriver, Configuration $config)
{
$cmf = new ClassMetadataFactory($mappingDriver);
$cmf->setCacheDriver($cache);
$cmf = new ClassMetadataFactory($config->getMappingDriverImpl());
$cmf->setCacheDriver($config->getMetadataCache());

$this->unitOfWork = new UnitOfWork($cmf, $storageDriver, $idConverters);
$this->unitOfWork = new UnitOfWork($cmf, $storageDriver, $config);
$this->storageDriver = $storageDriver;
}

public function find($className, $key, array $fields = null)
/**
* Find objects by key
*
* @param string $className
* @param string|array $key
* @return object
*/
public function find($className, $key)
{
return $this->unitOfWork->reconsititute($className, $key);
}
Expand All @@ -72,16 +85,34 @@ public function createRangeQuery($className, $partitionKey)
return new RangeQuery($this, $className, $partitionKey);
}

/**
* Persist new object in key value storage.
*
* @param object $object
* @return void
*/
public function persist($object)
{
$this->unitOfWork->scheduleForInsert($object);
}

/**
* Remove object
*
* @param object $object
* @return void
*/
public function remove($object)
{
$this->unitOfWork->scheduleForDelete($object);
}

/**
* Flush all outstanding changes from the managed object-graph into the
* key-value storage.
*
* @return void
*/
public function flush()
{
$this->unitOfWork->commit();
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/KeyValueStore/Id/IdConverterStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
interface IdConverterStrategy
{
function serialize($class, $id);
function unserialize($class, $id);
function serialize($class, $data);
function unserialize($class, $data);
}

8 changes: 4 additions & 4 deletions lib/Doctrine/KeyValueStore/Id/NullIdConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

class NullIdConverter implements IdConverterStrategy
{
public function serialize($class, $id)
public function serialize($class, $data)
{
return $id;
return $data;
}

public function unserialize($class, $id)
public function unserialize($class, $data)
{
return $id;
return $data;
}
}

29 changes: 29 additions & 0 deletions lib/Doctrine/KeyValueStore/KeyValueStoreException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* 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.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\KeyValueStore;

class KeyValueStoreException extends \Exception
{
static public function mappingDriverMissing()
{
return new self("No mapping driver was assigned to the configuration. Use \$config->setMappingDriverImpl()");
}
}

2 changes: 2 additions & 0 deletions lib/Doctrine/KeyValueStore/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ function delete($storageName, $key);
/**
* Find data at key
*
* Important note: The returned array does contain the identifier (again)!
*
* @param string $storageName
* @param array|string $key
* @return array
Expand Down
28 changes: 21 additions & 7 deletions lib/Doctrine/KeyValueStore/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,34 @@ class UnitOfWork
private $cmf;
private $storageDriver;
private $idHandler;

/**
* Serialized versions of the identifiers.
*
* This is the after {@see IdConverterStrategy#serialize} is called on the
* entity data.
*
* @var array
*/
private $identifiers;

private $originalData;
private $scheduledInsertions = array();
private $scheduledDeletions = array();
private $identityMap = array();
private $idConverter;

public function __construct($cmf, $storageDriver, $idConverter = null)
public function __construct($cmf, $storageDriver, $config = null)
{
$this->cmf = $cmf;
$this->storageDriver = $storageDriver;
$this->idConverter = $idConverter ?: new NullIdConverter();
$this->idConverter = $config->getIdConverterStrategy();
$this->idHandler = $storageDriver->supportsCompositePrimaryKeys() ?
new Id\CompositeIdHandler() :
new Id\SingleIdHandler();
}

public function tryGetById($id)
private function tryGetById($id)
{
$idHash = $this->idHandler->hash($id);
if (isset($this->identityMap[$idHash])) {
Expand Down Expand Up @@ -78,12 +88,15 @@ public function createEntity($class, $id, $data)
unset($data['php_class']);

$object = $this->tryGetById($id);
if ( ! $object) {
$object = $class->newInstance();
if ( $object) {
return $object;
}

$object = $class->newInstance();

$oid = spl_object_hash($object);
$this->originalData[$oid] = $data;
$data = $this->idConverter->unserialize($class, $data);

foreach ($data as $property => $value) {
if (isset($class->reflFields[$property])) {
Expand All @@ -93,7 +106,7 @@ public function createEntity($class, $id, $data)
}
}

$idHash = $this->idHandler->hash($id);
$idHash = $this->idHandler->hash($id);
$this->identityMap[$idHash] = $object;
$this->identifiers[$oid] = $id;

Expand Down Expand Up @@ -200,6 +213,7 @@ private function processInsertions()
foreach ($this->scheduledInsertions as $object) {
$class = $this->cmf->getMetadataFor(get_class($object));
$id = $this->idHandler->getIdentifier($class, $object);
$id = $this->idConverter->serialize($class, $id);

if ( ! $id) {
throw new \RuntimeException("Trying to persist entity that has no id.");
Expand All @@ -208,7 +222,7 @@ private function processInsertions()
$data = $this->getObjectSnapshot($class, $object);
$data['php_class'] = $class->name;

$oid = spl_object_hash($object);
$oid = spl_object_hash($object);
$idHash = $this->idHandler->hash($id);

$this->storageDriver->insert($class->storageName, $id, $data);
Expand Down
32 changes: 32 additions & 0 deletions tests/Doctrine/Tests/KeyValueStore/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Doctrine\Tests\KeyValueStore;

use Doctrine\KeyValueStore\Configuration;

class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
public function testNoMappingDriver()
{
$config = new Configuration();

$this->setExpectedException('Doctrine\KeyValueStore\KeyValueStoreException', 'No mapping driver was assigned to the configuration. Use $config->setMappingDriverImpl()');
$config->getMappingDriverImpl();
}

public function testDefaultCacheDriver()
{
$config = new Configuration();
$cache = $config->getMetadataCache();

$this->assertInstanceOf('Doctrine\Common\Cache\Cache', $cache);
}

public function testDefaultIdConverterStrategy()
{
$config = new Configuration();
$strategy = $config->getIdConverterStrategy();

$this->assertInstanceOf('Doctrine\KeyValueStore\Id\NullIdConverter', $strategy);
}
}

7 changes: 6 additions & 1 deletion tests/Doctrine/Tests/KeyValueStoreTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Doctrine\Tests;

use Doctrine\KeyValueStore\EntityManager;
use Doctrine\KeyValueStore\Configuration;
use Doctrine\KeyValueStore\Mapping\AnnotationDriver;
use Doctrine\KeyValueStore\Storage\DoctrineCacheStorage;
use Doctrine\Common\Cache\ArrayCache;
Expand All @@ -33,7 +34,11 @@ public function createManager($storage = null)

$reader = new \Doctrine\Common\Annotations\AnnotationReader();
$metadata = new AnnotationDriver($reader);
return new EntityManager($storage, $cache, $metadata);
$config = new Configuration();
$config->setMappingDriverImpl($metadata);
$config->setMetadataCache($cache);

return new EntityManager($storage, $config);
}
}

0 comments on commit 4fe8657

Please sign in to comment.