Merge branch 'master' into 1.1-exception

This commit is contained in:
李铭昕 2019-11-11 09:46:17 +08:00
commit f84c02984c
9 changed files with 153 additions and 4 deletions

View File

@ -3,7 +3,8 @@
## Added
- [#827](https://github.com/hyperf/hyperf/pull/827) Added a simple db component.
- [#913](https://github.com/hyperf/hyperf/pull/913) Added `Hyperf\ExceptionHandler\Listener\HandleErrorListener`.
- [#905](https://github.com/hyperf/hyperf/pull/905) Added twig template engine for view.
- [#913](https://github.com/hyperf/hyperf/pull/913) Added `Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler`.
## Fixed

View File

@ -46,18 +46,20 @@
},
"require-dev": {
"doctrine/common": "@stable",
"domnikl/statsd": "^3.0.1",
"friendsofphp/php-cs-fixer": "^2.14",
"influxdb/influxdb-php": "^1.15.0",
"jonahgeorge/jaeger-client-php": "^0.4.4",
"malukenho/docheader": "^0.1.6",
"mockery/mockery": "^1.0",
"php-di/php-di": "^6.0",
"phpstan/phpstan": "^0.11.15",
"phpunit/phpunit": "^7.0.0",
"smarty/smarty": "^3.1",
"swoft/swoole-ide-helper": "dev-master",
"symfony/property-access": "^4.3",
"symfony/serializer": "^4.3",
"influxdb/influxdb-php": "^1.15.0",
"domnikl/statsd": "^3.0.1"
"twig/twig": "^2.12"
},
"replace": {
"hyperf/amqp": "self.version",
@ -237,6 +239,7 @@
"HyperfTest\\Translation\\": "src/translation/tests/",
"HyperfTest\\Utils\\": "src/utils/tests/",
"HyperfTest\\Validation\\": "src/validation/tests/",
"HyperfTest\\View\\": "src/view/tests/",
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/"
}
},

View File

@ -64,7 +64,7 @@ return [
## 视图渲染引擎
官方目前支持 `Blade` `Smarty`种模板,默认安装 [hyperf/view](https://github.com/hyperf/view) 时不会自动安装任何模板引擎,需要您根据自身需求,自行安装对应的模板引擎,使用前必须安装任一模板引擎。
官方目前支持 `Blade` `Smarty` 和 `Twig`种模板,默认安装 [hyperf/view](https://github.com/hyperf/view) 时不会自动安装任何模板引擎,需要您根据自身需求,自行安装对应的模板引擎,使用前必须安装任一模板引擎。
### 安装 Blade 引擎
@ -78,6 +78,12 @@ composer require duncan3dc/blade
composer require smarty/smarty
```
### 安装 Twig 引擎
```bash
composer require twig/twig
```
### 接入其他模板
假设我们想要接入一个虚拟的模板引擎名为 `TemplateEngine`,那么我们需要在任意地方创建对应的 `TemplateEngine` 类,并实现 `Hyperf\View\Engine\EngineInterface` 接口。

View File

@ -26,6 +26,7 @@
"suggest": {
"hyperf/task": "Required to use task as a view render mode.",
"smarty/smarty": "Required to use smarty as a view render engine.",
"twig/twig": "Required to use twig as a view render engine.",
"duncan3dc/blade": "Required to use blade as a view render engine."
},
"autoload": {
@ -35,6 +36,7 @@
},
"autoload-dev": {
"psr-4": {
"HyperfTest\\View\\": "tests/"
}
},
"config": {

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\View\Engine;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class TwigEngine implements EngineInterface
{
public function render($template, $data, $config): string
{
$loader = new FilesystemLoader($config['view_path']);
$twig = new Environment($loader, ['cache' => $config['cache_path']]);
return $twig->render($template, $data);
}
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\View;
use Hyperf\View\Engine\SmartyEngine;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class SmartyTest extends TestCase
{
public function testRender()
{
$config = [
'view_path' => __DIR__ . '/tpl',
'cache_path' => __DIR__ . '/runtime',
];
$engine = new SmartyEngine();
$res = $engine->render('index.tpl', ['name' => 'Hyperf'], $config);
$this->assertEquals('<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
Hello, Hyperf. You are using smarty template now.
</body>
</html>', $res);
}
}

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\View;
use Hyperf\View\Engine\TwigEngine;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class TwigTest extends TestCase
{
public function testRender()
{
$config = [
'view_path' => __DIR__ . '/tpl',
'cache_path' => __DIR__ . '/runtime',
];
$engine = new TwigEngine();
$res = $engine->render('index.twig', ['name' => 'Hyperf'], $config);
$this->assertEquals('<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
Hello, Hyperf. You are using smarty template now.
</body>
</html>', $res);
}
}

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
Hello, {$name}. You are using smarty template now.
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
Hello, {{ name }}. You are using smarty template now.
</body>
</html>