/** * Render Page object to string * * @param Page $page * @return string Rendered page */ public function render(Page $page) { if (!isset($page->title)) { throw new PageException('Page MUST have a `title` to be serialized'); } $delimiter = $this->delimiterRender . PHP_EOL; $delimiterSection = $this->sectionDelimiterRender . PHP_EOL; $metaArr = $page->getMeta(); if (isset($metaArr['slug'])) { // For flat files DB, the slug is the filename thus we don't need the slug unset($metaArr['slug']); } // Create the front matter $meta = $delimiter; $meta .= trim($this->metadataParser->encode($metaArr)); $meta .= PHP_EOL . $delimiter . PHP_EOL; // Add the content $content = trim($page->getContent()); // Add sections (if exist) if ($sections = $page->getSections()) { foreach ($sections as $section) { $metaSection = trim($this->metadataParser->encode($section->getMeta())); // $content .= PHP_EOL . PHP_EOL . $delimiterSection . $section->name . PHP_EOL . $delimiterSection; $content .= PHP_EOL . PHP_EOL . $delimiterSection . $section->name . PHP_EOL . $metaSection . PHP_EOL . $delimiterSection; $content .= trim($section->content) . PHP_EOL; } } return $meta . $content; }
*/ $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); } }); /*