Skip to content

Commit a6a0593

Browse files
author
Marco Bunge
committed
Update identity map and unit of work. Fix datatype issues
1 parent 3559aae commit a6a0593

File tree

9 files changed

+636
-257
lines changed

9 files changed

+636
-257
lines changed

src/AbstractMapper.php

Lines changed: 152 additions & 164 deletions
Large diffs are not rendered by default.

src/Connection.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ final class Connection extends \Doctrine\DBAL\Connection
2626
*/
2727
private $prefix = null;
2828

29+
/**
30+
* @var MapperLocator
31+
*/
32+
private $mapperLocator;
33+
2934
/**
3035
* @return null|string
3136
*/
@@ -44,4 +49,31 @@ public function setPrefix($prefix)
4449
return $this;
4550
}
4651

52+
/**
53+
* @param $entityOrMapper
54+
* @return Mapper
55+
*/
56+
public function loadMapper($entityOrMapper){
57+
58+
return $this->getMapperLocator()->locate($entityOrMapper);
59+
}
60+
61+
/**
62+
* @return MapperLocator
63+
*/
64+
public function getMapperLocator()
65+
{
66+
if(null === $this->mapperLocator){
67+
$this->mapperLocator = new MapperLocator($this);
68+
}
69+
return $this->mapperLocator;
70+
}
71+
72+
/**
73+
* @return UnitOfWork
74+
*/
75+
public function createUnitOfWork(){
76+
return new UnitOfWork($this);
77+
}
78+
4779
}

src/ConnectionManager.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,21 @@
88

99
namespace Hawkbit\Storage;
1010

11-
12-
use Hawkbit\Storage\Connection as ConnectionWrapper;
1311
use Doctrine\DBAL\DBALException;
1412
use Doctrine\DBAL\DriverManager;
15-
use Doctrine\DBAL\Connection as Connection;
1613

1714
final class ConnectionManager
1815
{
1916

2017
const DEFAULT_CONNECTION = 'default';
2118

2219
/**
23-
* @var \Doctrine\DBAL\Connection[]
20+
* @var Connection[]
2421
*/
2522
protected $connections = [];
2623

2724
/**
28-
* @var \Doctrine\DBAL\Connection[]
25+
* @var Connection[]
2926
*/
3027
protected $previousConnections = [];
3128

@@ -65,39 +62,35 @@ public static function getInstance()
6562
* class from container.
6663
*
6764
* @param $definition
68-
* @return \Doctrine\DBAL\Connection|\Hawkbit\Storage\Connection
65+
* @return Connection
6966
*
7067
* @throws \Doctrine\DBAL\DBALException
7168
*/
7269
public static function create($definition)
7370
{
74-
// create connection from definition
75-
if ($definition instanceof Connection) {
76-
return $definition;
77-
}
78-
79-
// assume a valid service from IoC container
80-
// or assume a valid dsn and convert to connection array
81-
if (is_string($definition)) {
82-
$definition = ['url' => $definition];
83-
}
84-
85-
if (!is_array($definition)) {
86-
throw new DBALException('Unable to determine parameter array from definition');
87-
}
71+
if($definition instanceof Connection){
72+
$connection = $definition;
73+
}else{
74+
// assume a valid service from IoC container
75+
// or assume a valid dsn and convert to connection array
76+
if (is_string($definition)) {
77+
$definition = ['url' => $definition];
78+
}
8879

89-
if (!array_key_exists('wrapperClass', $definition)) {
90-
$definition['wrapperClass'] = ConnectionWrapper::class;
91-
}
80+
if (!is_array($definition)) {
81+
throw new DBALException('Unable to determine parameter array from definition');
82+
}
9283

93-
$connection = DriverManager::getConnection($definition);
84+
$definition['wrapperClass'] = Connection::class;
85+
$connection = DriverManager::getConnection($definition);
9486

95-
if (!($connection instanceof Connection)) {
96-
throw new \RuntimeException(sprintf('Connection needs to be an instance of %s', Connection::class));
87+
if (!($connection instanceof Connection)) {
88+
throw new \RuntimeException(sprintf('Connection needs to be an instance of %s', Connection::class));
89+
}
9790
}
9891

99-
//setup special configuration for blast connections
100-
if ($connection instanceof \Hawkbit\Storage\Connection) {
92+
//setup special configuration for connections
93+
if ($connection instanceof Connection) {
10194
if (array_key_exists('prefix', $definition)) {
10295
$connection->setPrefix($definition['prefix']);
10396
}
@@ -136,7 +129,7 @@ public function closeAll()
136129
/**
137130
* Get all connections
138131
*
139-
* @return \Doctrine\DBAL\Connection[]|\Hawkbit\Storage\Connection[]
132+
* @return Connection[]
140133
*/
141134
public function all()
142135
{
@@ -149,7 +142,7 @@ public function all()
149142
*
150143
* @see http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#getting-a-connection
151144
*
152-
* @param array|\Doctrine\DBAL\Connection|string $connection
145+
* @param array|Connection|string $connection
153146
* @param string $name
154147
*
155148
* @return $this
@@ -213,7 +206,7 @@ public function swapActiveConnection($name)
213206
*
214207
* @param $name
215208
*
216-
* @return \Doctrine\DBAL\Connection|\Hawkbit\Storage\Connection
209+
* @return Connection
217210
*
218211
* @throws \Doctrine\DBAL\DBALException
219212
*/

src/Hydrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @package Hawkbit\Storage
2121
*/
22-
final class Hydrator
22+
class Hydrator
2323
{
2424
/**
2525
* Map key-value array

src/IdentityMap.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 07.12.2016
6+
* Time: 07:35
7+
*/
8+
9+
namespace Hawkbit\Storage;
10+
11+
12+
use ArrayObject;
13+
use OutOfBoundsException;
14+
use SplObjectStorage;
15+
16+
class IdentityMap
17+
{
18+
/**
19+
* @var ArrayObject
20+
*/
21+
protected $idToObject;
22+
23+
/**
24+
* @var SplObjectStorage
25+
*/
26+
protected $objectToId;
27+
28+
public function __construct()
29+
{
30+
$this->objectToId = new SplObjectStorage();
31+
$this->idToObject = new ArrayObject();
32+
}
33+
/**
34+
* @param integer $id
35+
* @param mixed $object
36+
*/
37+
public function set($id, $object)
38+
{
39+
$this->idToObject[$id] = $object;
40+
$this->objectToId[$object] = $id;
41+
}
42+
/**
43+
* @param mixed $object
44+
* @throws OutOfBoundsException
45+
* @return integer
46+
*/
47+
public function getId($object)
48+
{
49+
if (false === $this->hasObject($object)) {
50+
throw new OutOfBoundsException();
51+
}
52+
53+
/** @var integer $id */
54+
$id = $this->objectToId[$object];
55+
return $id;
56+
}
57+
/**
58+
* @param integer $id
59+
* @return boolean
60+
*/
61+
public function hasId($id)
62+
{
63+
return isset($this->idToObject[$id]);
64+
}
65+
/**
66+
* @param mixed $object
67+
* @return boolean
68+
*/
69+
public function hasObject($object)
70+
{
71+
return isset($this->objectToId[$object]);
72+
}
73+
/**
74+
* @param string|int $id
75+
* @throws OutOfBoundsException
76+
* @return object
77+
*/
78+
public function getObject($id)
79+
{
80+
if (false === $this->hasId($id)) {
81+
throw new OutOfBoundsException();
82+
}
83+
return $this->idToObject[$id];
84+
}
85+
86+
/**
87+
* @param $object
88+
*/
89+
public function removeObject($object){
90+
if($this->hasObject($object)){
91+
$id = $this->getId($object);
92+
unset($this->objectToId[$object]);
93+
unset($this->idToObject[$id]);
94+
}
95+
}
96+
97+
/**
98+
* @param $id
99+
*/
100+
public function removeId($id){
101+
if($this->hasId($id)){
102+
$object = $this->getObject($id);
103+
unset($this->objectToId[$object]);
104+
unset($this->idToObject[$id]);
105+
}
106+
}
107+
}

src/Mapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getColumns();
5050
* Find entity by primary key
5151
*
5252
* @param [] $primaryKey
53-
* @return object[]
53+
* @return object[]|object
5454
*/
5555
public function find($primaryKey = []);
5656

0 commit comments

Comments
 (0)