|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LRC\Repository; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Repository manager. |
|
7
|
|
|
*/ |
|
8
|
|
|
class RepositoryManager |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var array Registered repositories. |
|
12
|
|
|
*/ |
|
13
|
|
|
private $repositories = []; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Create a new repository and add it to the manager. |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $model Model class. |
|
20
|
|
|
* @param array $config Repository configuration. |
|
21
|
|
|
* |
|
22
|
|
|
* @return ManagedRepository The created repository. |
|
23
|
|
|
* |
|
24
|
|
|
* @throws RepositoryException If the requested repository type is not implemented. |
|
25
|
|
|
*/ |
|
26
|
13 |
|
public function createRepository($model, $config) |
|
27
|
|
|
{ |
|
28
|
|
|
$defaults = [ |
|
29
|
13 |
|
'type' => 'db-soft', |
|
30
|
13 |
|
'key' => 'id', |
|
31
|
|
|
'deleted' => 'deleted' |
|
32
|
13 |
|
]; |
|
33
|
13 |
|
$config = array_merge($defaults, $config); |
|
34
|
|
|
|
|
35
|
13 |
|
switch ($config['type']) { |
|
36
|
13 |
|
case 'db': |
|
37
|
11 |
|
$repo = new DbRepository($config['db'], $config['table'], $model, $config['key']); |
|
38
|
11 |
|
break; |
|
39
|
9 |
|
case 'db-soft': |
|
40
|
8 |
|
$repo = new SoftDbRepository($config['db'], $config['table'], $model, $config['deleted'], $config['key']); |
|
41
|
8 |
|
break; |
|
42
|
1 |
|
default: |
|
43
|
1 |
|
throw new RepositoryException("Repository type '" . $config['type'] . "' not implemented"); |
|
44
|
13 |
|
} |
|
45
|
|
|
|
|
46
|
13 |
|
$this->addRepository($repo); |
|
47
|
13 |
|
return $repo; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Register a repository with the manager. |
|
53
|
|
|
* |
|
54
|
|
|
* @param ManagedRepository $repository Manageable repository. |
|
55
|
|
|
* |
|
56
|
|
|
* @throws RepositoryException If the manager already contains a repository for the same model class. |
|
57
|
|
|
*/ |
|
58
|
13 |
|
public function addRepository($repository) |
|
59
|
|
|
{ |
|
60
|
13 |
|
$class = $repository->getModelClass(); |
|
61
|
13 |
|
if ($this->getByClass($class)) { |
|
62
|
2 |
|
throw new RepositoryException("The manager already contains a repository for the model class '$class'"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
13 |
|
$repository->setManager($this); |
|
66
|
13 |
|
$this->repositories[$class] = $repository; |
|
67
|
13 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get a registered repository by model class. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $class Model class. |
|
74
|
|
|
* |
|
75
|
|
|
* @return ManagedRepository|null Matching repository, or null if no repository found. |
|
76
|
|
|
*/ |
|
77
|
13 |
|
public function getByClass($class) |
|
78
|
|
|
{ |
|
79
|
13 |
|
return (array_key_exists($class, $this->repositories) ? $this->repositories[$class] : null); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Inject manager reference into a model capable of receiving it. |
|
85
|
|
|
* |
|
86
|
|
|
* @param object $model Model instance. |
|
87
|
|
|
*/ |
|
88
|
10 |
|
public function manageModel($model) |
|
89
|
|
|
{ |
|
90
|
10 |
|
if ($model instanceof ManagedModelInterface) { |
|
91
|
10 |
|
$model->setManager($this); |
|
92
|
10 |
|
} |
|
93
|
10 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|