1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Ember Db - An embeddable document database for php. |
4
|
|
|
* Copyright (C) 2016 Alexander During |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* @link http://github.com/alexanderduring/php-ember-db |
20
|
|
|
* @copyright Copyright (C) 2016 Alexander During |
21
|
|
|
* @license http://www.gnu.org/licenses GNU General Public License v3.0 |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace EmberDb; |
25
|
|
|
|
26
|
|
|
use EmberDb\Collection\Collection; |
27
|
|
|
use EmberDb\Filter\Filter; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The class DocumentManager is responsible for acting as an interface to the database. |
31
|
|
|
*/ |
32
|
|
|
class DocumentManager |
33
|
|
|
{ |
34
|
|
|
private $config = array(); |
35
|
|
|
|
36
|
|
|
private $collections = []; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public function getDatabasePath() |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
if (!array_key_exists('database', $this->config)) throw new Exception('The config has no entry with key "database".'); |
44
|
|
|
if (!array_key_exists('path', $this->config['database'])) throw new Exception('The config has no entry with key "database/path".'); |
45
|
|
|
} |
46
|
|
|
catch (Exception $exception) { |
47
|
|
|
throw new Exception('Missing config: '.$exception->getMessage()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->config['database']['path']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function setDatabasePath($path) |
56
|
|
|
{ |
57
|
|
|
$this->config['database']['path'] = $path; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function insert($collectionName, Document $document) |
63
|
|
|
{ |
64
|
|
|
$collection = $this->getCollection($collectionName); |
65
|
|
|
$collection->insert($document); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public function insertMany($collectionName, $documents) |
71
|
|
|
{ |
72
|
|
|
$collection = $this->getCollection($collectionName); |
73
|
|
|
$collection->insertMany($documents); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return Document[] |
80
|
|
|
*/ |
81
|
|
|
public function find(string $collectionName, array $filterData = []): array |
82
|
|
|
{ |
83
|
|
|
$filter = new Filter($filterData); |
84
|
|
|
|
85
|
|
|
return $this->getCollection($collectionName)->find($filter); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
public function remove($collectionName) |
91
|
|
|
{ |
92
|
|
|
$this->getCollection($collectionName)->remove(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
private function getCollection(string $collectionName): Collection |
98
|
|
|
{ |
99
|
|
|
if (!array_key_exists($collectionName, $this->collections)) { |
100
|
|
|
$collection = new Collection($collectionName, $this->getDatabasePath()); |
101
|
|
|
$this->collections[$collectionName] = $collection; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->collections[$collectionName]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|