Skip to content

Commit

Permalink
Make storage name confiugrable on Entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Apr 2, 2012
1 parent f207bee commit 17f6059
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/Doctrine/KeyValueStore/Mapping/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function loadMetadataForClass($className, ClassMetadata $metadata)
if (!$entityAnnot) {
throw new \InvalidArgumentException($metadata->name . " is not a valid key-value-store entity.");
}
$metadata->storageName = $entityAnnot->storageName;

// Evaluate annotations on properties/fields
foreach ($class->getProperties() as $property) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Doctrine/KeyValueStore/Mapping/Annotations/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
*/
class Entity
{
/**
* @var string
*/
public $storageName;
}

3 changes: 3 additions & 0 deletions lib/Doctrine/KeyValueStore/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class ClassMetadata implements BaseClassMetadata
{
public $name;
public $storageName;
public $fields = array();
public $identifier = array();
public $isCompositeKey = false;
Expand All @@ -36,6 +37,8 @@ class ClassMetadata implements BaseClassMetadata
public function __construct($className)
{
$this->name = $className;
$parts = explode("\\", $className);
$this->storageName = end($parts);
}

public function mapIdentifier($fieldName)
Expand Down
16 changes: 7 additions & 9 deletions lib/Doctrine/KeyValueStore/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function reconsititute($className, $key)
{
$class = $this->cmf->getMetadataFor($className);
$id = $this->idHandler->normalizeId($class, $key);
$data = $this->storageDriver->find($className, $id);
$data = $this->storageDriver->find($class->storageName, $id);

$object = $this->tryGetById($id);
if ( ! $object) {
Expand All @@ -61,10 +61,6 @@ public function reconsititute($className, $key)
$oid = spl_object_hash($object);
$this->originalData[$oid] = $data;

if ( ! isset($data['php_class']) || ! ($object instanceof $data['php_class'])) {
throw new \RuntimeException("Trying to reconstitute " . $data['php_class'] . " but a " . $className . " was requested.");
}

foreach ($data as $property => $value) {
if (isset($class->reflFields[$property])) {
$class->reflFields[$property]->setValue($object, $value);
Expand Down Expand Up @@ -154,10 +150,11 @@ private function processIdentityMap()
continue;
}

$changeSet = $this->computeChangeSet($this->cmf->getMetadataFor(get_class($object)), $object);
$metadata = $this->cmf->getMetadataFor(get_class($object));
$changeSet = $this->computeChangeSet($metadata, $object);

if ($changeSet) {
$this->storageDriver->update(get_class($object), $this->identifiers[$hash], $changeSet);
$this->storageDriver->update($metadata->storageName, $this->identifiers[$hash], $changeSet);

if ($this->storageDriver->supportsPartialUpdates()) {
$this->originalData[$hash] = array_merge($this->originalData[$hash], $changeSet);
Expand All @@ -183,7 +180,7 @@ private function processInsertions()
$oid = spl_object_hash($object);
$idHash = $this->idHandler->hash($id);

$this->storageDriver->insert(get_class($object), $id, $data);
$this->storageDriver->insert($class->storageName, $id, $data);

$this->originalData[$oid] = $data;
$this->identifiers[$oid] = $id;
Expand All @@ -194,11 +191,12 @@ private function processInsertions()
private function processDeletions()
{
foreach ($this->scheduledDeletions as $object) {
$class = $this->cmf->getMetadataFor(get_class($object));
$oid = spl_object_hash($object);
$id = $this->identifiers[$oid];
$idHash = $this->idHandler->hash($id);

$this->storageDriver->delete(get_class($object), $id);
$this->storageDriver->delete($class->storageName, $id);

unset($this->identifiers[$oid], $this->originalData[$oid], $this->identityMap[$idHash]);
}
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</filter>

<php>
<var name="DOCTRINE_KEYVALUE_AZURE_AUTHSCHEMA" value="shared" />
<var name="DOCTRINE_KEYVALUE_AZURE_AUTHSCHEMA" value="sharedlite" />
<var name="DOCTRINE_KEYVALUE_AZURE_NAME" value="" />
<var name="DOCTRINE_KEYVALUE_AZURE_KEY" value="" />
</php>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testPersistAndRetrieveItem()

public function testRetrieveItem()
{
$this->populate(1, array('id' => 1, 'headline' => 'test', 'body' => 'tset', 'foo' => 'bar', 'php_class' => __NAMESPACE__ . '\\Post'));
$this->populate(1, array('id' => 1, 'headline' => 'test', 'body' => 'tset', 'foo' => 'bar', 'php_class' => 'post'));

$post = $this->manager->find(__NAMESPACE__ . '\\Post', 1);

Expand All @@ -82,14 +82,6 @@ public function testRetrieveItem()
$this->assertSame($post, $post2);
}

public function testRetrieveWrongClass()
{
$this->populate(1, array('id' => 1, 'headline' => 'test', 'body' => 'tset', 'foo' => 'bar', 'php_class' => 'stdClass'));

$this->setExpectedException("RuntimeException", "Trying to reconstitute");
$post = $this->manager->find(__NAMESPACE__ . '\\Post', 1);
}

public function testUpdateClass()
{
$post = new Post();
Expand All @@ -105,7 +97,7 @@ public function testUpdateClass()

$this->manager->flush();

$this->assertEquals(array('id' => 1, 'headline' => 'asdf', 'body' => 'bar', 'text' => 'baz', 'php_class' => __NAMESPACE__ . '\\Post'), $this->find(1));
$this->assertEquals(array('id' => 1, 'headline' => 'asdf', 'body' => 'bar', 'text' => 'baz', 'php_class' => 'post'), $this->find(1));
}

public function testRemoveClass()
Expand All @@ -128,7 +120,7 @@ public function testRemoveClass()
}

/**
* @KVS\Entity
* @KVS\Entity(storageName="post")
*/
class Post
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testCrud()
}

switch ($GLOBALS['DOCTRINE_KEYVALUE_AZURE_AUTHSCHEMA']) {
case 'shared':
case 'sharedlite':
$auth = new SharedKeyLiteAuthorization(
$GLOBALS['DOCTRINE_KEYVALUE_AZURE_NAME'],
$GLOBALS['DOCTRINE_KEYVALUE_AZURE_KEY']
Expand Down

0 comments on commit 17f6059

Please sign in to comment.