1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-20 12:46:47 +01:00

Remove deprecated Template and TemplateLoader

This commit is contained in:
nikic 2014-09-12 00:25:30 +02:00
parent e65fd664d1
commit 94eca2ce44
6 changed files with 2 additions and 456 deletions

View File

@ -3,13 +3,8 @@ Code generation
It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the
pretty printer to convert it to PHP code. To simplify code generation, the project comes with a set of builders for
common structures as well as simple templating support. Both features are described in the following:
Builders
--------
The project provides builders for classes, interfaces, methods, functions, parameters and properties, which
allow creating node trees with a fluid interface, instead of instantiating all nodes manually.
classes, interfaces, methods, functions, parameters and properties. The builders allow creating node trees using a
fluid interface, instead of instantiating all nodes manually.
Here is an example:
@ -59,209 +54,3 @@ abstract class SomeClass extends SomeOtherClass implements A\Few, Interfaces
}
}
```
Templates
---------
> **DEPRECATED**: This feature is deprecated and will be removed in PHP-Parser 1.0.
Additionally it is possible to generate code from reusable templates.
As an example consider the following template, which defines a general getter/setter skeleton in terms of a property
`__name__` and its `__type__`:
```php
<?php
class GetterSetterTemplate
{
/**
* @var __type__ The __name__
*/
protected $__name__;
/**
* Gets the __name__.
*
* @return __type__ The __name__
*/
public function get__Name__() {
return $this->__name__;
}
/**
* Sets the __name__.
*
* @param __type__ $__name__ The new __name__
*/
public function set__Name__($__name__) {
$this->__name__ = $__name__;
}
}
```
Using this template we can easily create a class with multiple properties and their respective getters and setters:
```php
<?php
// $templateString contains the above template
$template = new PhpParser\Template($parser, $templateString);
// We only have to specify the __name__ placeholder, as the
// capitalized __Name__ placeholder is automatically created
$properties = [
['name' => 'title', 'type' => 'string'],
['name' => 'body', 'type' => 'string'],
['name' => 'author', 'type' => 'User'],
['name' => 'timestamp', 'type' => 'DateTime'],
];
$class = $factory->class('BlogPost')->implement('Post');
foreach ($properties as $propertyPlaceholders) {
$stmts = $template->getStmts($propertyPlaceholders);
$class->addStmts(
// $stmts contains all statements from the template. So [0] fetches the class statement
// and ->stmts retrieves the methods.
$stmts[0]->stmts
);
}
echo $prettyPrinter->prettyPrint(array($class->getNode()));
```
The result would look roughly like this:
```php
<?php
class BlogPost implements Post
{
/**
* @var string The title
*/
protected $title;
/**
* @var string The body
*/
protected $body;
/**
* @var User The author
*/
protected $author;
/**
* @var DateTime The timestamp
*/
protected $timestamp;
/**
* Gets the title.
*
* @return string The title
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets the title.
*
* @param string $title The new title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Gets the body.
*
* @return string The body
*/
public function getBody()
{
return $this->body;
}
/**
* Sets the body.
*
* @param string $body The new body
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* Gets the author.
*
* @return User The author
*/
public function getAuthor()
{
return $this->author;
}
/**
* Sets the author.
*
* @param User $author The new author
*/
public function setAuthor($author)
{
$this->author = $author;
}
/**
* Gets the timestamp.
*
* @return DateTime The timestamp
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* Sets the timestamp.
*
* @param DateTime $timestamp The new timestamp
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
}
}
```
When using multiple templates it is easier to manage them on the filesystem. They can be loaded using the
`TemplateLoader`:
```php
<?php
// We'll store our templates in ./templates and give them a .php suffix
$loader = new PhpParser\TemplateLoader($parser, './templates', '.php');
// loads ./templates/GetterSetter.php
$getterSetterTemplate = $loader->load('GetterSetter');
// loads ./templates/Collection.php
$collectionTemplate = $loader->load('Collection');
// The use of a suffix is optional. The following code for example is equivalent:
$loader = new PhpParser\TemplateLoader($parser, './templates');
// loads ./templates/GetterSetter.php
$getterSetterTemplate = $loader->load('GetterSetter.php');
// loads ./templates/Collection.php
$collectionTemplate = $loader->load('Collection.php');
```

View File

@ -64,8 +64,6 @@ class Autoloader
'PHPParser_PrettyPrinter_Zend' => 'PhpParser\PrettyPrinter\Standard',
'PHPParser_Serializer' => 'PhpParser\Serializer',
'PHPParser_Serializer_XML' => 'PhpParser\Serializer\XML',
'PHPParser_Template' => 'PhpParser\Template',
'PHPParser_TemplateLoader' => 'PhpParser\TemplateLoader',
'PHPParser_Unserializer' => 'PhpParser\Unserializer',
'PHPParser_Unserializer_XML' => 'PhpParser\Unserializer\XML',

View File

@ -1,77 +0,0 @@
<?php
namespace PhpParser;
/**
* @deprecated
*/
class Template
{
protected $parser;
protected $template;
/**
* Creates a new code template from a template string.
*
* @param Parser $parser A parser instance
* @param string $template The template string
*/
public function __construct(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 Node[] Statements
*/
public function getStmts(array $placeholders) {
return $this->parser->parse(
$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;
}
}

View File

@ -1,53 +0,0 @@
<?php
namespace PhpParser;
/**
* @deprecated
*/
class TemplateLoader
{
protected $parser;
protected $baseDir;
protected $suffix;
/**
* Constructs a filesystem template loader.
*
* The templates are loaded from {baseDir}/{name}{suffix}.
*
* @param 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(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 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 Template($this->parser, file_get_contents($file));
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace PhpParser;
class TemplateLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testLoadWithoutSuffix() {
$templateLoader = new TemplateLoader(
new Parser(new Lexer),
__DIR__
);
// 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 TemplateLoader(
new Parser(new Lexer),
__DIR__, '.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 TemplateLoader(
new Parser(new Lexer),
__DIR__ . '/someDirectoryThatDoesNotExist'
);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testNonexistentFileError() {
$templateLoader = new TemplateLoader(
new Parser(new Lexer),
__DIR__
);
$templateLoader->load('SomeTemplateThatDoesNotExist');
}
}

View File

@ -1,61 +0,0 @@
<?php
namespace PhpParser;
class TemplateTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideTestPlaceholderReplacement
* @covers Template
*/
public function testPlaceholderReplacement($templateCode, $placeholders, $expectedPrettyPrint) {
$parser = new Parser(new Lexer);
$prettyPrinter = new PrettyPrinter\Standard;
$template = new 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_;'
),
array(
'<?php $foobar;',
array(),
'$foobar;'
),
);
}
}