1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sokil\Mongo\Collection; |
4
|
|
|
|
5
|
|
|
class Definition |
6
|
|
|
{ |
7
|
|
|
const DEFAULT_COLLECTION_CLASS = '\Sokil\Mongo\Collection'; |
8
|
|
|
const DEFAULT_GRIDFS_CLASS = '\Sokil\Mongo\GridFS'; |
9
|
|
|
|
10
|
|
|
const LOCK_NONE = 'NONE'; |
11
|
|
|
const LOCK_OPTIMISTIC = 'OPTIMISTIC'; |
12
|
|
|
const LOCK_PESSIMISTIC = 'PESSIMISTIC'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Fully qualified collection class |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $class; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
* @var bool is collection GridFs |
24
|
|
|
*/ |
25
|
|
|
private $gridfs; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Fully qualified document class or callable that returns classname |
29
|
|
|
* @var string|callable |
30
|
|
|
*/ |
31
|
|
|
private $documentClass = '\Sokil\Mongo\Document'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool Using document versioning |
35
|
|
|
*/ |
36
|
|
|
private $versioning = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array List of relations |
40
|
|
|
*/ |
41
|
|
|
private $relations = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array Index definition |
45
|
|
|
*/ |
46
|
|
|
private $index = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Fully qualified expression class for custom query builder |
50
|
|
|
*/ |
51
|
|
|
private $expressionClass = '\Sokil\Mongo\Expression'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* List of behaviors, attached to every document |
55
|
|
|
*/ |
56
|
|
|
private $behaviors = array(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Number of documents to return in each batch of response |
60
|
|
|
*/ |
61
|
|
|
private $batchSize; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Instructs the driver to stop waiting for a response and throw a |
65
|
|
|
* MongoCursorTimeoutException after a set time |
66
|
|
|
* A timeout can be set at any time and will affect subsequent queries on |
67
|
|
|
* the cursor, including fetching more results from the database. |
68
|
|
|
* @link http://php.net/manual/en/mongocursor.timeout.php |
69
|
|
|
* @var int time in milliseconds |
70
|
|
|
*/ |
71
|
|
|
private $cursorClientTimeout; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Server-side timeout for a query |
75
|
|
|
* Specifies a cumulative time limit in milliseconds to be allowed |
76
|
|
|
* by the server for processing operations on the cursor. |
77
|
|
|
* @link http://php.net/manual/en/mongocursor.maxtimems.php |
78
|
|
|
* @var int time in milliseconds |
79
|
|
|
*/ |
80
|
|
|
private $cursorServerTimeout; |
81
|
|
|
|
82
|
|
|
private $regexp; |
83
|
|
|
|
84
|
|
|
private $capped; |
85
|
|
|
|
86
|
|
|
private $size; |
87
|
|
|
|
88
|
|
|
private $max; |
89
|
|
|
|
90
|
|
|
private $lock = self::LOCK_NONE; |
91
|
|
|
|
92
|
|
|
private $documentPool = true; |
93
|
|
|
|
94
|
|
|
private $options = array(); |
95
|
|
|
|
96
|
|
|
public function __construct(array $definition = null) |
97
|
|
|
{ |
98
|
|
|
if ($definition) { |
99
|
|
|
$this->merge($definition); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function __get($name) |
104
|
|
|
{ |
105
|
|
|
return $this->getOption($name); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function __set($name, $value) |
109
|
|
|
{ |
110
|
|
|
$this->setOption($name, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function merge(array $definition) |
114
|
|
|
{ |
115
|
|
|
foreach ($definition as $name => $value) { |
116
|
|
|
$this->setOption($name, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setOption($name, $value) |
123
|
|
|
{ |
124
|
|
|
$method = 'set' . $name; |
125
|
|
|
if (method_exists($this, $method)) { |
126
|
|
|
call_user_func(array($this, $method), $value); |
127
|
|
|
} elseif (property_exists($this, $name)) { |
128
|
|
|
$this->{$name} = $value; |
129
|
|
|
} else { |
130
|
|
|
$this->options[$name] = $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getOption($name) |
138
|
|
|
{ |
139
|
|
|
if (property_exists($this, $name)) { |
140
|
|
|
return $this->{$name}; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return isset($this->options[$name]) ? $this->options[$name] : null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setClass($class) |
147
|
|
|
{ |
148
|
|
|
$this->class = rtrim($class, '\\'); |
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getClass() |
153
|
|
|
{ |
154
|
|
|
if (!$this->class) { |
155
|
|
|
$this->class = $this->gridfs |
156
|
|
|
? self::DEFAULT_GRIDFS_CLASS |
157
|
|
|
: self::DEFAULT_COLLECTION_CLASS; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->class; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get fully qualified class name of expression for current collection instance |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getExpressionClass() |
169
|
|
|
{ |
170
|
|
|
return $this->expressionClass; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getMongoCollectionOptions() |
174
|
|
|
{ |
175
|
|
|
$result = array( |
176
|
|
|
'capped' => $this->capped, |
177
|
|
|
'size' => $this->size, |
178
|
|
|
'max' => $this->max, |
179
|
|
|
); |
180
|
|
|
if (isset($this->options['validator'])) { |
181
|
|
|
$result['validator'] = $this->options['validator']; |
182
|
|
|
} |
183
|
|
|
return $result; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getOptions() |
187
|
|
|
{ |
188
|
|
|
return array( |
189
|
|
|
'class' => $this->class, |
190
|
|
|
'gridfs' => $this->gridfs, |
191
|
|
|
'documentClass' => $this->documentClass, |
192
|
|
|
'versioning' => $this->versioning, |
193
|
|
|
'index' => $this->index, |
194
|
|
|
'relations' => $this->relations, |
195
|
|
|
'expressionClass' => $this->expressionClass, |
196
|
|
|
'behaviors' => $this->behaviors, |
197
|
|
|
'batchSize' => $this->batchSize, |
198
|
|
|
'regexp' => $this->regexp, |
199
|
|
|
'capped' => $this->capped, |
200
|
|
|
'size' => $this->size, |
201
|
|
|
'max' => $this->max, |
202
|
|
|
'lock' => $this->lock, |
203
|
|
|
'documentPool' => $this->documentPool, |
204
|
|
|
'cursorClientTimeout' => $this->cursorClientTimeout, |
205
|
|
|
'cursorServerTimeout' => $this->cursorServerTimeout, |
206
|
|
|
) + $this->options; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|