@@ -106,31 +106,48 @@ jsonld_set_document_loader('jsonld_default_secure_document_loader');
106
106
// set a default custom document loader
107
107
jsonld_set_document_loader('my_custom_doc_loader');
108
108
109
- // a custom loader that demonstrates checking a simple in-memory cache
110
- // before falling back to the default loader
109
+ // a custom loader that demonstrates using a simple in-memory mock for
110
+ // certain contexts before falling back to the default loader
111
111
// note: if you want to set this loader as the new default, you'll need to
112
112
// store the previous default in another variable first and access that inside
113
113
// the loader
114
- global $cache ;
115
- $cache = array('http://example.com/mycontext' => (object)array(
114
+ global $mocks ;
115
+ $mocks = array('http://example.com/mycontext' => (object)array(
116
116
'hombre' => 'http://schema.org/name'));
117
-
118
- function custom_load($url) {
119
- global $jsonld_default_load_document, $cache;
120
- if(isset($cache[$url])) {
117
+ function mock_load($url) {
118
+ global $jsonld_default_load_document, $mocks;
119
+ if(isset($mocks[$url])) {
121
120
// return a "RemoteDocument", it has these three properties:
122
121
return (object)array(
123
122
'contextUrl' => null,
124
- 'document' => $cache [$url],
123
+ 'document' => $mocks [$url],
125
124
'documentUrl' => $url);
126
125
}
127
126
// use default loader
128
127
return call_user_func($jsonld_default_load_document, $url);
129
128
}
130
129
131
- // use the custom loader for just this call, witout modifying the default one
130
+ // use the mock loader for just this call, witout modifying the default one
132
131
$compacted = jsonld_compact($foo, 'http://example.com/mycontext', array(
133
- 'documentLoader' => 'custom_load'));
132
+ 'documentLoader' => 'mock_load'));
133
+
134
+ // a custom loader that uses a simplistic in-memory cache (no invalidation)
135
+ global $cache;
136
+ $cache = array();
137
+ function cache_load($url) {
138
+ global $jsonld_default_load_document, $cache;
139
+ if(isset($cache[$url])) {
140
+ return $cache[$url];
141
+ }
142
+ // use default loader
143
+ $doc = call_user_func($jsonld_default_load_document, $url);
144
+ $cache[$url] = $doc;
145
+ return $doc;
146
+ }
147
+
148
+ // use the cache loader for just this call, witout modifying the default one
149
+ $compacted = jsonld_compact($foo, 'http://schema.org', array(
150
+ 'documentLoader' => 'cache_load'));
134
151
```
135
152
136
153
Commercial Support
0 commit comments