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; protected $config;
/** @var CommonMarkConverter */ /** @var CommonMarkConverter */
protected $converter; private $converter;
public function __construct(Config $config) public function __construct(Config $config)
{ {
$this->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) protected function doConversion($raw)
{ {
Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE); Daux::writeln("Running conversion", OutputInterface::VERBOSITY_VERBOSE);
return $this->converter->convertToHtml($raw); return $this->getConverter()->convertToHtml($raw);
} }
public function convert($raw, Content $node) public function convert($raw, Content $node)

View File

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

View File

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

View File

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