Beispiel #1
0
 */
$this->patch('/{slug:.+}', function ($req, $res, $args) use($app, $pages) {
    $parsedBody = $req->getParsedBody();
    $bodyJson = json_encode($req->getParsedBody());
    $slug = $args['slug'];
    if (!isset($req->getQueryParams()['parse'])) {
        $pages->setRenderer($app['pageRendererRAW']);
    }
    $page = $pages->read($slug);
    if (!$page) {
        return $this->api->json($res, ['error' => 'PageDoesNotExists', 'message' => 'Page does not exists'], 404);
    }
    try {
        $patch = new Patch($page, $bodyJson);
        $patchedDocument = $patch->apply();
        $newPage = Page::pageFactory(json_decode($patchedDocument, true));
        $pages->update($slug, $newPage);
    } catch (InvalidPatchDocumentJsonException $e) {
        // Will be thrown when using invalid JSON in a patch document
        return $this->api->json($res, ['error' => 'InvalidPatchDocumentJsonException', 'message' => $e->getMessage()], 400);
    } catch (InvalidTargetDocumentJsonException $e) {
        // Will be thrown when using invalid JSON in a target document
        return $this->api->json($res, ['error' => 'InvalidTargetDocumentJsonException', 'message' => $e->getMessage()], 400);
    } catch (InvalidOperationException $e) {
        // Will be thrown when using an invalid JSON Pointer operation (i.e. missing property)
        return $this->api->json($res, ['error' => 'InvalidOperationException', 'message' => $e->getMessage()], 400);
    } catch (Exception $e) {
        return $this->api->json($res, ['error' => 'InvalidOperationException', 'message' => $e->getMessage()], 500);
    }
});
/*