Przeglądaj źródła

[Templating] added a getContent() method to the Storage class

Fabien Potencier 15 lat temu
rodzic
commit
e5f8da4ead

+ 4 - 0
src/Symfony/Components/Templating/Storage/FileStorage.php

@@ -20,4 +20,8 @@ namespace Symfony\Components\Templating\Storage;
  */
 class FileStorage extends Storage
 {
+  public function getContent()
+  {
+    return file_get_contents($this->template);
+  }
 }

+ 3 - 1
src/Symfony/Components/Templating/Storage/Storage.php

@@ -18,7 +18,7 @@ namespace Symfony\Components\Templating\Storage;
  * @subpackage templating
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class Storage
+abstract class Storage
 {
   protected $renderer;
   protected $template;
@@ -44,6 +44,8 @@ class Storage
     return (string) $this->template;
   }
 
+  abstract public function getContent();
+
   /**
    * Gets the renderer.
    *

+ 4 - 0
src/Symfony/Components/Templating/Storage/StringStorage.php

@@ -20,4 +20,8 @@ namespace Symfony\Components\Templating\Storage;
  */
 class StringStorage extends Storage
 {
+  public function getContent()
+  {
+    return $this->template;
+  }
 }

+ 6 - 1
tests/unit/Symfony/Components/Templating/Storage/FileStorageTest.php

@@ -14,7 +14,12 @@ require_once __DIR__.'/../../../../bootstrap.php';
 use Symfony\Components\Templating\Storage\Storage;
 use Symfony\Components\Templating\Storage\FileStorage;
 
-$t = new LimeTest(1);
+$t = new LimeTest(2);
 
 $storage = new FileStorage('foo');
 $t->ok($storage instanceof Storage, 'FileStorage is an instance of Storage');
+
+// ->getContent()
+$t->diag('->getContent()');
+$storage = new FileStorage(__DIR__.'/../../../../../fixtures/Symfony/Components/Templating/templates/foo.php');
+$t->is($storage->getContent(), '<?php echo $foo ?>', '->getContent() returns the content of the template');

+ 9 - 2
tests/unit/Symfony/Components/Templating/Storage/StorageTest.php

@@ -16,13 +16,20 @@ use Symfony\Components\Templating\Renderer\PhpRenderer;
 
 $t = new LimeTest(2);
 
+class TestStorage extends Storage
+{
+  public function getContent()
+  {
+  }
+}
+
 // __construct() __toString()
 $t->diag('__construct() __toString()');
 
-$storage = new Storage('foo');
+$storage = new TestStorage('foo');
 $t->is((string) $storage, 'foo', '__toString() returns the template name');
 
 // ->getRenderer()
 $t->diag('->getRenderer()');
-$storage = new Storage('foo', $renderer = new PhpRenderer());
+$storage = new TestStorage('foo', $renderer = new PhpRenderer());
 $t->ok($storage->getRenderer() === $renderer, '->getRenderer() returns the renderer');

+ 7 - 1
tests/unit/Symfony/Components/Templating/Storage/StringStorageTest.php

@@ -14,7 +14,13 @@ require_once __DIR__.'/../../../../bootstrap.php';
 use Symfony\Components\Templating\Storage\Storage;
 use Symfony\Components\Templating\Storage\StringStorage;
 
-$t = new LimeTest(1);
+$t = new LimeTest(2);
 
 $storage = new StringStorage('foo');
 $t->ok($storage instanceof Storage, 'StringStorage is an instance of Storage');
+
+
+// ->getContent()
+$t->diag('->getContent()');
+$storage = new StringStorage('foo');
+$t->is($storage->getContent(), 'foo', '->getContent() returns the content of the template');