This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thruway.module
244 lines (206 loc) · 6.3 KB
/
thruway.module
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\thruway\Plugin\thruway\Utils;
use Drupal\thruway\Plugin\Type\ResourcePluginManager;
use Thruway\ClientSession;
use Thruway\Connection;
/**
* Copied from the rest bundle
* @todo need to make this actually work
* Implements hook_permission().
*/
function thruway_permission() {
$permissions = array();
/* @var $manager ResourcePluginManager */
$manager = Drupal::service('thruway.plugin.manager');
$resources = Drupal::config('thruway.settings')->get('resources');
if ($resources && $enabled = array_intersect_key($manager->getDefinitions(), $resources)) {
foreach ($enabled as $key => $resource) {
$plugin = $manager->getInstance(array('id' => $key));
// $permissions = array_merge($permissions, $plugin->permissions());
}
}
return $permissions;
}
/**
* Implements hook_cache_flush().
*/
function thruway_cache_flush() {
}
/**
* Implements hook_entity_delete
*
* @param $entity
*/
function thruway_entity_delete(EntityInterface $entity) {
$connection = Drupal::service('thruway.connection');
if ($session = $connection->getSession()) {
_thruway_publish_delete($session, $entity);
}
else {
$options = Drupal::config('thruway.settings')->get('options');
$options['authmethods'] = ["drupal.{$options["realm"]}"];
$options['onChallenge'] = '_thruway_on_challenge';
$connection = new Connection($options);
$connection->on(
'open',
function (ClientSession $session) use ($connection, $entity) {
_thruway_publish_delete($session, $entity)->then(
function () use ($connection) {
$connection->close();
},
function ($error) use ($connection) {
// publish failed
$connection->close();
echo "Publish Error {$error}\n";
}
);
}
);
$connection->open();
}
}
function _thruway_publish_delete(ClientSession $session, EntityInterface $entity) {
return $session->publish(
"entity.{$entity->getEntityTypeId()}.{$entity->bundle()}.remove",
[Drupal::service('serializer')->serialize($entity, "array")],
[],
["acknowledge" => TRUE]
);
}
/**
* Implements hook_entity_update
*
* @param $entity
*
*/
function thruway_entity_update(EntityInterface $entity) {
$originalArray = Drupal::service('serializer')->serialize($entity->original, "array");
$entityArray = Drupal::service('serializer')->serialize($entity, "array");
//Get the changes
$updatedValues = arrayRecursiveDiff($entityArray, $originalArray);
//Get the entire field of the items that have changed
foreach($updatedValues as $key => $value){
$updatedValues[$key] = $entityArray[$key];
}
$updatedValues["uuid"] = $entityArray["uuid"];
$connection = Drupal::service('thruway.connection');
if ($session = $connection->getSession()) {
_thruway_publish_update($session, $updatedValues, $entity);
}
else {
$options = Drupal::config('thruway.settings')->get('options');
$options['authmethods'] = ["drupal.{$options["realm"]}"];
$options['onChallenge'] = '_thruway_on_challenge';
$connection = new Connection($options);
$connection->on(
'open',
function (ClientSession $session) use (
$connection,
$updatedValues,
$entity
) {
_thruway_publish_update($session, $updatedValues, $entity)->then(
function () use ($connection) {
$connection->close();
},
function ($error) use ($connection) {
// publish failed
$connection->close();
echo "Publish Error {$error}\n";
}
);
}
);
$connection->open();
}
}
function _thruway_publish_update(ClientSession $session, $updatedValues, EntityInterface $entity) {
return $session->publish(
"entity.{$entity->getEntityTypeId()}.{$entity->bundle()}.update",
[$updatedValues],
[],
["acknowledge" => TRUE]
);
}
/**
* Implements hook_entity_insert
*
* @param $entity
*
*/
function thruway_entity_insert(EntityInterface $entity) {
$connection = Drupal::service('thruway.connection');
if ($session = $connection->getSession()) {
_thruway_publish_add($session, $entity);
}
else {
$options = Drupal::config('thruway.settings')->get('options');
$options['authmethods'] = ["drupal.{$options["realm"]}"];
$options['onChallenge'] = '_thruway_on_challenge';
$connection = new Connection($options);
$connection->on(
'open',
function (ClientSession $session) use ($connection, $entity) {
_thruway_publish_add($session, $entity)->then(
function () use ($connection) {
$connection->close();
},
function ($error) use ($connection) {
// publish failed
$connection->close();
echo "Publish Error {$error}\n";
}
);
}
);
$connection->open();
}
}
function _thruway_publish_add(ClientSession $session, EntityInterface $entity) {
return $session->publish(
"entity.{$entity->getEntityTypeId()}.{$entity->bundle()}.add",
[Drupal::service('serializer')->serialize($entity::load($entity->id()), "array")],
[],
["acknowledge" => TRUE]
);
}
function _thruway_on_challenge($session, $method) {
$account = Drupal::currentUser();
if ($account->id() === 0) {
return "anonymous";
}
return Utils::getToken($account);
}
//function _thruway_reset_cache(EntityInterface $entity){
// $controller = \Drupal::entityManager()->getStorage($entity->getEntityTypeId());
// $controller->resetCache(array($entity->id()));
//}
/**
* @param $aArray1
* @param $aArray2
* @return array
*/
function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) {
$aReturn[$mKey] = $aRecursiveDiff;
}
}
else {
//checking for display is a hack. for some reason, it's flagged as changing, when it hasn't
if ($mValue != $aArray2[$mKey] && $mKey != "display") {
$aReturn[$mKey] = $mValue;
}
}
}
else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}