-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathClient.php
93 lines (76 loc) · 2.02 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace MongoLite;
/**
* Client object.
*/
class Client {
/**
* @var array
*/
protected array $databases = [];
/**
* @var string
*/
protected string $path;
/**
* @var array
*/
protected array $options;
/**
* Constructor
*
* @param string $path - Pathname to database file or :memory:
* @param array $options
*/
public function __construct(string $path, array $options = []) {
$this->path = \rtrim($path, '\\');
$this->options = $options;
}
/**
* List Databases
*
* @return array List of database names
*/
public function listDBs(): array {
// Return all databases available in memory
if ($this->path === Database::DSN_PATH_MEMORY) {
return array_keys($this->databases);
}
// Return all databases available on disk
$databases = [];
foreach (new \DirectoryIterator($this->path) as $fileInfo) {
if ($fileInfo->getExtension() === 'sqlite') {
$databases[] = $fileInfo->getBasename('.sqlite');
}
}
return $databases;
}
/**
* Select Collection
*
* @param string $database
* @param string $collection
* @return Collection
*/
public function selectCollection(string $database, string $collection): Collection {
return $this->selectDB($database)->selectCollection($collection);
}
/**
* Select database
*
* @param string $name
* @return Database
*/
public function selectDB(string $name): Database {
if (!isset($this->databases[$name])) {
$this->databases[$name] = new Database(
$this->path === Database::DSN_PATH_MEMORY ? $this->path : sprintf('%s/%s.sqlite', $this->path, $name),
$this->options
);
}
return $this->databases[$name];
}
public function __get($database) {
return $this->selectDB($database);
}
}