1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Add a filesystem template loader.

The template loaders loads templates from a base directory (and can
optionally use a suffix). For example

    $templateLoader = new PHPParser_TemplateLoader(
        $parser, './templates', '.php'
    );

    // loads ./templates/TestTemplate.php
    $templateLoader->load('TestTemplate');

Again the implementation is not optimal. The loader probably shouldn't
intantiate the Template itself, but instead should accept a
TemplateFactory. This seemed like overkill to me, so I left it out.
This commit is contained in:
nikic 2012-04-03 23:52:00 +02:00
parent 19c1f80589
commit a048112e2c
2 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,48 @@
<?php
class PHPParser_TemplateLoader
{
protected $parser;
protected $baseDir;
protected $suffix;
/**
* Constructs a filesystem template loader.
*
* The templates are loaded from {baseDir}/{name}{suffix}.
*
* @param PHPParser_Parser $parser A PHP parser instance
* @param string $baseDir The base directory to load templates from
* @param string $suffix An optional suffix to append after the template name
*/
public function __construct(PHPParser_Parser $parser, $baseDir, $suffix = '') {
if (!is_dir($baseDir)) {
throw new InvalidArgumentException(
sprintf('The specified base directory "%s" does not exist', $baseDir)
);
}
$this->parser = $parser;
$this->baseDir = $baseDir;
$this->suffix = $suffix;
}
/**
* Loads the template with the specified name.
*
* @param string $name The name of template
*
* @return PHPParser_Template The loaded template
*/
public function load($name) {
$file = $this->baseDir . '/' . $name . $this->suffix;
if (!is_file($file)) {
throw new InvalidArgumentException(
sprintf('The file "%s" does not exist', $file)
);
}
return new PHPParser_Template($this->parser, file_get_contents($file));
}
}

View File

@ -0,0 +1,44 @@
<?php
class PHPParser_Tests_TemplateLoaderTest extends PHPUnit_Framework_TestCase
{
public function testLoadWithoutSuffix() {
$templateLoader = new PHPParser_TemplateLoader(
new PHPParser_Parser, dirname(__FILE__)
);
// load this file as a template, as we don't really care about the contents
$template = $templateLoader->load('TemplateLoaderTest.php');
$this->assertInstanceOf('PHPParser_Template', $template);
}
public function testLoadWithSuffix() {
$templateLoader = new PHPParser_TemplateLoader(
new PHPParser_Parser, dirname(__FILE__), '.php'
);
// load this file as a template, as we don't really care about the contents
$template = $templateLoader->load('TemplateLoaderTest');
$this->assertInstanceOf('PHPParser_Template', $template);
}
/**
* @expectedException InvalidArgumentException
*/
public function testNonexistentBaseDirectoryError() {
new PHPParser_TemplateLoader(
new PHPParser_Parser, dirname(__FILE__) . '/someDirectoryThatDoesNotExist'
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testNonexistentFileError() {
$templateLoader = new PHPParser_TemplateLoader(
new PHPParser_Parser, dirname(__FILE__)
);
$templateLoader->load('SomeTemplateThatDoesNotExist');
}
}