Skip to content

Commit 2d6dd59

Browse files
author
Marco Bunge
committed
Update identity map and unit of work. Fix datatype issues
1 parent 305e48d commit 2d6dd59

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

src/AbstractMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(Connection $connection, Hydrator $hydrator = null)
7474
$this->doDefine();
7575

7676
$this->gateway = new Gateway($this->connection, $this->getTableName());
77-
$this->identityMap = new IdentityMap();
77+
$this->identityMap = $connection->loadIdentityMap($this->getEntityClass());
7878
}
7979

8080
/**

src/Connection.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ final class Connection extends \Doctrine\DBAL\Connection
3636
*/
3737
private $objectGraph;
3838

39+
/**
40+
* @var IdentityMap[]
41+
*/
42+
private $identityMap;
43+
3944
/**
4045
* @return null|string
4146
*/
@@ -74,6 +79,27 @@ public function getMapperLocator()
7479
return $this->mapperLocator;
7580
}
7681

82+
83+
/**
84+
* @param $classOrObject
85+
* @return IdentityMap
86+
*/
87+
public function loadIdentityMap($classOrObject)
88+
{
89+
if (is_object($classOrObject)) {
90+
$classOrObject = get_class($classOrObject);
91+
}
92+
93+
if (!is_string($classOrObject)) {
94+
throw new \InvalidArgumentException('Invalid data type ' . gettype($classOrObject));
95+
}
96+
if(!isset($this->identityMap[$classOrObject])){
97+
$this->identityMap[$classOrObject] = new IdentityMap();
98+
}
99+
100+
return $this->identityMap[$classOrObject];
101+
}
102+
77103
/**
78104
* @return UnitOfWork
79105
*/
@@ -94,19 +120,16 @@ public function getObjectGraph(){
94120
/**
95121
* @return array
96122
*/
97-
public function getIdentityStateGraph(){
98-
$mappers = $this->getMapperLocator()->getMapperMap();
123+
public function getEntityStateGraph(){
99124
$objectGraph = $this->getObjectGraph();
100125
$graph = [];
101126

102-
foreach ($mappers as $mapper){
103-
$identities = $mapper->getIdentityMap()->toArray();
127+
foreach ($this->identityMap as $identityMap){
128+
$identities = $identityMap->toArray();
104129

105130
foreach ($identities as $id => $object){
106-
$identities[$id] = $objectGraph->getState($object);
131+
$graph[get_class($object)][$id] = $objectGraph->getState($object);
107132
}
108-
109-
$graph[$mapper->getEntityClass()] = $identities;
110133
}
111134

112135
return $graph;

src/MapperLocator.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,4 @@ private function isValid($mapper)
170170
return '\\' . ltrim($mapper, '\\') instanceof Mapper;
171171
}
172172

173-
/**
174-
* INTERNAL: Mapping between entities and mappers
175-
* @return array
176-
*/
177-
public function getEntityMapperMap()
178-
{
179-
return $this->entityMapperMap;
180-
}
181-
182-
/**
183-
* INTERNAL: An in memory cache of mapper objects
184-
*
185-
* @return Mapper[]
186-
*/
187-
public function getMapperMap()
188-
{
189-
return $this->mapperMap;
190-
}
191-
192173
}

src/ObjectGraph.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public function getState($object){
5757
return self::UNHANDLED;
5858
}
5959

60+
/**
61+
* @return mixed
62+
*/
6063
public function toArray(){
6164
$copy = [];
6265

tests/IntegrationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testIntegration()
6363

6464
/** @var PostEntity $createdEntity */
6565
$createdEntity = $mapper->create($entity);
66-
$graph = $connection->getIdentityStateGraph();
66+
$graph = $connection->getEntityStateGraph();
6767

6868
$this->assertEquals($connection->lastInsertId(), $entity->getId());
6969
$this->assertEquals($entity, $createdEntity);
@@ -82,7 +82,7 @@ public function testIntegration()
8282

8383
/** @var PostEntity $updatedEntity */
8484
$updatedEntity = $mapper->update($entity);
85-
$graph = $connection->getIdentityStateGraph();
85+
$graph = $connection->getEntityStateGraph();
8686

8787
$this->assertEquals($entity, $updatedEntity);
8888
$this->assertEquals($updatedEntity->getContent(), 'FOO');
@@ -93,7 +93,7 @@ public function testIntegration()
9393

9494
$mapper->delete($entity);
9595

96-
$graph = $connection->getIdentityStateGraph();
96+
$graph = $connection->getEntityStateGraph();
9797

9898
if(true){
9999

0 commit comments

Comments
 (0)