Skip to content

Commit

Permalink
refactor: Remove \Leevel\Router\View and optimize view code
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Dec 4, 2020
1 parent 853613b commit cd73dc4
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 511 deletions.
15 changes: 1 addition & 14 deletions src/Leevel/Router/Provider/Register.php
Expand Up @@ -46,7 +46,6 @@ public function register(): void
$this->url();
$this->redirect();
$this->response();
$this->view();
}

/**
Expand Down Expand Up @@ -133,25 +132,13 @@ protected function response(): void
function (IContainer $container): Response {
$option = $container['option'];

return (new Response($container['view'], $container['redirect']))
return (new Response($container['views'], $container['redirect']))
->setViewSuccessTemplate($option->get('view\\success'))
->setViewFailTemplate($option->get('view\\fail'));
},
);
}

/**
* 注册 view 服务
*/
protected function view(): void
{
$this->container
->singleton(
'view',
fn (IContainer $container): View => new View($container['view.view']),
);
}

/**
* 设置 COOKIE 助手配置.
*/
Expand Down
18 changes: 5 additions & 13 deletions src/Leevel/Router/Response.php
Expand Up @@ -27,22 +27,13 @@
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use SplFileInfo;
use SplFileObject;
use Leevel\View\Manager;

/**
* 响应.
*/
class Response
{
/**
* 视图.
*/
protected IView $view;

/**
* 跳转实例.
*/
protected Redirect $redirect;

/**
* 视图正确模板.
*/
Expand All @@ -56,10 +47,11 @@ class Response
/**
* 构造函数.
*/
public function __construct(IView $view, Redirect $redirect)
public function __construct(
protected Manager $view,
protected Redirect $redirect,
)
{
$this->view = $view;
$this->redirect = $redirect;
}

/**
Expand Down
86 changes: 0 additions & 86 deletions src/Leevel/Router/View.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Leevel/Router/composer.json
Expand Up @@ -18,6 +18,7 @@
"leevel/pipeline": "1.1.*",
"leevel/di": "1.1.*",
"leevel/filesystem": "1.1.*",
"leevel/view": "1.1.*",
"symfony/http-foundation": "~5.1",
"symfony/finder": "~5.1"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Leevel/View/Console/Cache.php
Expand Up @@ -185,7 +185,7 @@ protected function getHtmlView(): Html
{
return $this->app
->container()
->make('view.views')
->make('views')
->connect('html');
}
}
4 changes: 3 additions & 1 deletion src/Leevel/View/Manager.php
Expand Up @@ -62,7 +62,9 @@ protected function makeConnectHtml(string $connect): Html
{
$html = new Html($this->normalizeConnectOption($connect));
$html->setParseResolver(function (): Parser {
return $this->container['view.parser'];
return (new Parser( new Compiler()))
->registerCompilers()
->registerParsers();
});

return $html;
Expand Down
56 changes: 11 additions & 45 deletions src/Leevel/View/Provider/Register.php
Expand Up @@ -22,10 +22,8 @@

use Leevel\Di\IContainer;
use Leevel\Di\Provider;
use Leevel\View\Compiler;
use Leevel\View\IView;
use Leevel\View\Manager;
use Leevel\View\Parser;
use Leevel\View\View;

/**
Expand All @@ -38,10 +36,8 @@ class Register extends Provider
*/
public function register(): void
{
$this->viewViews();
$this->viewView();
$this->viewCompiler();
$this->viewParser();
$this->views();
$this->view();
}

/**
Expand All @@ -50,10 +46,8 @@ public function register(): void
public static function providers(): array
{
return [
'view.views' => Manager::class,
'view.view' => [IView::class, View::class],
'view.compiler' => Compiler::class,
'view.parser' => Parser::class,
'views' => Manager::class,
'view' => [IView::class, View::class],
];
}

Expand All @@ -66,54 +60,26 @@ public static function isDeferred(): bool
}

/**
* 注册 view.views 服务.
* 注册 views 服务.
*/
protected function viewViews(): void
protected function views(): void
{
$this->container
->singleton(
'view.views',
'views',
fn (IContainer $container): Manager => new Manager($container),
);
}

/**
* 注册 view.view 服务.
* 注册 view 服务.
*/
protected function viewView(): void
protected function view(): void
{
$this->container
->singleton(
'view.view',
fn (IContainer $container): IView => $container['view.views']->connect(),
);
}

/**
* 注册 view.compiler 服务.
*/
protected function viewCompiler(): void
{
$this->container
->singleton(
'view.compiler',
fn (): Compiler => new Compiler(),
);
}

/**
* 注册 view.parser 服务.
*/
protected function viewParser(): void
{
$this->container
->singleton(
'view.parser',
function (IContainer $container): Parser {
return (new Parser($container['view.compiler']))
->registerCompilers()
->registerParsers();
},
'view',
fn (IContainer $container): IView => $container['views']->connect(),
);
}
}
Expand Up @@ -18,20 +18,19 @@
* file that was distributed with this source code.
*/

namespace Leevel\Router\Proxy;
namespace Leevel\View\Proxy;

use Leevel\Di\Container;
use Leevel\Router\View as BaseView;
use Leevel\View\Manager;

/**
* 代理 view.
*
* @method static void switchView(\Leevel\View\IView $view) 切换视图.
* @method static void setVar($name, $value = null) 变量赋值.
* @method static mixed getVar(?string $name = null) 获取变量赋值.
* @method static void deleteVar(array $name) 删除变量值.
* @method static void clearVar() 清空变量值.
* @method static string display(string $file, array $vars = [], ?string $ext = null) 加载视图文件.
* @method static string display(string $file, array $vars = [], ?string $ext = null) 加载视图文件.
* @method static void setVar(array|string $name, mixed $value = null) 设置模板变量.
* @method static mixed getVar(?string $name = null) 获取变量值.
* @method static void deleteVar(array $name) 删除变量值.
* @method static void clearVar() 清空变量值.
*/
class View
{
Expand All @@ -46,8 +45,8 @@ public static function __callStatic(string $method, array $args): mixed
/**
* 代理服务.
*/
public static function proxy(): BaseView
public static function proxy(): Manager
{
return Container::singletons()->make('view');
return Container::singletons()->make('views');
}
}
17 changes: 4 additions & 13 deletions tests/Router/Provider/RegisterTest.php
Expand Up @@ -25,15 +25,13 @@
use Leevel\Option\Option;
use Leevel\Router\IRouter;
use Leevel\Router\IUrl;
use Leevel\Router\IView;
use Leevel\Router\Provider\Register;
use Leevel\Router\Redirect;
use Leevel\Router\Response;
use Leevel\Router\Router;
use Leevel\Router\Url;
use Leevel\Router\View;
use Leevel\Session\ISession;
use Leevel\View\IView as IViews;
use Leevel\View\Manager;
use Tests\TestCase;

class RegisterTest extends TestCase
Expand All @@ -53,8 +51,7 @@ public function testBaseUse(): void
$this->assertInstanceof(Redirect::class, $container->make('redirect'));
$this->assertInstanceof(Response::class, $container->make('response'));
$this->assertInstanceof(Response::class, $container->make('response'));
$this->assertInstanceof(IView::class, $container->make('view'));
$this->assertInstanceof(View::class, $container->make('view'));
$this->assertInstanceof(Manager::class, $container->make('views'));

$this->assertSame('http://www.queryphp.cn/foo/bar?hello=world', $url->make('foo/bar', ['hello' => 'world']));
}
Expand Down Expand Up @@ -82,25 +79,19 @@ protected function createContainer(): Container
'fail' => 'fail',
],
]);

$container->singleton('option', $option);

$request = $this->createMock(Request::class);

$request->method('getEnter')->willReturn('');
$this->assertSame('', $request->getEnter());

$request->method('isSecure')->willReturn(false);
$this->assertFalse($request->isSecure());

$container->singleton('request', $request);

$view = $this->createMock(IViews::class);

$container->singleton('view.view', $view);
$view = $this->createMock(Manager::class);
$container->singleton('views', $view);

$session = $this->createMock(ISession::class);

$container->singleton('session', $session);

return $container;
Expand Down

0 comments on commit cd73dc4

Please sign in to comment.