1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sokil\Mongo\Document; |
4
|
|
|
|
5
|
|
|
use Sokil\Mongo\Document; |
6
|
|
|
use Sokil\Mongo\Expression; |
7
|
|
|
|
8
|
|
|
class RevisionManager |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Suffix added to collection name to get name of revisions collection |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
const REVISION_COLLECTION_SUFFIX = '.revisions'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @var \Sokil\Mongo\Document |
20
|
|
|
*/ |
21
|
|
|
private $document; |
22
|
|
|
|
23
|
|
|
private $revisionsCollection; |
24
|
|
|
|
25
|
|
|
public function __construct(Document $document = null) |
26
|
|
|
{ |
27
|
|
|
$this->document = $document; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Start listening for changes in document |
32
|
|
|
*/ |
33
|
|
|
public function listen() |
34
|
|
|
{ |
35
|
|
|
$revisionsCollection = $this->getRevisionsCollection(); |
36
|
|
|
$document = $this->document; |
37
|
|
|
|
38
|
|
|
$createRevisionCallback = function () use ($revisionsCollection, $document) { |
39
|
|
|
// create new revision |
40
|
|
|
$revisionsCollection |
41
|
|
|
->createDocument() |
42
|
|
|
->setDocumentData($document->getOriginalData()) |
43
|
|
|
->save(); |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
$this->document->onBeforeUpdate($createRevisionCallback, PHP_INT_MAX); |
47
|
|
|
$this->document->onBeforeDelete($createRevisionCallback, PHP_INT_MAX); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return \Sokil\Mongo\Collection |
52
|
|
|
*/ |
53
|
|
|
private function getRevisionsCollection() |
54
|
|
|
{ |
55
|
|
|
if ($this->revisionsCollection) { |
56
|
|
|
return $this->revisionsCollection; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$collection = $this->document->getCollection(); |
60
|
|
|
$revisionsCollectionName = $collection->getName() . self::REVISION_COLLECTION_SUFFIX; |
61
|
|
|
|
62
|
|
|
$this->revisionsCollection = $collection |
63
|
|
|
->getDatabase() |
64
|
|
|
->map($revisionsCollectionName, array( |
65
|
|
|
'documentClass' => '\Sokil\Mongo\Revision', |
66
|
|
|
)) |
67
|
|
|
->getCollection($revisionsCollectionName); |
68
|
|
|
|
69
|
|
|
return $this->revisionsCollection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getRevisions($limit = null, $offset = null) |
73
|
|
|
{ |
74
|
|
|
$cursor = $this |
|
|
|
|
75
|
|
|
->getRevisionsCollection() |
76
|
|
|
->find() |
77
|
|
|
->where('__documentId__', $this->document->getId()); |
78
|
|
|
|
79
|
|
|
if ($limit) { |
80
|
|
|
$cursor->limit($limit); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($offset) { |
84
|
|
|
$cursor->skip($offset); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $cursor->findAll(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get revision by id |
92
|
|
|
* |
93
|
|
|
* @param int|string|\MongoId $id |
94
|
|
|
* @return \Sokil\Mongo\Revision |
95
|
|
|
*/ |
96
|
|
|
public function getRevision($id) |
97
|
|
|
{ |
98
|
|
|
return $this |
|
|
|
|
99
|
|
|
->getRevisionsCollection() |
100
|
|
|
->find() |
101
|
|
|
->byId($id) |
102
|
|
|
->findOne(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getRevisionsCount() |
106
|
|
|
{ |
107
|
|
|
return $this |
|
|
|
|
108
|
|
|
->getRevisionsCollection() |
109
|
|
|
->find() |
110
|
|
|
->where('__documentId__', $this->document->getId()) |
111
|
|
|
->count(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function clearRevisions() |
115
|
|
|
{ |
116
|
|
|
$documentId = $this->document->getId(); |
117
|
|
|
$this |
118
|
|
|
->getRevisionsCollection() |
119
|
|
|
->batchDelete(function (Expression $expression) use ($documentId) { |
120
|
|
|
/* @var $expression \Sokil\Mongo\Expression */ |
121
|
|
|
return $expression->where('__documentId__', $documentId); |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: