Skip to content

Commit

Permalink
Extracted PrettyPrinter from memio/memio 1.0.0-rc10
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Chardonnet committed Apr 22, 2015
0 parents commit 9c1b951
Show file tree
Hide file tree
Showing 68 changed files with 1,987 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Third party
/composer.lock
/phpunit.xml
/vendor
/.couscous
/.puli
32 changes: 32 additions & 0 deletions .travis.yml
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;
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# CHANGELOG

## 1.0.0-rc-1: Import

* imported pretty printer from [memio/memio](http://github.com/memio/memio) v1.0.0-rc10
63 changes: 63 additions & 0 deletions CONTRIBUTING.md
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/pretty-printer/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 PrettyPrinter's coding standards:

./vendor/bin/php-cs-fixer fix --config=sf23 .

## Specifications

Memio PrettyPrinter 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/pretty-printer/MyNewClass.php

# Generate the specified class:
phpspec run

# Customize the class:
$EDITOR src/Memio/pretty-printer/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/pretty-printer.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.
19 changes: 19 additions & 0 deletions LICENSE
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.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Memio's PrettyPrinter [![SensioLabsInsight](https://insight.sensiolabs.com/projects/a2b24423-9840-45ab-a011-598aa3ba26bf/mini.png)](https://insight.sensiolabs.com/projects/a2b24423-9840-45ab-a011-598aa3ba26bf) [![Travis CI](https://travis-ci.org/memio/pretty-printer.png)](https://travis-ci.org/memio/pretty-printer)

`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/pretty-printer:~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\Memio\PrettyPrinter;
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__.'/vendor/memio/pretty-printer/templates');
$twig = new \Twig_Environment($loader);
$prettyPrinter = new PrettyPrinter($twig);

// 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)
27 changes: 27 additions & 0 deletions VERSIONING.md
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`
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "memio/pretty-printer",
"license": "MIT",
"type": "library",
"description": "Memio's PrettyPrinter, used to generate PHP code from given Model",
"keywords": ["generator", "PHP", "code"],
"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\\PrettyPrinter\\": "src/Memio/PrettyPrinter"
}},
"require": {
"memio/model": "~1.0",
"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"
}
}
2 changes: 2 additions & 0 deletions phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extensions:
- Cjm\PhpSpec\Extension\TypeHintedMethodsExtension
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the memio/pretty-printer 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\PrettyPrinter\PrettyPrinter;

use Memio\Model\Argument;
use PhpSpec\ObjectBehavior;

class EmptyCollectionPrettyPrinterSpec extends ObjectBehavior
{
function it_is_a_pretty_printer_strategy()
{
$this->shouldImplement('Memio\PrettyPrinter\PrettyPrinter\PrettyPrinterStrategy');
}

function it_supports_empty_arrays()
{
$this->supports(array(), array())->shouldBe(true);
}

function it_generates_an_empty_string()
{
$this->generateCode(array())->shouldBe('');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the memio/pretty-printer 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\PrettyPrinter\PrettyPrinter;

use Memio\Model\Argument;
use PhpSpec\ObjectBehavior;
use Twig_Environment;

class ModelCollectionPrettyPrinterSpec extends ObjectBehavior
{
function let(Twig_Environment $twig)
{
$this->beConstructedWith($twig);
}

function it_is_a_pretty_printer_strategy()
{
$this->shouldImplement('Memio\PrettyPrinter\PrettyPrinter\PrettyPrinterStrategy');
}

function it_supports_array_of_models()
{
$argument = new Argument('string', 'filename');
$arguments = array($argument);

$this->supports($arguments, array())->shouldBe(true);
}

function it_generates_code_using_collection_templates(Twig_Environment $twig)
{
$argument = new Argument('string', 'filename');
$arguments = array($argument);

$twig->render('collection/argument_collection.twig', array('argument_collection' => $arguments))->shouldBeCalled();

$this->generateCode($arguments);
}
}
Loading

0 comments on commit 9c1b951

Please sign in to comment.