forked from forceworkbench/forceworkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkbenchContext.php
More file actions
298 lines (245 loc) · 8.69 KB
/
Copy pathWorkbenchContext.php
File metadata and controls
298 lines (245 loc) · 8.69 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
require_once 'ConnectionConfiguration.php';
require_once 'PartnerConnectionProvider.php';
require_once 'MetadataConnectionProvider.php';
require_once 'ApexConnectionProvider.php';
require_once 'AsyncBulkConnectionProvider.php';
require_once 'RestDataConnectionProvider.php';
require_once 'SfdcVersionsProvider.php';
require_once 'UserInfoProvider.php';
require_once 'DescribeGlobalProvider.php';
require_once 'DescribeSObjectsProvider.php';
class WorkbenchContext {
// namespace for $_SESSION instance of $this and keyed values in $_REQUEST
const INSTANCE = "WORKBENCH_CONTEXT";
// request keys
const CACHE = "CACHE";
const ALL_CONNECTIONS = 'ALL_CONNECTIONS';
const HAS_DEFAULT_OBJECT_CHANGED = "HAS_DEFAULT_OBJECT_CHANGED";
const REQUEST_START_TIME = "REQUEST_START_TIME";
// cache keys
const PARTNER = "PARTNER";
const METADATA = "METADATA";
const ASYNC_BULK = "ASYNC_BULK";
const APEX = "APEX";
const REST_DATA = "REST_DATA";
const SFDC_VERSIONS = "SFDC_VERSIONS";
const USER_INFO = "USER_INFO";
const DESCRIBE_GLOBAL = "DESCRIBE_GLOBAL";
const DESCRIBE_SOBJECTS = "DESCRIBE_SOBJECTS";
// session-based instance fields
private $connConfig;
private $cache;
private $defaultObject;
private $sfdcUiSidLikelySet;
private $agreedToTerms;
/**
* @static
* @return bool true if Workbench Context is established
*/
static function isEstablished() {
return isset($_SESSION[self::INSTANCE]);
}
/**
* Establishes a new Workbench Context.
*
* @static
* @param ConnectionConfiguration $connConfig
* @return void
*/
static function establish(ConnectionConfiguration $connConfig) {
if (self::isEstablished()) {
throw new Exception("Workbench session already established. Call get() or release() instead.");
}
$_SESSION[self::INSTANCE] = new WorkbenchContext($connConfig);
}
/**
* Gets the current Workbench Context
*
* @static
* @return WorkbenchContext
*/
static function get() {
if (isset($_SESSION[self::INSTANCE])) {
return $_SESSION[self::INSTANCE];
}
throw new Exception("Workbench Context not yet established");
}
private function __construct(ConnectionConfiguration $connConfig) {
if ($connConfig->getHost() == null) {
throw new Exception("Host must be set to establish Workbench Context.");
}
if ($connConfig->getApiVersion() == null) {
throw new Exception("API Version must be set to establish Workbench Context.");
}
$this->connConfig = $connConfig;
$this->initializeCache();
$this->defaultObject = false;
$this->defaultObjectChanged = false;
$this->sfdcUiSidLikelySet = false;
$this->agreedToTerms = false;
}
function login($username, $password, $orgId, $portalId) {
if ($orgId != null || $portalId != null) {
$this->getPartnerConnection()->setLoginScopeHeader(new LoginScopeHeader($orgId, $portalId));
}
$loginResult = $this->getPartnerConnection()->login($username, $password);
$this->connConfig->applyLoginResult($loginResult);
unset($_REQUEST[self::INSTANCE][self::ALL_CONNECTIONS]);
}
function release() {
unset($_SESSION[self::INSTANCE]);
unset($_REQUEST[self::INSTANCE]);
}
function getConnConfig() {
return $this->connConfig;
}
function isLoggedIn() {
return $this->connConfig->getSessionId() != null;
}
function getSessionId() {
return $this->connConfig->getSessionId();
}
function getHost() {
return $this->connConfig->getHost();
}
function setApiVersion($apiVersion) {
$this->connConfig->setApiVersion($apiVersion);
unset($_REQUEST[self::INSTANCE][self::ALL_CONNECTIONS]);
}
/**
* @param $minVersion
* @return bool
*/
function isApiVersionAtLeast($minVersion) {
return $this->getApiVersion() >= $minVersion;
}
function getApiVersion() {
return $this->connConfig->getApiVersion();
}
function isSecure() {
return $this->connConfig->isSecure();
}
// TODO: if this becomes too many things, should we make the context observable?
function beginRequestHook() {
if (isset($_REQUEST[self::INSTANCE][self::REQUEST_START_TIME])) {
throw new Exception("beginRequestHook() should not be called more than once per request");
}
$_REQUEST[self::INSTANCE][self::REQUEST_START_TIME] = microtime(true);
if (isset($_REQUEST['default_object'])) {
$this->setDefaultObject($_REQUEST['default_object']);
}
}
function isRequestStartTimeSet() {
return isset($_REQUEST[self::INSTANCE][self::REQUEST_START_TIME]);
}
function getRequestProcessingTime() {
return microtime(true) - $_REQUEST[self::INSTANCE][self::REQUEST_START_TIME];
}
function setDefaultObject($defaultObject) {
if ($defaultObject != $this->defaultObject) {
$_REQUEST[self::INSTANCE][self::HAS_DEFAULT_OBJECT_CHANGED] = true;
}
return $this->defaultObject = $defaultObject;
}
function getDefaultObject() {
return $this->defaultObject;
}
function hasDefaultObjectChanged() {
return isset($_REQUEST[self::INSTANCE][self::HAS_DEFAULT_OBJECT_CHANGED])
&& $_REQUEST[self::INSTANCE][self::HAS_DEFAULT_OBJECT_CHANGED];
}
function getObjectTypeByKeyPrefixOrId($keyPrefixOrId) {
$keyPrefix = substr($keyPrefixOrId, 0, 3);
$describeGlobal = $this->describeGlobal();
return isset($describeGlobal->byKeyPrefix[$keyPrefix])
? $describeGlobal->byKeyPrefix[$keyPrefix]
: null;
}
function setIsUiSessionLikelySet($sfdcUiSidLikelySet) {
$this->sfdcUiSidLikelySet = $sfdcUiSidLikelySet;
}
function agreeToTerms() {
$this->agreedToTerms = true;
}
function hasAgreedToTerms() {
return $this->agreedToTerms;
}
/**
* @return bool
*/
function isUiSessionLikelySet() {
return $this->sfdcUiSidLikelySet;
}
// CACHING & CONNECTIONS
private function initializeCache() {
$this->cache[self::PARTNER] = new PartnerConnectionProvider(self::PARTNER);
$this->cache[self::METADATA] = new MetadataConnectionProvider(self::METADATA);
$this->cache[self::APEX] = new ApexConnectionProvider(self::APEX);
$this->cache[self::ASYNC_BULK] = new AsyncBulkConnectionProvider(self::ASYNC_BULK);
$this->cache[self::REST_DATA] = new RestDataConnectionProvider(self::REST_DATA);
$this->cache[self::USER_INFO] = new UserInfoProvider(self::USER_INFO);
$this->cache[self::SFDC_VERSIONS] = new CurrentAppVersionProvider(self::SFDC_VERSIONS);
$this->cache[self::DESCRIBE_GLOBAL] = new DescribeGlobalProvider(self::DESCRIBE_GLOBAL);
$this->cache[self::DESCRIBE_SOBJECTS] = new DescribeSObjectsProvider(self::DESCRIBE_SOBJECTS);
}
function clearCache() {
$this->initializeCache();
}
private function &getCacheableValue($cacheKey, $args = null) {
return $this->cache[$cacheKey]->get($args);
}
/**
* @param $type
* @return AbstractConnectionProvider
*/
private function getConnection($type) {
return $this->getCacheableValue($type, $this->connConfig);
}
/**
* @return SforcePartnerClient
*/
function getPartnerConnection() {
return $this->getConnection(self::PARTNER);
}
/**
* @return SforceMetadataClient
*/
function getMetadataConnection() {
return $this->getConnection(self::METADATA);
}
/**
* @return SforceApexClient
*/
function getApexConnection() {
return $this->getConnection(self::APEX);
}
/**
* @return BulkApiClient
*/
function getAsyncBulkConnection() {
return $this->getConnection(self::ASYNC_BULK);
}
/**
* @return RestApiClient
*/
function getRestDataConnection() {
return $this->getConnection(self::REST_DATA);
}
function getSfdcVersions() {
return $this->getCacheableValue(self::SFDC_VERSIONS);
}
function getCurrentSfdcVersion() {
return end($this->getCacheableValue(self::SFDC_VERSIONS));
}
function getUserInfo() {
return $this->getCacheableValue(self::USER_INFO);
}
function describeGlobal() {
return $this->getCacheableValue(self::DESCRIBE_GLOBAL);
}
function describeSObjects($sObjectTypes) {
return $this->getCacheableValue(self::DESCRIBE_SOBJECTS, $sObjectTypes);
}
}
?>