From 94eca2ce4494a19bf45502600ed40c1faa683d75 Mon Sep 17 00:00:00 2001 From: nikic Date: Fri, 12 Sep 2014 00:25:30 +0200 Subject: [PATCH] Remove deprecated Template and TemplateLoader --- doc/4_Code_generation.markdown | 215 +------------------------- lib/PhpParser/Autoloader.php | 2 - lib/PhpParser/Template.php | 77 --------- lib/PhpParser/TemplateLoader.php | 53 ------- test/PhpParser/TemplateLoaderTest.php | 50 ------ test/PhpParser/TemplateTest.php | 61 -------- 6 files changed, 2 insertions(+), 456 deletions(-) delete mode 100644 lib/PhpParser/Template.php delete mode 100644 lib/PhpParser/TemplateLoader.php delete mode 100644 test/PhpParser/TemplateLoaderTest.php delete mode 100644 test/PhpParser/TemplateTest.php diff --git a/doc/4_Code_generation.markdown b/doc/4_Code_generation.markdown index 3f46fb6..c75e23c 100644 --- a/doc/4_Code_generation.markdown +++ b/doc/4_Code_generation.markdown @@ -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 -__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 - '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 -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 -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'); -``` \ No newline at end of file diff --git a/lib/PhpParser/Autoloader.php b/lib/PhpParser/Autoloader.php index 4498bc5..836f91d 100644 --- a/lib/PhpParser/Autoloader.php +++ b/lib/PhpParser/Autoloader.php @@ -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', diff --git a/lib/PhpParser/Template.php b/lib/PhpParser/Template.php deleted file mode 100644 index 7a36d7d..0000000 --- a/lib/PhpParser/Template.php +++ /dev/null @@ -1,77 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/lib/PhpParser/TemplateLoader.php b/lib/PhpParser/TemplateLoader.php deleted file mode 100644 index 411ea32..0000000 --- a/lib/PhpParser/TemplateLoader.php +++ /dev/null @@ -1,53 +0,0 @@ -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)); - } -} \ No newline at end of file diff --git a/test/PhpParser/TemplateLoaderTest.php b/test/PhpParser/TemplateLoaderTest.php deleted file mode 100644 index 29a6d2a..0000000 --- a/test/PhpParser/TemplateLoaderTest.php +++ /dev/null @@ -1,50 +0,0 @@ -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'); - } -} \ No newline at end of file diff --git a/test/PhpParser/TemplateTest.php b/test/PhpParser/TemplateTest.php deleted file mode 100644 index 6fe2053..0000000 --- a/test/PhpParser/TemplateTest.php +++ /dev/null @@ -1,61 +0,0 @@ -assertEquals( - $expectedPrettyPrint, - $prettyPrinter->prettyPrint($template->getStmts($placeholders)) - ); - } - - public function provideTestPlaceholderReplacement() { - return array( - array( - ' 'foo'), - '$foo + $Foo;' - ), - array( - ' 'Foo'), - '$foo + $Foo;' - ), - array( - ' 'foo', 'Name' => 'Bar'), - '$foo + $Bar;' - ), - array( - ' 'Bar', 'name' => 'foo'), - '$foo + $Bar;' - ), - array( - ' 'infix'), - '$prefixInfixSuffix;' - ), - array( - ' 'foo'), - '$_foo_;' - ), - array( - '