Lazy load CommonMark converter, speeds up generation in serve mode when file is cached (~ -10ms)

This commit is contained in:
Stéphane Goetz 2019-11-30 21:46:58 +01:00
parent 727c5c015e
commit abaf36bec6
4 changed files with 20 additions and 15 deletions

View File

@ -12,12 +12,23 @@ class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
protected $config;
/** @var CommonMarkConverter */
protected $converter;
private $converter;
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
}
protected function createConverter() {
return new CommonMarkConverter(['daux' => $this->config]);
}
protected function getConverter() {
if (!$this->converter) {
$this->converter = $this->createConverter();
}
return $this->converter;
}
/**
@ -31,7 +42,7 @@ class ContentType implements \Todaymade\Daux\ContentTypes\ContentType
protected function doConversion($raw)
{
Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE);
return $this->converter->convertToHtml($raw);
return $this->getConverter()->convertToHtml($raw);
}
public function convert($raw, Content $node)

View File

@ -4,9 +4,7 @@ use Todaymade\Daux\Config;
class ContentType extends \Todaymade\Daux\ContentTypes\Markdown\ContentType
{
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
protected function createConverter() {
return new CommonMarkConverter(['daux' => $this->config]);
}
}

View File

@ -4,9 +4,7 @@ use Todaymade\Daux\Config;
class ContentType extends \Todaymade\Daux\ContentTypes\Markdown\ContentType
{
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
protected function createConverter() {
return new CommonMarkConverter(['daux' => $this->config]);
}
}

View File

@ -4,9 +4,7 @@ use Todaymade\Daux\Config;
class ContentType extends \Todaymade\Daux\ContentTypes\Markdown\ContentType
{
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
protected function createConverter() {
return new CommonMarkConverter(['daux' => $this->config]);
}
}