Lazy load all content, keep it off the memory.

fixes #443
This commit is contained in:
Stéphane Goetz 2017-06-06 23:16:45 +02:00
parent 21c0211f0d
commit 6b5d395ca3
4 changed files with 29 additions and 33 deletions

View File

@ -4,7 +4,6 @@ abstract class SimplePage implements Page
{ {
protected $title; protected $title;
protected $content; protected $content;
protected $generated = null;
public function __construct($title, $content) public function __construct($title, $content)
{ {
@ -18,11 +17,7 @@ abstract class SimplePage implements Page
public function getContent() public function getContent()
{ {
if (is_null($this->generated)) { return $this->generatePage();
$this->generated = $this->generatePage();
}
return $this->generated;
} }
protected function initializePage($title, $content) protected function initializePage($title, $content)

View File

@ -121,10 +121,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
'page' => ContentPage::fromFile($node, $params, $contentType), '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') { if ($key == 'index.html') {
$final['title'] = $this->prefix . $tree->getTitle(); $final['title'] = $this->prefix . $tree->getTitle();
$final['file'] = $node; $final['file'] = $node;

View File

@ -219,13 +219,13 @@ class Publisher
} }
} }
protected function shouldUpdate($local, $published) protected function shouldUpdate($local, $local_content, $published)
{ {
if (!array_key_exists('content', $published)) { if (!array_key_exists('content', $published)) {
return true; return true;
} }
$trimmed_local = trim($local->getContent()); $trimmed_local = trim($local_content);
$trimmed_distant = trim($published['content']); $trimmed_distant = trim($published['content']);
if ($trimmed_local == $trimmed_distant) { if ($trimmed_local == $trimmed_distant) {
@ -272,13 +272,14 @@ class Publisher
$this->run( $this->run(
'- ' . $this->niceTitle($entry['file']->getUrl()), '- ' . $this->niceTitle($entry['file']->getUrl()),
function () use ($entry, $published, $parent_id) { 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( $this->client->updatePage(
$parent_id, $parent_id,
$published['id'], $published['id'],
$published['version'] + 1, $published['version'] + 1,
$entry['title'], $entry['title'],
$entry['page']->getContent() $generated_content
); );
} }
} }

View File

@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Tree; <?php namespace Todaymade\Daux\Tree;
use RuntimeException;
use Webuni\FrontMatter\FrontMatter; use Webuni\FrontMatter\FrontMatter;
class Content extends ContentAbstract class Content extends ContentAbstract
@ -19,20 +20,32 @@ class Content extends ContentAbstract
/** @var bool */ /** @var bool */
protected $manuallySetContent = false; protected $manuallySetContent = false;
protected function getFrontMatter()
{
$content = null;
if ($this->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 * @return string
*/ */
public function getContent() public function getContent()
{ {
if (!$this->content && !$this->manuallySetContent) { return $this->getFrontMatter()->getContent();
$this->content = file_get_contents($this->getPath());
}
if ($this->attributes === null) {
$this->parseAttributes();
}
return $this->content;
} }
/** /**
@ -101,17 +114,8 @@ class Content extends ContentAbstract
// is called in "getContent" // is called in "getContent"
$this->attributes = []; $this->attributes = [];
$frontMatter = new FrontMatter(); $document = $this->getFrontMatter();
$content = $this->getContent();
if (substr($content, 0, 3) == "\xef\xbb\xbf") {
$content = substr($content, 3);
}
$document = $frontMatter->parse($content);
$this->attributes = array_replace_recursive($this->attributes, $document->getData()); $this->attributes = array_replace_recursive($this->attributes, $document->getData());
$this->setContent($document->getContent());
} }
public function setAttributes(array $attributes) public function setAttributes(array $attributes)