Skip to content

Commit

Permalink
Lazy renderer initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jan 3, 2018
1 parent d299bcd commit 11fd2f0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Phug/OptionsBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Phug;

use Phug\Util\Partial\OptionTrait;

class OptionsBundle
{
use OptionTrait;
}
48 changes: 48 additions & 0 deletions src/Phug/Partial/FacadeOptionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Phug\Partial;

use ArrayObject;
use Phug\OptionsBundle;

trait FacadeOptionsTrait
{
/**
* @var OptionsBundle
*/
private static $options = null;

protected static function resetFacadeOptions()
{
static::$options = null;
}

protected static function callOption($method, array $arguments)
{
if (!static::$options) {
static::$options = new OptionsBundle();
}

return call_user_func_array([static::$options, $method], $arguments);
}

protected static function isOptionMethod($method)
{
return in_array($method, [
'hasOption',
'getOption',
'setOption',
'setOptions',
'setOptionsRecursive',
'setOptionsDefaults',
'unsetOption',
]);
}

protected static function getFacadeOptions()
{
$options = static::$options ? (static::$options->getOptions() ?: []) : [];

return $options instanceof ArrayObject ? $options->getArrayCopy() : $options;
}
}
9 changes: 8 additions & 1 deletion src/Phug/Phug.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Phug;

use Phug\Partial\ExtensionsTrait;
use Phug\Partial\FacadeOptionsTrait;

class Phug
{
use ExtensionsTrait;
use FacadeOptionsTrait;

/**
* List of global filters stored as array where keys are filter names, and values the action callback.
Expand Down Expand Up @@ -48,7 +50,7 @@ private static function normalizeKeywordName($name)

private static function getOptions(array $options = [])
{
$extras = [];
$extras = static::getFacadeOptions();
foreach (['filters', 'keywords'] as $option) {
$method = 'get'.ucfirst($option);
$extras[$option] = static::$method();
Expand Down Expand Up @@ -108,6 +110,7 @@ public static function removeOptions($path, $options)
*/
public static function reset()
{
static::resetFacadeOptions();
self::$renderer = null;
self::$extensions = [];
self::$filters = [];
Expand Down Expand Up @@ -492,6 +495,10 @@ public static function textualCacheDirectory($source, $destination = null, $opti
*/
public static function __callStatic($name, $arguments)
{
if (!self::$renderer && static::isOptionMethod($name)) {
return static::callOption($name, $arguments);
}

return call_user_func_array([static::getRenderer(), $name], $arguments);
}
}
21 changes: 21 additions & 0 deletions tests/Phug/DefaultOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
use Phug\Renderer\Adapter\StreamAdapter;
use Phug\Util\OptionInterface;

/**
* @coversDefaultClass \Phug\Partial\FacadeOptionsTrait
*/
class DefaultOptionsTest extends AbstractPhugTest
{
private static function isCallable($expected)
Expand Down Expand Up @@ -540,4 +543,22 @@ public function testLexerOptions()
'on_token' => null,
], Phug::getRenderer()->getCompiler()->getParser()->getLexer());
}

/**
* @covers \Phug\Phug::__callStatic
* @covers ::resetFacadeOptions
* @covers ::callOption
* @covers ::isOptionMethod
* @covers ::getFacadeOptions
*/
public function testInitialOption()
{
include_once __DIR__.'/Util/TCompiler.php';
include_once __DIR__.'/Util/TParser.php';
Phug::reset();
Phug::setOption('parser_class_name', \TParser::class);
Phug::setOption('compiler_class_name', \TCompiler::class);
self::assertInstanceOf(\TCompiler::class, Phug::getRenderer()->getCompiler());
self::assertInstanceOf(\TParser::class, Phug::getRenderer()->getCompiler()->getParser());
}
}
5 changes: 5 additions & 0 deletions tests/Phug/Util/TCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class TCompiler extends \Phug\Compiler
{
}
5 changes: 5 additions & 0 deletions tests/Phug/Util/TParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class TParser extends \Phug\Parser
{
}

0 comments on commit 11fd2f0

Please sign in to comment.