Эх сурвалжийг харах

Add a CascadingLoader implementation.

Justin Hileman 13 жил өмнө
parent
commit
e5bc8d5edf

+ 69 - 0
src/Mustache/Loader/CascadingLoader.php

@@ -0,0 +1,69 @@
+<?php
+
+/*
+ * This file is part of Mustache.php.
+ *
+ * (c) 2013 Justin Hileman
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * A Mustache Template cascading loader implementation, which delegates to other
+ * Loader instances.
+ */
+class Mustache_Loader_CascadingLoader implements Mustache_Loader
+{
+    private $loaders;
+
+    /**
+     * Construct a CascadingLoader with an array of loaders:
+     *
+     *     $loader = new Mustache_Loader_CascadingLoader(array(
+     *         new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__),
+     *         new Mustache_Loader_FilesystemLoader(__DIR__.'/templates')
+     *     ));
+     *
+     * @param array $loaders An array of Mustache Loader instances
+     */
+    public function __construct(array $loaders = array())
+    {
+        $this->loaders = array();
+        foreach ($loaders as $loader) {
+            $this->addLoader($loader);
+        }
+    }
+
+    /**
+     * Add a Loader instance.
+     *
+     * @param Mustache_Loader $loader A Mustache Loader instance
+     */
+    public function addLoader(Mustache_Loader $loader)
+    {
+        $this->loaders[] = $loader;
+    }
+
+    /**
+     * Load a Template by name.
+     *
+     * @throws Mustache_Exception_UnknownTemplateException If a template file is not found.
+     *
+     * @param string $name
+     *
+     * @return string Mustache Template source
+     */
+    public function load($name)
+    {
+        foreach ($this->loaders as $loader) {
+            try {
+                return $loader->load($name);
+            } catch (Mustache_Exception_UnknownTemplateException $e) {
+                // do nothing, check the next loader.
+            }
+        }
+
+        throw new Mustache_Exception_UnknownTemplateException($name);
+    }
+}

+ 40 - 0
test/Mustache/Test/Loader/CascadingLoaderTest.php

@@ -0,0 +1,40 @@
+<?php
+
+/*
+ * This file is part of Mustache.php.
+ *
+ * (c) 2012 Justin Hileman
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * @group unit
+ */
+class Mustache_Test_Loader_CascadingLoaderTest extends PHPUnit_Framework_TestCase
+{
+    public function testLoadTemplates()
+    {
+        $loader = new Mustache_Loader_CascadingLoader(array(
+            new Mustache_Loader_ArrayLoader(array('foo' => '{{ foo }}')),
+            new Mustache_Loader_ArrayLoader(array('bar' => '{{#bar}}BAR{{/bar}}')),
+        ));
+
+        $this->assertEquals('{{ foo }}', $loader->load('foo'));
+        $this->assertEquals('{{#bar}}BAR{{/bar}}', $loader->load('bar'));
+    }
+
+    /**
+     * @expectedException Mustache_Exception_UnknownTemplateException
+     */
+    public function testMissingTemplatesThrowExceptions()
+    {
+        $loader = new Mustache_Loader_CascadingLoader(array(
+            new Mustache_Loader_ArrayLoader(array('foo' => '{{ foo }}')),
+            new Mustache_Loader_ArrayLoader(array('bar' => '{{#bar}}BAR{{/bar}}')),
+        ));
+
+        $loader->load('not_a_real_template');
+    }
+}