From 6b5d395ca369152f5c91bfecb9191421fb904f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Tue, 6 Jun 2017 23:16:45 +0200 Subject: [PATCH] Lazy load all content, keep it off the memory. fixes #443 --- libs/Format/Base/SimplePage.php | 7 +---- libs/Format/Confluence/Generator.php | 4 --- libs/Format/Confluence/Publisher.php | 9 +++--- libs/Tree/Content.php | 42 +++++++++++++++------------- 4 files changed, 29 insertions(+), 33 deletions(-) diff --git a/libs/Format/Base/SimplePage.php b/libs/Format/Base/SimplePage.php index bc7660a..1f1c7aa 100644 --- a/libs/Format/Base/SimplePage.php +++ b/libs/Format/Base/SimplePage.php @@ -4,7 +4,6 @@ abstract class SimplePage implements Page { protected $title; protected $content; - protected $generated = null; public function __construct($title, $content) { @@ -18,11 +17,7 @@ abstract class SimplePage implements Page public function getContent() { - if (is_null($this->generated)) { - $this->generated = $this->generatePage(); - } - - return $this->generated; + return $this->generatePage(); } protected function initializePage($title, $content) diff --git a/libs/Format/Confluence/Generator.php b/libs/Format/Confluence/Generator.php index 24bdbe4..4dda51c 100644 --- a/libs/Format/Confluence/Generator.php +++ b/libs/Format/Confluence/Generator.php @@ -121,10 +121,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator 'page' => ContentPage::fromFile($node, $params, $contentType), ]; - // As the page is lazily generated - // We do it now to fail fast in case of problem - $data['page']->getContent(); - if ($key == 'index.html') { $final['title'] = $this->prefix . $tree->getTitle(); $final['file'] = $node; diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index 09b0087..bbfb200 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -219,13 +219,13 @@ class Publisher } } - protected function shouldUpdate($local, $published) + protected function shouldUpdate($local, $local_content, $published) { if (!array_key_exists('content', $published)) { return true; } - $trimmed_local = trim($local->getContent()); + $trimmed_local = trim($local_content); $trimmed_distant = trim($published['content']); if ($trimmed_local == $trimmed_distant) { @@ -272,13 +272,14 @@ class Publisher $this->run( '- ' . $this->niceTitle($entry['file']->getUrl()), function () use ($entry, $published, $parent_id) { - if ($this->shouldUpdate($entry['page'], $published)) { + $generated_content = $entry['page']->getContent(); + if ($this->shouldUpdate($entry['page'], $generated_content, $published)) { $this->client->updatePage( $parent_id, $published['id'], $published['version'] + 1, $entry['title'], - $entry['page']->getContent() + $generated_content ); } } diff --git a/libs/Tree/Content.php b/libs/Tree/Content.php index 3b39caf..3f548f7 100644 --- a/libs/Tree/Content.php +++ b/libs/Tree/Content.php @@ -1,5 +1,6 @@ manuallySetContent) { + $content = $this->content; + } else if (!$this->getPath()) { + throw new RuntimeException("Empty content"); + } else { + $content = file_get_contents($this->getPath()); + } + + $frontMatter = new FrontMatter(); + + if (substr($content, 0, 3) == "\xef\xbb\xbf") { + $content = substr($content, 3); + } + + return $frontMatter->parse($content); + } + /** * @return string */ public function getContent() { - if (!$this->content && !$this->manuallySetContent) { - $this->content = file_get_contents($this->getPath()); - } - - if ($this->attributes === null) { - $this->parseAttributes(); - } - - return $this->content; + return $this->getFrontMatter()->getContent(); } /** @@ -101,17 +114,8 @@ class Content extends ContentAbstract // is called in "getContent" $this->attributes = []; - $frontMatter = new FrontMatter(); - - $content = $this->getContent(); - if (substr($content, 0, 3) == "\xef\xbb\xbf") { - $content = substr($content, 3); - } - - $document = $frontMatter->parse($content); - + $document = $this->getFrontMatter(); $this->attributes = array_replace_recursive($this->attributes, $document->getData()); - $this->setContent($document->getContent()); } public function setAttributes(array $attributes)