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
bovenliggende 21c0211f0d
commit 6b5d395ca3
4 gewijzigde bestanden met toevoegingen van 29 en 33 verwijderingen

Bestand weergeven

@ -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)

Bestand weergeven

@ -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;

Bestand weergeven

@ -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
);
}
}

Bestand weergeven

@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Tree;
use RuntimeException;
use Webuni\FrontMatter\FrontMatter;
class Content extends ContentAbstract
@ -19,20 +20,32 @@ class Content extends ContentAbstract
/** @var bool */
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
*/
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)