Added NoneEngine as the default engine of view config (#2958)

* Changed default engine of view config

* Update CHANGELOG-2.0.md

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
沈唁 2020-12-11 16:21:12 +08:00 committed by GitHub
parent e04811fea8
commit 28fa61c687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 100 additions and 8 deletions

View File

@ -11,6 +11,7 @@
## Changed
- [#2934](https://github.com/hyperf/hyperf/pull/2934) Changed config file `scout.php` which search engine index is used as the model index name by default.
- [#2958](https://github.com/hyperf/hyperf/pull/2958) Added NoneEngine as the default engine of view config.
## Optimized

View File

@ -2,13 +2,13 @@
> 基于 laravel blade 模板引擎改写, 支持原始 blade 模板引擎的语法.
```
```bash
composer require hyperf/view-engine
```
## 生成配置
```
```bash
php bin/hyperf.php vendor:publish hyperf/view-engine
```

View File

@ -10,7 +10,13 @@ composer require hyperf/view
## 配置
View 组件的配置文件位于 `config/autoload/view.php`,若配置文件不存在可自行创建,以下为相关配置的说明:
View 组件的配置文件位于 `config/autoload/view.php`,若配置文件不存在可执行如下命令生成配置文件
```bash
php bin/hyperf.php vendor:publish hyperf/view
```
以下为相关配置的说明:
| 配置 | 类型 | 默认值 | 备注 |
|:-----------------:|:------:|:-------------------------------------:|:----------------:|

View File

@ -9,15 +9,14 @@ declare(strict_types=1);
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\View\Engine\NoneEngine;
use Hyperf\View\Mode;
use Hyperf\ViewEngine\HyperfViewEngine;
return [
'engine' => HyperfViewEngine::class,
'engine' => NoneEngine::class,
'mode' => Mode::SYNC,
'config' => [
'view_path' => BASE_PATH . '/storage/view/',
'cache_path' => BASE_PATH . '/runtime/view/',
'charset' => 'UTF-8',
],
];

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\View\Engine;
class NoneEngine implements EngineInterface
{
public function render($template, $data, $config): string
{
return <<<'HTML'
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hyperf</title>
<!-- Fonts -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Hyperf</h1>
<p>Hyperf is an extremely performant and flexible PHP CLI framework based on Swoole 4.5+, powered by the
state-of-the-art coroutine server and a large number of battle-tested components. Aside from the decisive
benchmark outmatching against PHP-FPM frameworks, Hyperf also distinct itself by its focus on flexibility
and composability. Hyperf ships with an AOP-enabling dependency injector to ensure components and classes
are pluggable and meta programmable. All of its core components strictly follow the PSR standards and thus
can be used in other frameworks.</p>
<p><a class="btn btn-primary btn-lg" href="https://hyperf.wiki/" role="button">Learn more</a></p>
<p>This view engine is not available, please use engines below.</p>
<ul class="list-group">
<li class="list-group-item"><a href="https://github.com/hyperf/view-engine">hyperf/view-engine</a></li>
<li class="list-group-item"><a href="https://github.com/duncan3dc/blade">duncan3dc/blade</a></li>
<li class="list-group-item"><a href="https://github.com/smarty-php/smarty">smarty/smarty</a></li>
<li class="list-group-item"><a href="https://github.com/twigphp/Twig">twig/twig</a></li>
<li class="list-group-item"><a href="https://github.com/thephpleague/plates">league/plates</a></li>
<li class="list-group-item"><a href="https://github.com/sy-records/think-template">sy-records/think-template</a></li>
</ul>
</div>
</div>
</body>
</html>
HTML;
}
}

View File

@ -17,7 +17,7 @@ use Hyperf\Task\Task;
use Hyperf\Task\TaskExecutor;
use Hyperf\Utils\Context;
use Hyperf\View\Engine\EngineInterface;
use Hyperf\View\Engine\SmartyEngine;
use Hyperf\View\Engine\NoneEngine;
use Hyperf\View\Exception\EngineNotFindException;
use Hyperf\View\Exception\RenderException;
use Psr\Container\ContainerInterface;
@ -47,7 +47,7 @@ class Render implements RenderInterface
public function __construct(ContainerInterface $container, ConfigInterface $config)
{
$engine = $config->get('view.engine', SmartyEngine::class);
$engine = $config->get('view.engine', NoneEngine::class);
if (! $container->has($engine)) {
throw new EngineNotFindException("{$engine} engine is not found.");
}

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\View;
use Hyperf\View\Engine\NoneEngine;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class NoneTest extends TestCase
{
public function testRender()
{
$content = (new NoneEngine())->render('/', [], []);
$this->assertNotEmpty($content);
}
}