mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Add simple templating support.
Templates use __name__ placeholders. A variant of the placeholder with a capitalized first latter can be accessed using __Name__ (this is useful for camel case identifiers, e.g. get__Name__). Currently the implemention is not particularly clean, because the Template instantiates a Lexer itself. Fixing this requires a major refactoring of the lexer/parser interface.
This commit is contained in:
parent
72586235c4
commit
19c1f80589
84
lib/PHPParser/Template.php
Normal file
84
lib/PHPParser/Template.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Template
|
||||
{
|
||||
protected $parser;
|
||||
protected $template;
|
||||
|
||||
/**
|
||||
* Creates a new code template from a template string.
|
||||
*
|
||||
* @param PHPParser_Parser $parser A parser instance
|
||||
* @param string $template The template string
|
||||
*/
|
||||
public function __construct(PHPParser_Parser $parser, $template) {
|
||||
$this->parser = $parser;
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the statements of the template with the passed in placeholders
|
||||
* replaced.
|
||||
*
|
||||
* @param array $placeholders Placeholders
|
||||
*
|
||||
* @return PHPParser_Node[] Statements
|
||||
*/
|
||||
public function getStmts(array $placeholders) {
|
||||
/*
|
||||
* TODO This is evil.
|
||||
* The lexer shouldn't be created in here, instead it should be a dependency, which
|
||||
* basically means that we'd need to have a LexerFactory (which seems strange).
|
||||
* An alternative solution would be to make the lexer work similar to how the parser
|
||||
* works. I.e. one would instantiate the Lexer only once and then pass the results
|
||||
* of ->lex() to the parser (which would then be the full tokens array). This design
|
||||
* seems cleaner, but comes at the expense of higher memory consumption, as the token
|
||||
* array can be quite large.
|
||||
*/
|
||||
return $this->parser->parse(
|
||||
new PHPParser_Lexer_Emulative(
|
||||
$this->getTemplateWithPlaceholdersReplaced($placeholders)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function getTemplateWithPlaceholdersReplaced(array $placeholders) {
|
||||
if (empty($placeholders)) {
|
||||
return $this->template;
|
||||
}
|
||||
|
||||
return strtr($this->template, $this->preparePlaceholders($placeholders));
|
||||
}
|
||||
|
||||
/*
|
||||
* Prepare the placeholders for replacement. This means that
|
||||
* a) all placeholders will be surrounded with __.
|
||||
* b) ucfirst/lcfirst variations of the placeholders are generated.
|
||||
*
|
||||
* E.g. for an input array of ['foo' => 'bar'] the result will be
|
||||
* ['__foo__' => 'bar', '__Foo__' => 'Bar'].
|
||||
*/
|
||||
protected function preparePlaceholders(array $placeholders) {
|
||||
$preparedPlaceholders = array();
|
||||
|
||||
foreach ($placeholders as $name => $value) {
|
||||
$preparedPlaceholders['__' . $name . '__'] = $value;
|
||||
|
||||
if (ctype_lower($name[0])) {
|
||||
$ucfirstName = ucfirst($name);
|
||||
if (!isset($placeholders[$ucfirstName])) {
|
||||
$preparedPlaceholders['__' . $ucfirstName . '__'] = ucfirst($value);
|
||||
}
|
||||
}
|
||||
|
||||
if (ctype_upper($name[0])) {
|
||||
$lcfirstName = lcfirst($name);
|
||||
if (!isset($placeholders[$lcfirstName])) {
|
||||
$preparedPlaceholders['__' . $lcfirstName . '__'] = lcfirst($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $preparedPlaceholders;
|
||||
}
|
||||
}
|
54
test/PHPParser/Tests/TemplateTest.php
Normal file
54
test/PHPParser/Tests/TemplateTest.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
class PHPParser_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideTestPlaceholderReplacement
|
||||
* @covers PHPParser_Template
|
||||
*/
|
||||
public function testPlaceholderReplacement($templateCode, $placeholders, $expectedPrettyPrint) {
|
||||
$parser = new PHPParser_Parser;
|
||||
$prettyPrinter = new PHPParser_PrettyPrinter_Zend;
|
||||
|
||||
$template = new PHPParser_Template($parser, $templateCode);
|
||||
$this->assertEquals(
|
||||
$expectedPrettyPrint,
|
||||
$prettyPrinter->prettyPrint($template->getStmts($placeholders))
|
||||
);
|
||||
}
|
||||
|
||||
public function provideTestPlaceholderReplacement() {
|
||||
return array(
|
||||
array(
|
||||
'<?php $__name__ + $__Name__;',
|
||||
array('name' => 'foo'),
|
||||
'$foo + $Foo;'
|
||||
),
|
||||
array(
|
||||
'<?php $__name__ + $__Name__;',
|
||||
array('Name' => 'Foo'),
|
||||
'$foo + $Foo;'
|
||||
),
|
||||
array(
|
||||
'<?php $__name__ + $__Name__;',
|
||||
array('name' => 'foo', 'Name' => 'Bar'),
|
||||
'$foo + $Bar;'
|
||||
),
|
||||
array(
|
||||
'<?php $__name__ + $__Name__;',
|
||||
array('Name' => 'Bar', 'name' => 'foo'),
|
||||
'$foo + $Bar;'
|
||||
),
|
||||
array(
|
||||
'<?php $prefix__Name__Suffix;',
|
||||
array('name' => 'infix'),
|
||||
'$prefixInfixSuffix;'
|
||||
),
|
||||
array(
|
||||
'<?php $___name___;',
|
||||
array('name' => 'foo'),
|
||||
'$_foo_;'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user