commit 0b49055fce883a3937638b33d96cc693b4e3f083 Author: Hiroaki Matsuura Date: Thu Mar 10 19:02:45 2016 +0900 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6e1e02 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.lock +vendor +data/cache/*.php \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fbb8ca4 --- /dev/null +++ b/composer.json @@ -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" + } +} \ No newline at end of file diff --git a/data/cache/.gitkeep b/data/cache/.gitkeep new file mode 100644 index 0000000..212c4ba --- /dev/null +++ b/data/cache/.gitkeep @@ -0,0 +1 @@ +#!/usr/bin/env bash \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..388c093 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,24 @@ + + + + + + tests/ + + + + + + src/ + + + \ No newline at end of file diff --git a/src/Blade.php b/src/Blade.php new file mode 100644 index 0000000..3fe28d0 --- /dev/null +++ b/src/Blade.php @@ -0,0 +1,169 @@ +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(); + } +} \ No newline at end of file diff --git a/tests/BladeTest.php b/tests/BladeTest.php new file mode 100644 index 0000000..a6bca4d --- /dev/null +++ b/tests/BladeTest.php @@ -0,0 +1,77 @@ +view = new Blade([__DIR__ . '/templates', __DIR__ . '/another'], __DIR__ . '/../data/cache'); + } + + public function testFetch() + { + $output = $this->view->fetch('example', [ + 'name' => 'Josh', + ]); + $this->assertEquals("

Hi, my name is Josh.

\n", $output); + } + + public function testSingleTemplateWithANamespace() + { + $views = new Blade([ + 'One' => __DIR__ . '/templates', + ]); + $output = $views->fetch('example', [ + 'name' => 'Josh', + ]); + $this->assertEquals("

Hi, my name is Josh.

\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("

Hi, my name is Peter.

\n", $outputOne); + $this->assertEquals("

Hi, my name is Peter and I am male.

\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("

Hi, my name is Josh.

\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); + } +} \ No newline at end of file diff --git a/tests/another/example2.blade.php b/tests/another/example2.blade.php new file mode 100644 index 0000000..ee69620 --- /dev/null +++ b/tests/another/example2.blade.php @@ -0,0 +1 @@ +

Hi, my name is {{ $name }} and I am {{ $gender }}.

diff --git a/tests/templates/example.blade.php b/tests/templates/example.blade.php new file mode 100644 index 0000000..8114d2f --- /dev/null +++ b/tests/templates/example.blade.php @@ -0,0 +1 @@ +

Hi, my name is {{ $name }}.