app.php 18.3 KB
Newer Older
Cyril Vazquez's avatar
Cyril Vazquez committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/*
 * Copyright (C) Maarch
 *
 * This file is part of Maarch RM.
 *
 * Maarch RM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Maarch RM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Maarch RM. If not, see <http://www.gnu.org/licenses/>.
 */
20
// The Maarch http application will be used`with JsonApi documents for the schema
21
use Maarch\Http\Application;
22
use Maarch\JsonApi\Schema as JsonApi;
23
use Maarch\Metamodel;
24

25
// DOC
26
27
28
29
$docTemplate = new Metamodel\Documentation();

// DEFINITIONS -> SCHEMA
$schema = new Application\Schema();
30

31
$typeTemplate = new Metamodel\Type();
32
33
34
35
36
37
38
39
40
41
42
43
44
$propertyTemplate = new JsonApi\Property();
$linkTemplate = new Application\Link();
$paramTemplate = new Application\Parameter();

$attributesPropertyTemplate = $propertyTemplate->withName('attributes');
$relationshipsPropertyTemplate = $propertyTemplate->withName('relationships');
$metadataPropertyTemplate = $propertyTemplate->withName('metadata');
$objectTemplate = $typeTemplate->withType('object');
$resourceTemplate = new JsonApi\Resource();
$ressourceCollectionTemplate = new JsonApi\ResourceCollection();

// The configuration is included from a directory contents
// This path MUST be set by an external configuration (ini, constant...)
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
45
$schemaConfDir = __DIR__.'/schema';
46
47
48
49
50
51
52
53
54
55
$schemaConf = [];
foreach (scandir($schemaConfDir) as $schemaConfFile) {
    if (is_file($schemaConfDir.DIRECTORY_SEPARATOR.$schemaConfFile)) {
        $types = include($schemaConfDir.DIRECTORY_SEPARATOR.$schemaConfFile);

        foreach ($types as $name => $type) {
            $schema->set($name, $type);
        }
    }
}
Cyril Vazquez's avatar
Cyril Vazquez committed
56
57


58
// DEFINITIONS -> REPRESENTATIONS
59
$representations = new Application\Definitions();
60
61
$representationTemplate = new Application\Representation(['mediaType' => 'application/vnd.api+json']);

62
63
$representations->set('orgCollectionRepresentation', $representationTemplate->withType('orgs'));
$representations->set('orgRepresentation', $representationTemplate->withType('org'));
Cyril Vazquez's avatar
Cyril Vazquez committed
64

65
66
$representations->set('orgUnitCollectionRepresentation', $representationTemplate->withType('orgUnits'));
$representations->set('orgUnitRepresentation', $representationTemplate->withType('orgUnit'));
67

68
69
$representations->set('folderCollectionRepresentation', $representationTemplate->withType('folders'));
$representations->set('folderRepresentation', $representationTemplate->withType('folder'));
70

71
72
73
74
$representations->set('archiveUnitCollectionRepresentation', $representationTemplate->withType('archiveUnits'));
$representations->set('archiveUnitRepresentation', $representationTemplate->withType('archiveUnit'));
$representations->set('archiveUnitDescriptionRepresentation', $representationTemplate->withType('archiveUnitDescription'));
$representations->set('archiveUnitManagementRepresentation', $representationTemplate->withType('archiveUnitManagement'));
75

76
77
$representations->set('dataObjectCollectionRepresentation', $representationTemplate->withType('dataObjects'));
$representations->set('dataObjectRepresentation', $representationTemplate->withType('dataObject'));
78

79
80
$representations->set('archiveUnitProfileCollectionRepresentation', $representationTemplate->withType('archiveUnitProfiles'));
$representations->set('archiveUnitProfileRepresentation', $representationTemplate->withType('archiveUnitProfile'));
81

82
// RESPONSES
83
$responses = new Application\Definitions();
84
85
$responseTemplate = new Application\Response();

86
87
88
89
90
$responses->set('getOrgsResponse', $responseTemplate->withRepresentation('jsonapi', 'orgCollectionRepresentation'));
$responses->set('getOrgResponse', $responseTemplate->withRepresentation('jsonapi', 'orgRepresentation'));

$responses->set('getOrgUnitsResponse', $responseTemplate->withRepresentation('jsonapi', 'orgUnitCollectionRepresentation'));
$responses->set('getOrgUnitResponse', $responseTemplate->withRepresentation('jsonapi', 'orgUnitRepresentation'));
91

92
93
$responses->set('getFoldersResponse', $responseTemplate->withRepresentation('jsonapi', 'folderCollectionRepresentation'));
$responses->set('getFolderResponse', $responseTemplate->withRepresentation('jsonapi', 'folderRepresentation'));
94

95
96
97
98
$responses->set('getArchiveUnitsResponse', $responseTemplate->withRepresentation('jsonapi', 'archiveUnitCollectionRepresentation'));
$responses->set('getArchiveUnitResponse', $responseTemplate->withRepresentation('jsonapi', 'archiveUnitRepresentation'));
$responses->set('getArchiveUnitDescriptionResponse', $responseTemplate->withRepresentation('jsonapi', 'archiveUnitDescriptionRepresentation'));
$responses->set('getArchiveUnitManagementResponse', $responseTemplate->withRepresentation('jsonapi', 'archiveUnitManagementRepresentation'));
99

100
101
$responses->set('getDataObjectsResponse', $responseTemplate->withRepresentation('jsonapi', 'dataObjectCollectionRepresentation'));
$responses->set('getDataObjectResponse', $responseTemplate->withRepresentation('jsonapi', 'dataObjectRepresentation'));
102

103
104
105
$responses->set('getArchiveUnitProfilesResponse', $responseTemplate->withRepresentation('jsonapi', 'archiveUnitProfileCollectionRepresentation'));
$responses->set('getArchiveUnitProfileResponse', $responseTemplate->withRepresentation('jsonapi', 'archiveUnitProfileRepresentation'));

106
// OPERATIONS
107
$operations = new Application\Definitions();
108
$operationTemplate = new Application\Operation();
109
110

$operations->set('getResourceDescriptionOperation', $operationTemplate->withEndpoint('getResourceDescription'));
111
$operations->set('getAppDescriptionOperation', $operationTemplate->withEndpoint('getAppDescription'));
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
// Orgs
$operations->set('getOrgsOperation', $operationTemplate->withEndpoint('getOrgs')->withResponse('default', 'getOrgsResponse'));
$operations->set('getOrgOperation', $operationTemplate->withEndpoint('getOrg')->withResponse('default', 'getOrgResponse'));
$operations->set('getOrgOrgUnitsOperation', $operationTemplate->withEndpoint('getOrgOrgUnits')->withResponse('default', 'getOrgUnitsResponse'));
$operations->set('getOrgParentOperation', $operationTemplate->withEndpoint('getOrgParent')->withResponse('default', 'getOrgResponse'));
$operations->set('getOrgChildrenOperation', $operationTemplate->withEndpoint('getOrgChildren')->withResponse('default', 'getOrgsResponse'));

// OrgUnits
$operations->set('getOrgUnitsOperation', $operationTemplate->withEndpoint('getOrgUnits')->withResponse('default', 'getOrgUnitsResponse'));
$operations->set('getOrgUnitOperation', $operationTemplate->withEndpoint('getOrgUnit')->withResponse('default', 'getOrgUnitResponse'));
$operations->set('getOrgUnitParentOperation', $operationTemplate->withEndpoint('getOrgUnitParent')->withResponse('default', 'getOrgUnitResponse'));
$operations->set('getOrgUnitChildrenOperation', $operationTemplate->withEndpoint('getOrgUnitChildren')->withResponse('default', 'getOrgUnitsResponse'));
$operations->set('getOrgUnitFoldersOperation', $operationTemplate->withEndpoint('getOrgUnitFolders')->withResponse('default', 'getFoldersResponse'));
$operations->set('getOrgUnitArchiveUnitsOperation', $operationTemplate->withEndpoint('getOrgUnitArchiveUnits')->withResponse('default', 'getArchiveUnitsResponse'));

// Folders
$operations->set('getFoldersOperation', $operationTemplate->withEndpoint('getFolders')->withResponse('default', 'getFoldersResponse'));
$operations->set('getFolderOperation', $operationTemplate->withEndpoint('getFolder')->withResponse('default', 'getFolderResponse'));
$operations->set('getFolderParentOperation', $operationTemplate->withEndpoint('getFolderParent')->withResponse('default', 'getFolderResponse'));
$operations->set('getFolderChildrenOperation', $operationTemplate->withEndpoint('getFolderChildren')->withResponse('default', 'getFoldersResponse'));
$operations->set('getFolderArchiveUnitsOperation', $operationTemplate->withEndpoint('getFolderArchiveUnits')->withResponse('default', 'getArchiveUnitsResponse'));

$operations->set('createFolderOperation', $operationTemplate->withEndpoint('createFolder')->withResponse('default', 'getFolderResponse'));

// ArchiveUnits
$operations->set('getArchiveUnitsOperation', $operationTemplate->withEndpoint('getArchiveUnits')->withResponse('default', 'getArchiveUnitsResponse'));
$operations->set('getArchiveUnitOperation', $operationTemplate->withEndpoint('getArchiveUnit')->withResponse('default', 'getArchiveUnitResponse'));
$operations->set('getArchiveUnitDescriptionOperation', $operationTemplate->withEndpoint('getArchiveUnitDescription')->withResponse('default', 'getArchiveUnitDescriptionResponse'));
$operations->set('getArchiveUnitManagementOperation', $operationTemplate->withEndpoint('getArchiveUnitManagement')->withResponse('default', 'getArchiveUnitManagementResponse'));
$operations->set('getArchiveUnitChildrenOperation', $operationTemplate->withEndpoint('getArchiveUnitChildren')->withResponse('default', 'getArchiveUnitsResponse'));
$operations->set('getArchiveUnitParentOperation', $operationTemplate->withEndpoint('getArchiveUnitParent')->withResponse('default', 'getArchiveUnitResponse'));

$operations->set('getArchiveUnitDataObjectsOperation', $operationTemplate->withEndpoint('getArchiveUnitDataObjects')->withResponse('default', 'getDataObjectsResponse'));
$operations->set('getArchiveUnitDataObjectOperation', $operationTemplate->withEndpoint('getArchiveUnitDataObject')->withResponse('default', 'getDataObjectResponse'));
$operations->set('getArchiveUnitDataObjectContentsOperation', $operationTemplate->withEndpoint('getArchiveUnitDataObjectContents')->withResponse('default', 'getDataObjectContentsResponse'));

// DataObjects
$operations->set('getDataObjectsOperation', $operationTemplate->withEndpoint('getDataObjects')->withResponse('default', 'getDataObjectsResponse'));
$operations->set('getDataObjectOperation', $operationTemplate->withEndpoint('getDataObject')->withResponse('default', 'getDataObjectResponse'));
$operations->set('getDataObjectContentsOperation', $operationTemplate->withEndpoint('getDataObjectContents')->withResponse('default', 'getDataObjectContentsResponse'));
153

154
155
156
157
// ArchiveUnitProfiles
$operations->set('getArchiveUnitProfilesOperation', $operationTemplate->withEndpoint('getArchiveUnitProfiles')->withResponse('default', 'getArchiveUnitProfilesResponse'));
$operations->set('getArchiveUnitProfileOperation', $operationTemplate->withEndpoint('getArchiveUnitProfile')->withResponse('default', 'getArchiveUnitProfileResponse'));
$operations->set('getArchiveUnitDescriptionProfileOperation', $operationTemplate->withEndpoint('getArchiveUnitDescriptionProfile')->withResponse('default', 'getArchiveUnitDescriptionProfileResponse'));
158
159

// PATHS -> RESOURCES
160

161
$resourceIdParamTemplate = new Application\Parameter(['binding' => 'path', 'type' => $typeTemplate->withType('string'), 'isRequired' => true]);
162
163
164
165
166
$resourceTemplate = new Application\Resource();

// App
$appResource = $resourceTemplate
    ->withOperation('get', 'getAppDescriptionOperation');
167

168
// docs
169
170
$typeParam = $resourceIdParamTemplate
    ->withName('type')
Cyril Vazquez's avatar
Wip    
Cyril Vazquez committed
171
172
173
174
175
    ->withType(
        $typeTemplate
            ->withType('string')
            ->withEnum(['orgs', 'orgUnits', 'folders', 'archiveUnits', 'archiveProfileUnits'])
    )
176
177
178
179
180
    ->withDocumentation(
        $docTemplate
            ->withSummary('resourceType')
    )
;
181
182
183
184
$resourceDescriptionResource = $resourceTemplate
    ->withParameters([$typeParam])
    ->withOperation('get', 'getResourceDescriptionOperation');

185
186
// Orgs
$orgIdParam = $resourceIdParamTemplate->withName('orgId');
187
188
189
$orgResource = $resourceTemplate
    ->withParameters([$orgIdParam])
    ->withOperation('get', 'getOrgOperation')
190
    ->withResource(
191
        'parent',
192
193
        $resourceTemplate
            ->withParameters([$orgIdParam])
194
195
196
197
198
199
200
201
202
203
204
205
206
            ->withOperation('get', 'getOrgParentOperation')
    )
    ->withResource(
        'children',
        $resourceTemplate
            ->withParameters([$orgIdParam])
            ->withOperation('get', 'getOrgChildrenOperation')
    )
    ->withResource(
        'orgUnits',
        $resourceTemplate
            ->withParameters([$orgIdParam])
            ->withOperation('get', 'getOrgOrgUnitsOperation')
207
    )
Cyril Vazquez's avatar
Cyril Vazquez committed
208
;
209
210
211
$orgCollectionResource = $resourceTemplate
    ->withOperation('get', 'getOrgsOperation')
    ->withResource('{orgId}', $orgResource);
Cyril Vazquez's avatar
Cyril Vazquez committed
212

213
214
215
216
217
// OrgUnits
$orgUnitIdParam = $resourceIdParamTemplate->withName('orgUnitId');
$orgUnitResource = $resourceTemplate
    ->withParameters([$orgUnitIdParam])
    ->withOperation('get', 'getOrgUnitOperation')
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    ->withResource(
        'parent',
        $resourceTemplate
            ->withParameters([$orgUnitIdParam])
            ->withOperation('get', 'getOrgUnitParentOperation')
    )
    ->withResource(
        'children',
        $resourceTemplate
            ->withParameters([$orgUnitIdParam])
            ->withOperation('get', 'getOrgUnitChildrenOperation')
    )
    ->withResource(
        'folders',
        $resourceTemplate
            ->withParameters([$orgUnitIdParam])
            ->withOperation('get', 'getOrgUnitFoldersOperation')
    )
    ->withResource(
        'archiveUnits',
        $resourceTemplate
            ->withParameters([$orgUnitIdParam])
            ->withOperation('get', 'getOrgUnitArchiveUnitsOperation')
    )
242
;
243
$orgUnitCollectionResource = $resourceTemplate
244
    ->withOperation('get', 'getOrgUnitsOperation')
245
246
    ->withResource('{orgUnitId}', $orgUnitResource)
;
247

248

249
250
251
252
253
// Folders
$folderIdParam = $resourceIdParamTemplate->withName('folderId');
$folderResource = $resourceTemplate
    ->withParameters([$folderIdParam])
    ->withOperation('get', 'getFolderOperation')
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    ->withResource(
        'parent',
        $resourceTemplate
            ->withParameters([$folderIdParam])
            ->withOperation('get', 'getFolderParentOperation')
    )
    ->withResource(
        'children',
        $resourceTemplate
            ->withParameters([$folderIdParam])
            ->withOperation('get', 'getFolderChildrenOperation')
    )
    ->withResource(
        'archiveUnits',
        $resourceTemplate
            ->withParameters([$folderIdParam])
            ->withOperation('get', 'getFolderArchiveUnitsOperation')
    )
272
;
273
$folderCollectionResource = $resourceTemplate
274
    ->withOperation('get', 'getFoldersOperation')
275
276
    ->withResource('{folderId}', $folderResource)
;
Cyril Vazquez's avatar
Cyril Vazquez committed
277

278
// ArchiveUnits and DataObjects
279
280
281
282
$archiveUnitIdParam = $resourceIdParamTemplate->withName('archiveUnitId');
$dataObjectIdParam = $resourceIdParamTemplate->withName('dataObjectId');
$archiveUnitResource = $resourceTemplate
    ->withParameters([$archiveUnitIdParam])
283
    ->withOperation('get', 'getArchiveUnitOperation')
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    ->withResource(
        'description',
        $resourceTemplate
            ->withParameters([$archiveUnitIdParam])
            ->withOperation('get', 'getArchiveUnitDescriptionOperation')
    )
    ->withResource(
        'management',
        $resourceTemplate
            ->withParameters([$archiveUnitIdParam])
            ->withOperation('get', 'getArchiveUnitManagementOperation')
    )
    ->withResource(
        'parent',
        $resourceTemplate
            ->withParameters([$archiveUnitIdParam])
            ->withOperation('get', 'getArchiveUnitParentOperation')
    )
    ->withResource(
        'children',
        $resourceTemplate
            ->withParameters([$archiveUnitIdParam])
            ->withOperation('get', 'getArchiveUnitChildrenOperation')
    )
    ->withResource(
        'dataObjects',
        $resourceTemplate
            ->withParameters([$archiveUnitIdParam])
            ->withOperation('get', 'getArchiveUnitDataObjectsOperation')
            ->withResource(
                '{dataObjectId}',
                $resourceTemplate
                    ->withParameters([$archiveUnitIdParam, $dataObjectIdParam])
                    ->withOperation('get', 'getArchiveUnitDataObjectOperation')
                    ->withResource(
                        'contents',
                        $resourceTemplate
                            ->withParameters([$archiveUnitIdParam, $dataObjectIdParam])
                            ->withOperation('get', 'getArchiveUnitDataObjectContentsOperation')
                    )
            )
    )
326
;
327
$archiveUnitCollectionResource = $resourceTemplate
328
329
330
331
    ->withOperation('get', 'getArchiveUnitsOperation')
    ->withResource('{archiveUnitId}', $archiveUnitResource)
;

332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// ArchiveUnitProfiles
$archiveUnitProfileIdParam = $resourceIdParamTemplate->withName('archiveUnitProfileId');
$archiveUnitProfileResource = $resourceTemplate
    ->withParameters([$archiveUnitProfileIdParam])
    ->withOperation('get', 'getArchiveUnitProfileOperation')
    ->withResource(
        'descriptionProfile',
        $resourceTemplate
            ->withParameters([$archiveUnitProfileIdParam])
            ->withOperation('get', 'getArchiveUnitDescriptionProfileOperation')
    )
;
$archiveUnitProfileCollectionResource = $resourceTemplate
    ->withOperation('get', 'getArchiveUnitProfilesOperation')
    ->withResource('{archiveUnitProfileId}', $archiveUnitProfileResource);

348

349
350
// APP
$app = new Application\Application();
351
$app = $app
352
    ->withHost('http://maarchrm')
Cyril Vazquez's avatar
Cyril Vazquez committed
353
    ->withBasePath('/V3')
354
355
356
357
358
359
360
// REFS -> TYPES
    ->withDefinition('schema', $schema)
    ->withDefinition('representations', $representations)
    ->withDefinition('responses', $responses)
    ->withDefinition('operations', $operations)

// PATHS -> RESOURCES
361
    ->withResource('', $appResource)
362
363
364
365
366
367
    ->withResource('docs/types/{type}', $resourceDescriptionResource)
    ->withResource('orgs', $orgCollectionResource)
    ->withResource('orgUnits', $orgUnitCollectionResource)
    ->withResource('folders', $folderCollectionResource)
    ->withResource('archiveUnits', $archiveUnitCollectionResource)
    ->withResource('archiveUnitProfiles', $archiveUnitProfileCollectionResource)
Cyril Vazquez's avatar
Cyril Vazquez committed
368
369
;

Cyril Vazquez's avatar
Cyril Vazquez committed
370
371
372

// The Json for OpenApi configuration
// This path MUST be set by an external configuration (ini, constant...)
373
/*$appConfFile = 'V3/App/conf/openapi.json';
Cyril Vazquez's avatar
Cyril Vazquez committed
374
375
376
377
378
379
380
$openApi = json_decode(file_get_contents($appConfFile));

// The app endpoint
// This url MUST be set by an external configuration (ini, constant...)
$openApi->host = 'http://maarchrm';

// Instanciate the Maarch http application
381
382
383
$app = new App($openApi);

//$container->add('App', $app);*/
Cyril Vazquez's avatar
Cyril Vazquez committed
384

385
$container->add('App', $app);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
386

Cyril Vazquez's avatar
Cyril Vazquez committed
387
// Return the Maarch http application to the caller
388
return $app;