Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MacFJA committed Mar 5, 2016
0 parents commit 3bdf988
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/phpcs.xml.dist export-ignore
/tests export-ignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build
composer.lock
docs
vendor
coverage.clover
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/phptransformers/blade).


## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.


## Running Tests

``` bash
$ composer test
```


**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2016 [MacFJA](https://github.com/MacFJA)

> 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.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Blade for PHPTransformers

[Laravel Blade](https://laravel.com/docs/master/blade) support for [PHPTransformers](http://github.com/phptransformers/phptransformer).

## Install

Via Composer

``` bash
$ composer require phptransformers/blade
```

## Usage

``` php
$engine = new BladeTransformer();
echo $engine->render('Hello, {{ $name }}!', array('name' => 'phptransformers'));
```


### Options

``` php
$engine = new BladeTransformer(array(
'cache-dir' => 'path/to/the/cache', // Default to the system temporary directory
));

// ...

$blade = new Illuminate\View\Compilers\BladeCompiler(
new Illuminate\Filesystem\Filesystem(),
sys_get_temp_dir()
);
$engine = new BladeTransformer(array(
'blade' => $blade
));
```

## Testing

``` bash
$ composer test
```

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "phptransformers/blade",
"description": "Laravel Blade implementation of PhpTransformer.",
"keywords": [
"PhpTransformers", "PhpTransformer", "Blade", "Laravel", "Template Engine", "Templates"
],
"homepage": "https://github.com/phptransformers/blade",
"license": "MIT",
"authors": [
{
"name": "MacFJA",
"role": "Developer"
}
],
"require": {
"php" : ">=5.3.0",
"phptransformers/phptransformer": "~1.0.0",
"illuminate/view": "^5.2"
},
"require-dev": {
"phpunit/phpunit" : "~4.6",
"squizlabs/php_codesniffer": "~2.3",
"phpmd/phpmd": "^2.3",
"sebastian/phpcpd": "^2.0"
},
"autoload": {
"psr-4": {
"PhpTransformers\\Blade\\": "lib"
}
},
"autoload-dev": {
"psr-4": {
"PhpTransformers\\Blade\\Test\\": "tests"
}
},
"scripts": {
"test": [
"phpcs",
"phpmd lib/ text cleancode,codesize,controversial,design,naming,unusedcode",
"phpcpd lib/",
"phpunit --colors=always --coverage-text --coverage-clover=coverage.clover"
]
}
}
86 changes: 86 additions & 0 deletions lib/BladeTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace PhpTransformers\Blade;

use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use PhpTransformers\PhpTransformer\TransformerInterface;

/**
* Class BladeTransformer.
*
* The PhpTransformer for Laravel Blade template engine.
* {@link https://laravel.com/docs/master/blade}
*
* @author MacFJA
* @package PhpTransformers\Blade
* @license MIT
*/
class BladeTransformer implements TransformerInterface
{
protected $blade;

/**
* The transformer constructor.
*
* Options are:
* - "blade" a Illuminate\View\Compilers\BladeCompiler instance
* - "cache-dir" the directory where Blade will search and store templates output
* if the option "blade" is provided, the option "cache-dir" is ignored.
*
* @param array $options The BladeTransformer options
*/
public function __construct(array $options = array())
{
if (array_key_exists('blade', $options)) {
$this->blade = $options['blade'];
return;
}

$cacheDir = sys_get_temp_dir();
if (array_key_exists('cache-dir', $options)) {
$cacheDir = $options['cache-dir'];
}

$this->blade = new BladeCompiler(new Filesystem(), $cacheDir);
}

public function getName()
{
return 'blade';
}

public function renderFile($file, array $locals = array())
{
if ($this->blade->isExpired($file)) {
// Compile Blade template into PHP code
$this->blade->compile($file);
}

// Get the path of the compiled template
$path = $this->blade->getCompiledPath($file);

// Render the template
ob_start();
extract($locals, EXTR_SKIP);
include $path;
return ltrim(ob_get_clean());
}

public function render($template, array $locals = array())
{
// Compile Blade template into PHP code
$data = $this->blade->compileString($template);

// Create the temporary PHP file that will contains the compiled PHP code
$path = tempnam(sys_get_temp_dir(), 'blade');
file_put_contents($path, $data);

ob_start();
extract($locals, EXTR_SKIP);
include $path;
$result = ob_get_clean();
unlink($path);
return $result;
}
}
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<ruleset name="PSR-2 Standard">
<rule ref="PSR2"/>
<file>./lib</file>
</ruleset>
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="PHPTransformers">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">lib/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
85 changes: 85 additions & 0 deletions tests/BladeTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace PhpTransformers\Blade\Test;

use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use PhpTransformers\Blade\BladeTransformer;

class BladeTransformerTest extends \PHPUnit_Framework_TestCase
{
public function testRenderFile()
{
$engine = new BladeTransformer();
$template = 'tests/Fixtures/BladeTransformer.tpl';
$locals = array(
'name' => 'Linus',
);
$actual = $engine->renderFile($template, $locals);
self::assertEquals('Hello, Linus!', $actual);
}

public function testRender()
{
$engine = new BladeTransformer();
$template = file_get_contents('tests/Fixtures/BladeTransformer.tpl');
$locals = array(
'name' => 'Linus',
);
$actual = $engine->render($template, $locals);
self::assertEquals('Hello, Linus!', $actual);
}

public function testGetName()
{
$engine = new BladeTransformer();
self::assertEquals('blade', $engine->getName());
}

public function testConstructor()
{
$blade = new BladeCompiler(new Filesystem(), sys_get_temp_dir());
$engine = new BladeTransformer(array('blade' => $blade));

$template = 'tests/Fixtures/BladeTransformer.tpl';
$locals = array(
'name' => 'Linus',
);
$actual = $engine->renderFile($template, $locals);
self::assertEquals('Hello, Linus!', $actual);
}

public function testExpired()
{
// Clean-up cache dir
$path = __DIR__.DIRECTORY_SEPARATOR.'cache';
if (!is_dir($path)) {
mkdir($path);
} else {
foreach (glob($path.DIRECTORY_SEPARATOR.'*') as $file) {
unlink($file);
}
}

$engine = new BladeTransformer(array('cache-dir' => $path));

$template = 'tests/Fixtures/BladeTransformer.tpl';
$locals = array(
'name' => 'Linus',
);

// By checking the code coverage, we can see that the first times, `compile` is executed ...
$actual = $engine->renderFile($template, $locals);
self::assertEquals('Hello, Linus!', $actual);

// ... but not the second times
$actual = $engine->renderFile($template, $locals);
self::assertEquals('Hello, Linus!', $actual);

// Clean-up cache dir
foreach (glob($path.DIRECTORY_SEPARATOR.'*') as $file) {
unlink($file);
}
rmdir($path);
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/BladeTransformer.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, {{ $name }}!

0 comments on commit 3bdf988

Please sign in to comment.