mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 03:37:44 +08:00
Merge branch 'master' into 1.1-exception
This commit is contained in:
commit
f84c02984c
@ -3,7 +3,8 @@
|
|||||||
## Added
|
## Added
|
||||||
|
|
||||||
- [#827](https://github.com/hyperf/hyperf/pull/827) Added a simple db component.
|
- [#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
|
## Fixed
|
||||||
|
|
||||||
|
@ -46,18 +46,20 @@
|
|||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"doctrine/common": "@stable",
|
"doctrine/common": "@stable",
|
||||||
|
"domnikl/statsd": "^3.0.1",
|
||||||
"friendsofphp/php-cs-fixer": "^2.14",
|
"friendsofphp/php-cs-fixer": "^2.14",
|
||||||
|
"influxdb/influxdb-php": "^1.15.0",
|
||||||
"jonahgeorge/jaeger-client-php": "^0.4.4",
|
"jonahgeorge/jaeger-client-php": "^0.4.4",
|
||||||
"malukenho/docheader": "^0.1.6",
|
"malukenho/docheader": "^0.1.6",
|
||||||
"mockery/mockery": "^1.0",
|
"mockery/mockery": "^1.0",
|
||||||
"php-di/php-di": "^6.0",
|
"php-di/php-di": "^6.0",
|
||||||
"phpstan/phpstan": "^0.11.15",
|
"phpstan/phpstan": "^0.11.15",
|
||||||
"phpunit/phpunit": "^7.0.0",
|
"phpunit/phpunit": "^7.0.0",
|
||||||
|
"smarty/smarty": "^3.1",
|
||||||
"swoft/swoole-ide-helper": "dev-master",
|
"swoft/swoole-ide-helper": "dev-master",
|
||||||
"symfony/property-access": "^4.3",
|
"symfony/property-access": "^4.3",
|
||||||
"symfony/serializer": "^4.3",
|
"symfony/serializer": "^4.3",
|
||||||
"influxdb/influxdb-php": "^1.15.0",
|
"twig/twig": "^2.12"
|
||||||
"domnikl/statsd": "^3.0.1"
|
|
||||||
},
|
},
|
||||||
"replace": {
|
"replace": {
|
||||||
"hyperf/amqp": "self.version",
|
"hyperf/amqp": "self.version",
|
||||||
@ -237,6 +239,7 @@
|
|||||||
"HyperfTest\\Translation\\": "src/translation/tests/",
|
"HyperfTest\\Translation\\": "src/translation/tests/",
|
||||||
"HyperfTest\\Utils\\": "src/utils/tests/",
|
"HyperfTest\\Utils\\": "src/utils/tests/",
|
||||||
"HyperfTest\\Validation\\": "src/validation/tests/",
|
"HyperfTest\\Validation\\": "src/validation/tests/",
|
||||||
|
"HyperfTest\\View\\": "src/view/tests/",
|
||||||
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/"
|
"HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -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 引擎
|
### 安装 Blade 引擎
|
||||||
|
|
||||||
@ -78,6 +78,12 @@ composer require duncan3dc/blade
|
|||||||
composer require smarty/smarty
|
composer require smarty/smarty
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 安装 Twig 引擎
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer require twig/twig
|
||||||
|
```
|
||||||
|
|
||||||
### 接入其他模板
|
### 接入其他模板
|
||||||
|
|
||||||
假设我们想要接入一个虚拟的模板引擎名为 `TemplateEngine`,那么我们需要在任意地方创建对应的 `TemplateEngine` 类,并实现 `Hyperf\View\Engine\EngineInterface` 接口。
|
假设我们想要接入一个虚拟的模板引擎名为 `TemplateEngine`,那么我们需要在任意地方创建对应的 `TemplateEngine` 类,并实现 `Hyperf\View\Engine\EngineInterface` 接口。
|
||||||
|
@ -26,6 +26,7 @@
|
|||||||
"suggest": {
|
"suggest": {
|
||||||
"hyperf/task": "Required to use task as a view render mode.",
|
"hyperf/task": "Required to use task as a view render mode.",
|
||||||
"smarty/smarty": "Required to use smarty as a view render engine.",
|
"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."
|
"duncan3dc/blade": "Required to use blade as a view render engine."
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@ -35,6 +36,7 @@
|
|||||||
},
|
},
|
||||||
"autoload-dev": {
|
"autoload-dev": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
"HyperfTest\\View\\": "tests/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
|
27
src/view/src/Engine/TwigEngine.php
Normal file
27
src/view/src/Engine/TwigEngine.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
45
src/view/tests/SmartyTest.php
Normal file
45
src/view/tests/SmartyTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
45
src/view/tests/TwigTest.php
Normal file
45
src/view/tests/TwigTest.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
10
src/view/tests/tpl/index.tpl
Normal file
10
src/view/tests/tpl/index.tpl
Normal 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>
|
10
src/view/tests/tpl/index.twig
Normal file
10
src/view/tests/tpl/index.twig
Normal 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>
|
Loading…
Reference in New Issue
Block a user