-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootstrap.php
121 lines (79 loc) · 2.75 KB
/
bootstrap.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
include(__DIR__.'/vendor/autoload.php');
$app->module('detektivo')->extend([
'config' => function($key = null, $default = null) {
static $config;
if (!$config) {
$config = $this->app->retrieve('config/detektivo');
}
if ($key) {
return isset($config[$key]) ? $config[$key] : $default;
}
return $config;
},
'fields' => function($collection) {
static $collections;
if (!$collections) {
$collections = $this->config('collections', []);
}
if (isset($collections[$collection])) {
if ($collections[$collection] == '*') {
$fields = array_keys($entry);
} else {
$fields = $collections[$collection];
}
return $fields;
}
return null;
},
'storage' => function() {
static $storage;
if (!$storage) {
if ($config = $this->config()) {
$storage = \Detektivo\Storage\Manager::getStorage($config);
}
}
return $storage;
}
]);
$app->on('cockpit.bootstrap', function() {
$this->on('collections.removecollection', function($collection) {
$collections = $this->module('detektivo')->config('collections', []);
if (isset($collections[$collection])) {
$this->module('detektivo')->storage()->deleteIndex($collection);
}
});
$this->on('collections.save.after', function($collection, $entry, $isUpdate) {
if ($fields = $this->module('detektivo')->fields($collection)) {
$data = ['_id' => $entry['_id']];
foreach ($fields as $field) {
if (isset($entry[$field])) { $data[$field] = $entry[$field]; }
}
if (count($data)) {
$ret = $this->module('detektivo')->storage()->save($collection, $data);
}
}
});
$this->on('collections.remove.before', function($collection, $criteria) {
$collections = $this->module('detektivo')->config('collections', []);
if (!isset($collections[$collection])) return;
$entries = $this->module('collections')->find($collection, [
'fields' => ['_id' => true],
'filter' => $criteria
]);
if (!count($entries)) return;
$ids = [];
foreach ($entries as $entry) $ids[] = $entry['_id'];
$this->module('detektivo')->storage()->delete($collection, $ids);
});
});
// ADMIN
if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
include_once(__DIR__.'/admin.php');
}
// REST
if (COCKPIT_API_REQUEST) {
$app->on('cockpit.rest.init', function($routes) {
$routes['detektivo'] = 'Detektivo\\Controller\\RestApi';
});
}