diff --git a/lib/Twig/FactoryRuntimeLoader.php b/lib/Twig/FactoryRuntimeLoader.php new file mode 100644 index 000000000..cf7ba9308 --- /dev/null +++ b/lib/Twig/FactoryRuntimeLoader.php @@ -0,0 +1,40 @@ + + */ +class Twig_FactoryRuntimeLoader implements Twig_RuntimeLoaderInterface +{ + private $map; + + /** + * @param array $map An array of format [classname => factory callable] + */ + public function __construct(array $map = array()) + { + $this->map = $map; + } + + /** + * {@inheritdoc} + */ + public function load($class) + { + if (isset($this->map[$class])) { + $runtimeFactory = $this->map[$class]; + + return $runtimeFactory(); + } + } +} diff --git a/test/Twig/Tests/RuntimeFactoryLoaderTest.php b/test/Twig/Tests/RuntimeFactoryLoaderTest.php new file mode 100644 index 000000000..f418edf78 --- /dev/null +++ b/test/Twig/Tests/RuntimeFactoryLoaderTest.php @@ -0,0 +1,32 @@ + 'getRuntime')); + + $this->assertInstanceOf('stdClass', $loader->load('stdClass')); + } + + public function testLoadReturnsNullForUnmappedRuntime() + { + $loader = new Twig_FactoryRuntimeLoader(); + + $this->assertNull($loader->load('stdClass')); + } +} + +function getRuntime() +{ + return new stdClass(); +}