1
|
|
|
<?php |
2
|
|
|
namespace vakata\orm; |
3
|
|
|
|
4
|
|
|
use \vakata\database\DBInterface; |
5
|
|
|
use \vakata\database\schema\TableQuery; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Manager ORM class |
9
|
|
|
*/ |
10
|
|
|
class Manager |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var DBInterface |
14
|
|
|
*/ |
15
|
|
|
protected $db; |
16
|
|
|
/** |
17
|
|
|
* @var DataMapper[] |
18
|
|
|
*/ |
19
|
|
|
protected $map; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create an instance |
23
|
|
|
* @param DBInterface $db the database schema |
24
|
|
|
*/ |
25
|
14 |
|
public function __construct(DBInterface $db) |
26
|
|
|
{ |
27
|
14 |
|
$this->db = $db; |
28
|
14 |
|
$this->map = []; |
29
|
14 |
|
} |
30
|
|
|
/** |
31
|
|
|
* Add a mapper for a specific table |
32
|
|
|
* |
33
|
|
|
* @param string $table the table name |
34
|
|
|
* @param DataMapper $mapper the mapper instance |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
14 |
|
public function registerMapper(string $table, DataMapper $mapper) |
38
|
|
|
{ |
39
|
14 |
|
$this->map[$table] = $mapper; |
40
|
14 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
/** |
43
|
|
|
* Add a generic mapper for a table name |
44
|
|
|
* |
45
|
|
|
* @param string $table the table name |
46
|
|
|
* @param callable $creator a callable to invoke when a new instance is needed |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
14 |
|
public function registerGenericMapper(string $table, callable $creator) |
50
|
|
|
{ |
51
|
14 |
|
return $this->registerMapper($table, new GenericDataMapper($this, $this->db, $table, $creator)); |
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* Add a generic mapper for a table name using a class name |
55
|
|
|
* |
56
|
|
|
* @param string $table the table name |
57
|
|
|
* @param string $class the class name to use when creating new instances |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
1 |
|
public function registerGenericMapperWithClassName(string $table, string $class) |
61
|
|
|
{ |
62
|
|
|
return $this->registerGenericMapper($table, function () use ($class) { |
63
|
1 |
|
return new $class; |
64
|
1 |
|
}); |
65
|
|
|
} |
66
|
|
|
/** |
67
|
|
|
* Is a mapper available for a given table name |
68
|
|
|
* |
69
|
|
|
* @param string $table the table name |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
|
|
public function hasMapper(string $table) |
73
|
|
|
{ |
74
|
|
|
return isset($this->map[$table]); |
75
|
|
|
} |
76
|
|
|
/** |
77
|
|
|
* Get a mapper for a table name, if a mapper is not found a new generic mapper is registered using \StdClass |
78
|
|
|
* |
79
|
|
|
* @param string $table |
80
|
|
|
* @return DataMapper |
81
|
|
|
*/ |
82
|
14 |
|
public function getMapper(string $table) |
83
|
|
|
{ |
84
|
14 |
|
if (!isset($this->map[$table])) { |
85
|
14 |
|
$this->registerGenericMapper($table, function () { |
86
|
|
|
return new class() extends \StdClass implements LazyLoadable { |
87
|
|
|
use LazyLoad; |
88
|
|
|
}; |
89
|
14 |
|
}); |
90
|
|
|
} |
91
|
14 |
|
return $this->map[$table]; |
92
|
|
|
} |
93
|
13 |
|
public function __call(string $table, array $args) |
94
|
|
|
{ |
95
|
13 |
|
$collection = $this->fromTable($table); |
96
|
13 |
|
return !count($args) ? |
97
|
13 |
|
$collection : |
98
|
13 |
|
$collection->find($args[0]); |
99
|
|
|
} |
100
|
|
|
/** |
101
|
|
|
* Get a repository from a table query |
102
|
|
|
* |
103
|
|
|
* @param TableQuery $query |
104
|
|
|
* @return Repository |
105
|
|
|
*/ |
106
|
14 |
|
public function fromQuery(TableQuery $query) : Repository |
107
|
|
|
{ |
108
|
14 |
|
return new DatabaseRepository($this->getMapper($query->getDefinition()->getName()), $query); |
109
|
|
|
} |
110
|
|
|
/** |
111
|
|
|
* Get a repository for a given table name |
112
|
|
|
* |
113
|
|
|
* @param string $table |
114
|
|
|
* @return Repository |
115
|
|
|
*/ |
116
|
13 |
|
public function fromTable(string $table) : Repository |
117
|
|
|
{ |
118
|
13 |
|
return $this->fromQuery($this->db->table($table)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|