@@ -6,24 +6,17 @@ Create custom content types for [Edlib](https://edlib.com/).
6
6
7
7
## Requirements
8
8
9
- * PHP 8.2
10
- * A [ PSR-17] ( https://www.php-fig.org/psr/psr-17/ ) implementation, e.g.
11
- [ guzzlehttp/psr7] ( https://packagist.org/packages/guzzlehttp/psr7 )
12
- * A [ PSR-18] ( https://www.php-fig.org/psr/psr-18/ ) compatible HTTP client, e.g.
13
- [ Guzzle 7] ( https://packagist.org/packages/guzzlehttp/guzzle )
14
- * Network access to Edlib internal services
15
- * A RabbitMQ instance shared with Edlib (optional)
9
+ * PHP 8.2 or 8.3
16
10
17
11
## Installation
18
12
19
13
~~~ sh
20
- composer require cerpus/edlib-resource-kit guzzlehttp/guzzle:^7 guzzlehttp/psr7
14
+ composer require cerpus/edlib-resource-kit
21
15
~~~
22
16
23
- ## Usage (Edlib 3)
17
+ ## Usage
24
18
25
- Rather than communicating via bespoke APIs over private network connections,
26
- Edlib 3 is notified of new content via the [ LTI Content-Item Message standard] ( http://www.imsglobal.org/specs/lticiv1p0/specification ) .
19
+ Edlib is notified of new content via the [ LTI Content-Item Message standard] ( http://www.imsglobal.org/specs/lticiv1p0/specification ) .
27
20
Edlib Resource Kit provides message objects, mappers, and serialisers for
28
21
working with Content-Item messages.
29
22
@@ -96,196 +89,12 @@ Output:
96
89
}
97
90
```
98
91
99
- ## Usage (old Edlib)
100
-
101
92
### Framework integration
102
93
103
94
We provide [ integration with
104
95
Laravel] ( https://github.com/cerpus/php-edlib-resource-kit-laravel ) that
105
96
simplifies use of this package.
106
97
107
- ### Configuration
108
-
109
- There are two ways of making Edlib aware of new resources:
110
-
111
- * Using the message bus
112
- * Synchronous HTTP request
113
-
114
- The HTTP approach is slower, but waits for the publishing of a resource to be
115
- completed, allowing for error handling.
116
-
117
- When using the message bus approach, a RabbitMQ connection must be provided:
118
-
119
- ~~~ php
120
- use Cerpus\EdlibResourceKit\ResourceKit;
121
- use Cerpus\PubSub\Connection\ConnectionFactory;
122
-
123
- $connectionFactory = new ConnectionFactory('localhost', 5672, 'guest', 'guest');
124
- $resourceKit = new ResourceKit($connectionFactory);
125
- ~~~
126
-
127
- For the HTTP approach, other than providing the ` synchronousResourceManager `
128
- flag, there is no mandatory configuration:
129
-
130
- ~~~ php
131
- use Cerpus\EdlibResourceKit\ResourceKit;
132
-
133
- $resourceKit = new ResourceKit(synchronousResourceManager: true);
134
- ~~~~
135
-
136
- Once you have a ResourceKit instance, you can begin to access the various
137
- components of it:
138
-
139
- ~~~php
140
- $resourceManager = $resourceKit->getResourceManager();
141
- $versionManager = $resourecKit->getResourceVersionManager();
142
- ~~~~
143
-
144
- ### Notifying Edlib of content updates
145
-
146
- Given a model class representing an item of your custom content type (in this
147
- case an article):
148
-
149
- ~~~php
150
- namespace App\Models;
151
-
152
- use Cerpus\EdlibResourceKit\Contract\EdlibResource;
153
-
154
- class Article
155
- {
156
- // Database-mapped article ID
157
- private string $id;
158
-
159
- public function toEdlibResource(): EdlibResource
160
- {
161
- return new ArticleEdlibResource($this->id, /* ... */);
162
- }
163
- }
164
- ~~~
165
-
166
- Create an accompanying ` EdlibResource ` class:
167
-
168
- ~~~ php
169
- namespace App\DataObjects;
170
-
171
- use Cerpus\EdlibResourceKit\Contract\EdlibResource;
172
-
173
- class ArticleEdlibResource implements EdlibResource
174
- {
175
- public function __construct(private string $systemId, /* ... */)
176
- {
177
- }
178
-
179
- public function getSystemName(): string
180
- {
181
- return 'my-unique-and-persistent-system-name';
182
- }
183
-
184
- public function getSystemId(): string
185
- {
186
- return $this->systemId;
187
- }
188
-
189
- public function getContentType(): string|null
190
- {
191
- return 'article';
192
- }
193
-
194
- // ... implement the remaining EdlibResource methods here
195
- }
196
- ~~~
197
-
198
- When the article is created or updated, a corresponding call to the resource
199
- manager must take place. The procedure will vary depending on your framework of
200
- choice, but here it is demonstrated using the observer pattern:
201
-
202
- ~~~ php
203
- use Cerpus\EdlibResourceKit\Contract\EdlibResource;
204
- use Cerpus\EdlibResourceKit\Resource\ResourceManagerInterface;
205
-
206
- class ArticleObserver
207
- {
208
- public function __construct(private ResourceManagerInterface $manager)
209
- {
210
- }
211
-
212
- public function onCreate(Article $article): void
213
- {
214
- $this->save($article->toEdlibResource());
215
- }
216
-
217
- public function onUpdate(Article $article): void
218
- {
219
- $this->save($article->toEdlibResource());
220
- }
221
-
222
- private function save(EdlibResource $resource): void
223
- {
224
- try {
225
- return $this->manager->save($resource);
226
- } catch (ResourceSaveFailedException $e) {
227
- // handle the failure somehow
228
- }
229
- }
230
- }
231
- ~~~
232
-
233
- If everything went well, the resource should now be accessible from within
234
- Edlib.
235
-
236
- ## Advanced usage
237
-
238
- ### Overriding the HTTP client & message factories
239
-
240
- This library will look for and make use of any PSR-17 compatible message
241
- factories and PSR-18 compatible HTTP clients that are installed (via
242
- [ HTTPlug Discovery] ( https://github.com/php-http/discovery ) ). You can override
243
- these with factories & clients of your choosing:
244
-
245
- ~~~ php
246
- use App\Http\MyClient;
247
- use App\Http\MyRequestFactory;
248
- use App\Http\MyStreamFactory;
249
- use Cerpus\EdlibResourceKit\ResourceKit;
250
-
251
- $resourceKit = new ResourceKit(
252
- httpClient: new MyClient(),
253
- requestFactory: new MyRequestFactory(),
254
- streamFactory: new MyStreamFactory(),
255
- synchronousResourceManager: true,
256
- );
257
- ~~~
258
-
259
- By wiring up the HTTP client provided by your web framework, you may get better
260
- logging & debugging capabilities.
261
-
262
- ### Adding extra data when publishing resources
263
-
264
- It might be necessary to add extra data to a resource before publishing it. This
265
- can be done using a custom serializer:
266
-
267
- ~~~ php
268
- use Cerpus\EdlibResourceKit\Contract\EdlibResource;
269
- use Cerpus\EdlibResourceKit\ResourceKit;
270
- use Cerpus\EdlibResourceKit\Serializer\ResourceSerializer;
271
-
272
- class MySerializer extends ResourceSerializer
273
- {
274
- public function serialize(EdlibResource $resource): array
275
- {
276
- $data = parent::serialize($resource);
277
-
278
- if ($resource instanceof MyEdlibResource) {
279
- $data['some_custom_key'] => $resource->getMyCustomData();
280
- }
281
-
282
- return $data;
283
- }
284
- }
285
-
286
- $resourceKit = new ResourceKit($pubSub, resourceSerializer: new MySerializer());
287
- ~~~
288
-
289
98
## License
290
99
291
100
This package is released under the GNU General Public License 3.0. See the
0 commit comments