-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted TwigTemplateEngine from memio/pretty-printer 1.0.0-rc5
- Loading branch information
Loïc Chardonnet
committed
Apr 28, 2015
0 parents
commit c6fb8b9
Showing
56 changed files
with
1,407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Third party | ||
/composer.lock | ||
/phpunit.xml | ||
/vendor | ||
/.couscous | ||
/.puli |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
language: php | ||
|
||
sudo: false | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- hhvm | ||
|
||
matrix: | ||
# include: | ||
# - php: 5.3.3 | ||
# env: COMPOSER_FLAGS="--prefer-lowest" | ||
allow_failures: | ||
- php: hhvm | ||
- php: 7.0 | ||
|
||
before_script: | ||
- curl http://cs.sensiolabs.org/get/php-cs-fixer.phar -o php-cs-fixer.phar | ||
- composer selfupdate | ||
- composer update $COMPOSER_FLAGS | ||
|
||
script: | ||
- echo '[phpspec] Running specification tests'; ./vendor/bin/phpspec run -n -f dot | ||
- output=$(php -n php-cs-fixer.phar fix -v --dry-run --config=sf23 .); if [[ $(grep -o F <<< $output | wc -l) -gt 3 ]]; then while read -r line; do echo -e "\e[00;31m$line\e[00m"; done <<< "$output"; false; fi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# CHANGELOG | ||
|
||
## 1.0.0-rc1: Import | ||
|
||
* imported twig template engine from [memio/pretty-printer](http://github.com/memio/pretty-printer) v1.0.0-rc5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# How to contribute | ||
|
||
Everybody should be able to help. Here's how you can make this project more | ||
awesome: | ||
|
||
1. [Fork it](https://github.com/memio/twig-template-engine/fork_select) | ||
2. improve it | ||
3. submit a [pull request](https://help.github.com/articles/creating-a-pull-request) | ||
|
||
Your work will then be reviewed as soon as possible (suggestions about some | ||
changes, improvements or alternatives may be given). | ||
|
||
Here's some tips to make you the best contributor ever: | ||
|
||
* [Standard code](#standard-code) | ||
* [Specifications](#specifications) | ||
* [Keeping your fork up-to-date](#keeping-your-fork-up-to-date) | ||
|
||
## Standard code | ||
|
||
Use [PHP CS fixer](http://cs.sensiolabs.org/) to make your code compliant with | ||
Memio TwigTemplateEngine's coding standards: | ||
|
||
./vendor/bin/php-cs-fixer fix --config=sf23 . | ||
|
||
## Specifications | ||
|
||
Memio TwigTemplateEngine drives its development using [phpspec](http://www.phpspec.net/): | ||
|
||
# Generate the specification class: | ||
phpspec describe 'Memio\Memio\MyNewClass' | ||
|
||
# Customize the specification class: | ||
$EDITOR tests/spec/Memio/TwigTemplateEngine/MyNewClass.php | ||
|
||
# Generate the specified class: | ||
phpspec run | ||
|
||
# Customize the class: | ||
$EDITOR src/Memio/TwigTemplateEngine/MyNewClass.php | ||
|
||
phpspec run # Should be green! | ||
|
||
## Keeping your fork up-to-date | ||
|
||
To keep your fork up-to-date, you should track the upstream (original) one | ||
using the following command: | ||
|
||
git remote add upstream https://github.com/memio/twig-template-engine.git | ||
|
||
Then get the upstream changes: | ||
|
||
git checkout master | ||
git pull --rebase origin master | ||
git pull --rebase upstream master | ||
git checkout <your-branch> | ||
git rebase master | ||
|
||
Finally, publish your changes: | ||
|
||
git push -f origin <your-branch> | ||
|
||
Your pull request will be automatically updated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2015 Loïc Chardonnet | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# Memio's TwigTemplateEngine [![SensioLabsInsight](https://insight.sensiolabs.com/projects/c8194cd1-0f80-4bce-9ab2-8368db5411b3/mini.png)](https://insight.sensiolabs.com/projects/c8194cd1-0f80-4bce-9ab2-8368db5411b3) [![Travis CI](https://travis-ci.org/memio/twig-template-engine.png)](https://travis-ci.org/memio/twig-template-engine) | ||
|
||
`PrettyPrinter` is a code generator (printer) that takes a Model and calls the | ||
appropriate `TemplateEngine` to actually generate the corresponding code, | ||
using highly opinionated coding standards (pretty). | ||
|
||
`PrettyPrinter` returns a string that can be saved in a file, dislpayed on a | ||
console output or displayed in a web page. Possibilities are endless! | ||
|
||
> **Note**: This package is part of [Memio](http://memio.github.io/memio). | ||
> Have a look at [the main repository](http://github.com/memio/memio). | ||
## Installation | ||
|
||
Install it using [Composer](https://getcomposer.org/download): | ||
|
||
composer require memio/twig-template-engine:~1.0@rc | ||
|
||
## Example | ||
|
||
We're going to generate a class with a constructor and two attributes: | ||
|
||
```php | ||
<?php | ||
|
||
require __DIR__.'/vendor/autoload.php'; | ||
|
||
use Memio\TwigTemplateEngine\TwigTemplateEngine; | ||
use Memio\TwigTemplateEngine\TwigExtension\Line\ContractLineStrategy; | ||
use Memio\TwigTemplateEngine\TwigExtension\Line\FileLineStrategy; | ||
use Memio\TwigTemplateEngine\TwigExtension\Line\Line; | ||
use Memio\TwigTemplateEngine\TwigExtension\Line\MethodPhpdocLineStrategy; | ||
use Memio\TwigTemplateEngine\TwigExtension\Line\ObjectLineStrategy; | ||
use Memio\TwigTemplateEngine\TwigExtension\Line\StructurePhpdocLineStrategy; | ||
use Memio\TwigTemplateEngine\TwigExtension\Type; | ||
use Memio\TwigTemplateEngine\TwigExtension\Whitespace; | ||
use Memio\TwigTemplateEngine\TwigTemplateEngine; | ||
use Memio\Model\File; | ||
use Memio\Model\Object; | ||
use Memio\Model\Property; | ||
use Memio\Model\Method; | ||
use Memio\Model\Argument; | ||
|
||
// Initialize the code generator | ||
$loader = new \Twig_Loader_Filesystem(__DIR__.'/templates'); | ||
$twig = new \Twig_Environment($loader); | ||
|
||
$line = new Line(); | ||
$line->add(new ContractLineStrategy()); | ||
$line->add(new FileLineStrategy()); | ||
$line->add(new MethodPhpdocLineStrategy()); | ||
$line->add(new ObjectLineStrategy()); | ||
$line->add(new StructurePhpdocLineStrategy()); | ||
|
||
$twig->addExtension(new Type()); | ||
$twig->addExtension(new Whitespace($line)); | ||
|
||
$templateEngine = new TwigTemplateEngine($twig); | ||
$prettyPrinter = new PrettyPrinter($templateEngine); | ||
|
||
// Describe the code you want to generate using "Models" | ||
$myService = File::make('src/Vendor/Project/MyService.php') | ||
->setStructure( | ||
Object::make('Vendor\Project\MyService') | ||
->addProperty(new Property('createdAt')) | ||
->addProperty(new Property('filename')) | ||
->addMethod( | ||
Method::make('__construct') | ||
->addArgument(new Argument('DateTime', 'createdAt')) | ||
->addArgument(new Argument('string', 'filename')) | ||
) | ||
) | ||
; | ||
|
||
// Generate the code and display in the console | ||
echo $prettyPrinter->generateCode($myService); | ||
|
||
// Or display it in a browser | ||
// echo '<pre>'.htmlspecialchars($prettyPrinter->generateCode($myService)).'</pre>'; | ||
``` | ||
|
||
With this simple example, we get the following output: | ||
|
||
``` | ||
<?php | ||
namespace Vendor\Project; | ||
class MyService | ||
{ | ||
private $createdAt; | ||
private $filename; | ||
public function __construct(DateTime $createdAt, $filename) | ||
{ | ||
} | ||
} | ||
``` | ||
|
||
Have a look at [the main respository](http://github.com/memio/memio) to discover the full power of Memio. | ||
|
||
## Want to know more? | ||
|
||
Memio uses [phpspec](http://phpspec.net/), which means the tests also provide the documentation. | ||
Not convinced? Then clone this repository and run the following commands: | ||
|
||
composer install | ||
./vendor/bin/phpspec run -n -f pretty | ||
|
||
You can see the current and past versions using one of the following: | ||
|
||
* the `git tag` command | ||
* the [releases page on Github](https://github.com/memio/memio/releases) | ||
* the file listing the [changes between versions](CHANGELOG.md) | ||
|
||
And finally some meta documentation: | ||
|
||
* [copyright and MIT license](LICENSE) | ||
* [versioning and branching models](VERSIONING.md) | ||
* [contribution instructions](CONTRIBUTING.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Versioning and branching models | ||
|
||
This file explains the versioning and branching models of this project. | ||
|
||
## Versioning | ||
|
||
The versioning is inspired by [Semantic Versioning](http://semver.org/): | ||
|
||
> Given a version number MAJOR.MINOR.PATCH, increment the: | ||
> | ||
> 1. MAJOR version when you make incompatible API changes | ||
> 2. MINOR version when you add functionality in a backwards-compatible manner | ||
> 3. PATCH version when you make backwards-compatible bug fixes | ||
### Public API | ||
|
||
Classes and methods marked with the `@api` tag are considered to be the public | ||
API of this project. | ||
|
||
## Branching Model | ||
|
||
The branching is inspired by [@jbenet](https://github.com/jbenet) | ||
[simple git branching model](https://gist.github.com/jbenet/ee6c9ac48068889b0912): | ||
|
||
> 1. `master` must always be deployable. | ||
> 2. **all changes** are made through feature branches (pull-request + merge) | ||
> 3. rebase to avoid/resolve conflicts; merge in to `master` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "memio/twig-template-engine", | ||
"license": "MIT", | ||
"type": "library", | ||
"description": "Memio's Twig templates, used to generate PHP code from given Model", | ||
"keywords": ["generator", "PHP", "code", "twig", "templates"], | ||
"homepage": "http://memio.github.io/memio", | ||
"authors": [ | ||
{ | ||
"name": "Loïc Chardonnet", | ||
"email": "[email protected]", | ||
"homepage": "http://gnugat.github.io", | ||
"role": "Developer" | ||
} | ||
], | ||
"autoload": { "psr-4": { | ||
"Memio\\TwigTemplateEngine\\": "src/Memio/TwigTemplateEngine", | ||
"Memio\\TwigTemplateEngine\\Config\\": "config" | ||
}}, | ||
"require": { | ||
"memio/model": "~1.0", | ||
"memio/pretty-printer": "~1.0@rc", | ||
"php": ">=5.3.3", | ||
"twig/twig": "~1.18" | ||
}, | ||
"require-dev": { | ||
"ciaranmcnulty/phpspec-typehintedmethods": "~1.1", | ||
"fabpot/php-cs-fixer": "~1.6", | ||
"phpspec/phpspec": "~2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Memio\PrettyPrinter\Config; | ||
|
||
class Locate | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public static function templates() | ||
{ | ||
return __DIR__.'/../templates'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
extensions: | ||
- Cjm\PhpSpec\Extension\TypeHintedMethodsExtension |
38 changes: 38 additions & 0 deletions
38
spec/Memio/TwigTemplateEngine/TwigExtension/Line/ContractLineStrategySpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the memio/twig-template-engine package. | ||
* | ||
* (c) Loïc Chardonnet <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace spec\Memio\TwigTemplateEngine\TwigExtension\Line; | ||
|
||
use Memio\Model\Contract; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class ContractLineStrategySpec extends ObjectBehavior | ||
{ | ||
const CONSTANT_BLOCK = 'constants'; | ||
|
||
function it_is_a_line_strategy() | ||
{ | ||
$this->shouldImplement('Memio\TwigTemplateEngine\TwigExtension\Line\LineStrategy'); | ||
} | ||
|
||
function it_supports_contracts(Contract $contract) | ||
{ | ||
$this->supports($contract)->shouldBe(true); | ||
} | ||
|
||
function it_needs_line_after_constants_if_contract_has_both_constants_and_methods(Contract $contract) | ||
{ | ||
$contract->allConstants()->willReturn(array(1)); | ||
$contract->allMethods()->willReturn(array(2)); | ||
|
||
$this->needsLineAfter($contract, self::CONSTANT_BLOCK)->shouldBe(true); | ||
} | ||
} |
Oops, something went wrong.