Merge branch 'a'
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
composer.lock
|
||||||
|
vendor
|
||||||
|
data/cache/*.php
|
34
composer.json
Normal file
34
composer.json
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
"name": "hiropeke/slim-blade-view",
|
||||||
|
"type": "library",
|
||||||
|
"description": "Slim Framework 3 view helper built on top of the Blade component",
|
||||||
|
"keywords": [
|
||||||
|
"slim",
|
||||||
|
"framework",
|
||||||
|
"view",
|
||||||
|
"template",
|
||||||
|
"blade",
|
||||||
|
"renderer"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Hiroaki Matsuura",
|
||||||
|
"email": "hiropeke.jp@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"illuminate/view": "5.*",
|
||||||
|
"philo/laravel-blade": "3.*",
|
||||||
|
"psr/http-message": "^1.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Slim\\Views\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^5.0",
|
||||||
|
"slim/slim": "^3.0"
|
||||||
|
}
|
||||||
|
}
|
1
data/cache/.gitkeep
vendored
Normal file
1
data/cache/.gitkeep
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
#!/usr/bin/env bash
|
24
phpunit.xml.dist
Normal file
24
phpunit.xml.dist
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<phpunit backupGlobals="false"
|
||||||
|
backupStaticAttributes="false"
|
||||||
|
colors="true"
|
||||||
|
convertErrorsToExceptions="true"
|
||||||
|
convertNoticesToExceptions="true"
|
||||||
|
convertWarningsToExceptions="true"
|
||||||
|
processIsolation="false"
|
||||||
|
stopOnFailure="false"
|
||||||
|
syntaxCheck="false"
|
||||||
|
>
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Renderer Tests">
|
||||||
|
<directory>tests/</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<filter>
|
||||||
|
<whitelist>
|
||||||
|
<directory>src/</directory>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
</phpunit>
|
169
src/Blade.php
Normal file
169
src/Blade.php
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Slim\Views;
|
||||||
|
|
||||||
|
use Illuminate\Events\Dispatcher;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
class Blade
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $viewPaths;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $cachePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $attributes;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blade constructor.
|
||||||
|
* @param array $viewPaths
|
||||||
|
* @param $cachePath
|
||||||
|
* @param Dispatcher|null $events
|
||||||
|
*/
|
||||||
|
public function __construct($viewPaths = [], $cachePath = '', Dispatcher $events = null, $attributes = [])
|
||||||
|
{
|
||||||
|
if (is_string($viewPaths)) {
|
||||||
|
$viewPaths = [$viewPaths];
|
||||||
|
}
|
||||||
|
$this->viewPaths = $viewPaths;
|
||||||
|
$this->cachePath = $cachePath;
|
||||||
|
$this->attributes = $attributes;
|
||||||
|
$this->renderer = new \Philo\Blade\Blade($viewPaths, $cachePath, $events);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a template
|
||||||
|
*
|
||||||
|
* $data cannot contain template as a key
|
||||||
|
*
|
||||||
|
* throws RuntimeException if $templatePath . $template does not exist
|
||||||
|
*
|
||||||
|
* @param ResponseInterface $response
|
||||||
|
* @param string $template
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return ResponseInterface
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function render(ResponseInterface $response, $template, array $data = [])
|
||||||
|
{
|
||||||
|
$output = $this->fetch($template, $data);
|
||||||
|
$response->getBody()->write($output);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the attributes for the renderer
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getAttributes()
|
||||||
|
{
|
||||||
|
return $this->attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the attributes for the renderer
|
||||||
|
*
|
||||||
|
* @param array $attributes
|
||||||
|
*/
|
||||||
|
public function setAttributes(array $attributes)
|
||||||
|
{
|
||||||
|
$this->attributes = $attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the attribute for the renderer
|
||||||
|
*
|
||||||
|
* @param $key
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function set($key, $value)
|
||||||
|
{
|
||||||
|
$this->attributes[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve an attribute
|
||||||
|
*
|
||||||
|
* @param $key
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function get($key)
|
||||||
|
{
|
||||||
|
if (!isset($this->attributes[$key])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->attributes[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getViewPaths()
|
||||||
|
{
|
||||||
|
return $this->viewPaths;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $viewPaths
|
||||||
|
*/
|
||||||
|
public function setViewPaths($viewPaths)
|
||||||
|
{
|
||||||
|
$this->viewPaths = $viewPaths;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCachePath()
|
||||||
|
{
|
||||||
|
return $this->cachePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $cachePath
|
||||||
|
*/
|
||||||
|
public function setCachePath($cachePath)
|
||||||
|
{
|
||||||
|
$this->cachePath = $cachePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a template and returns the result as a string
|
||||||
|
*
|
||||||
|
* cannot contain template as a key
|
||||||
|
*
|
||||||
|
* throws RuntimeException if $templatePath . $template does not exist
|
||||||
|
*
|
||||||
|
* @param $template
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* @throws \RuntimeException
|
||||||
|
*/
|
||||||
|
public function fetch($template, array $data = [])
|
||||||
|
{
|
||||||
|
if (isset($data['template'])) {
|
||||||
|
throw new \InvalidArgumentException("Duplicate template key found");
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = array_merge($this->attributes, $data);
|
||||||
|
|
||||||
|
return $this->renderer->view()->make($template, $data)->render();
|
||||||
|
}
|
||||||
|
}
|
77
tests/BladeTest.php
Normal file
77
tests/BladeTest.php
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Slim\Tests\Views;
|
||||||
|
|
||||||
|
use Slim\Views\Blade;
|
||||||
|
|
||||||
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
|
class BladeTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Blade
|
||||||
|
*/
|
||||||
|
protected $view;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->view = new Blade([__DIR__ . '/templates', __DIR__ . '/another'], __DIR__ . '/../data/cache');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFetch()
|
||||||
|
{
|
||||||
|
$output = $this->view->fetch('example', [
|
||||||
|
'name' => 'Josh',
|
||||||
|
]);
|
||||||
|
$this->assertEquals("<p>Hi, my name is Josh.</p>\n", $output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSingleTemplateWithANamespace()
|
||||||
|
{
|
||||||
|
$views = new Blade([
|
||||||
|
'One' => __DIR__ . '/templates',
|
||||||
|
]);
|
||||||
|
$output = $views->fetch('example', [
|
||||||
|
'name' => 'Josh',
|
||||||
|
]);
|
||||||
|
$this->assertEquals("<p>Hi, my name is Josh.</p>\n", $output);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMultipleTemplatesWithMulNamespace()
|
||||||
|
{
|
||||||
|
$views = new Blade([
|
||||||
|
'One' => __DIR__ . '/templates',
|
||||||
|
'Two' => __DIR__ . '/another',
|
||||||
|
]);
|
||||||
|
$outputOne = $views->fetch('example', [
|
||||||
|
'name' => 'Peter',
|
||||||
|
]);
|
||||||
|
$outputTwo = $views->fetch('example2', [
|
||||||
|
'name' => 'Peter',
|
||||||
|
'gender' => 'male',
|
||||||
|
]);
|
||||||
|
$this->assertEquals("<p>Hi, my name is Peter.</p>\n", $outputOne);
|
||||||
|
$this->assertEquals("<p>Hi, my name is Peter and I am male.</p>\n", $outputTwo);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRender()
|
||||||
|
{
|
||||||
|
$mockBody = $this->getMockBuilder('Psr\Http\Message\StreamInterface')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$mockResponse = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
$mockBody->expects($this->once())
|
||||||
|
->method('write')
|
||||||
|
->with("<p>Hi, my name is Josh.</p>\n")
|
||||||
|
->willReturn(28);
|
||||||
|
$mockResponse->expects($this->once())
|
||||||
|
->method('getBody')
|
||||||
|
->willReturn($mockBody);
|
||||||
|
$response = $this->view->render($mockResponse, 'example', [
|
||||||
|
'name' => 'Josh',
|
||||||
|
]);
|
||||||
|
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
|
||||||
|
}
|
||||||
|
}
|
1
tests/another/example2.blade.php
Normal file
1
tests/another/example2.blade.php
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>Hi, my name is {{ $name }} and I am {{ $gender }}.</p>
|
1
tests/templates/example.blade.php
Normal file
1
tests/templates/example.blade.php
Normal file
@ -0,0 +1 @@
|
|||||||
|
<p>Hi, my name is {{ $name }}.</p>
|
Reference in New Issue
Block a user