Skip to content

Commit

Permalink
基于Tp模板规则重新实现路径解析
Browse files Browse the repository at this point in the history
  • Loading branch information
NHZEX committed Nov 22, 2019
1 parent 4444f72 commit 06aa230
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
4 changes: 0 additions & 4 deletions driver/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ public function parseTemplate(string $template)
$appName ? $appName . DIRECTORY_SEPARATOR : ''
);
}

// 追加模板优先索引路径
$this->blade->prependLocation($path);
$this->blade->locationUnique();
}

$depr = $this->config['view_depr'];
Expand Down
29 changes: 2 additions & 27 deletions src/BladeInstance.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private function getResolver(): EngineResolver
private function getViewFinder(): ViewFinderInterface
{
if (!$this->finder) {
$this->finder = new FileViewFinder(new Filesystem(), [$this->path]);
// $this->finder = new FileViewFinder(new Filesystem(), [$this->path]);
$this->finder = new TpViewFinder();
}

return $this->finder;
Expand Down Expand Up @@ -219,32 +220,6 @@ public function addLocation(string $location): BladeInterface
return $this;
}

/**
* 追加一个视图索引路径并设置为首选
*
* @param string $location
* @return $this
*/
public function prependLocation(string $location): BladeInterface
{
$this->getViewFinder()->prependLocation($location);

return $this;
}

/**
* 移除重复的视图索引路径
*
* @return $this
*/
public function locationUnique(): BladeInterface
{
$finder = $this->getViewFinder();
$finder->setPaths(array_unique($finder->getPaths()));

return $this;
}

/**
* Check if a view exists.
*
Expand Down
95 changes: 95 additions & 0 deletions src/TpViewFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);

namespace HZEX\Blade;

use Illuminate\View\ViewFinderInterface;
use think\App;
use think\View;
use think\view\driver\Blade;

class TpViewFinder implements ViewFinderInterface
{

/**
* Get the fully qualified location of the view.
*
* @param string $view
* @return string
*/
public function find($view)
{
/** @var View|Blade $view */
$driver = App::getInstance()->make(View::class);
$view = str_replace('.', DIRECTORY_SEPARATOR, $view);
return $driver->parseTemplate($view);
}

/**
* Add a location to the finder.
*
* @param string $location
* @return void
*/
public function addLocation($location)
{
// TODO: Implement addLocation() method.
}

/**
* Add a namespace hint to the finder.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function addNamespace($namespace, $hints)
{
// TODO: Implement addNamespace() method.
}

/**
* Prepend a namespace hint to the finder.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function prependNamespace($namespace, $hints)
{
// TODO: Implement prependNamespace() method.
}

/**
* Replace the namespace hints for the given namespace.
*
* @param string $namespace
* @param string|array $hints
* @return void
*/
public function replaceNamespace($namespace, $hints)
{
// TODO: Implement replaceNamespace() method.
}

/**
* Add a valid view extension to the finder.
*
* @param string $extension
* @return void
*/
public function addExtension($extension)
{
// TODO: Implement addExtension() method.
}

/**
* Flush the cache of located views.
*
* @return void
*/
public function flush()
{
// TODO: Implement flush() method.
}
}
5 changes: 2 additions & 3 deletions tests/ViewBladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public function testParametersFetch()

public function testAltPath()
{
$this->engine->addLocation(__DIR__ . "/views/alt");
$result = $this->engine->render("view3");
$result = $this->engine->render("alt/view3");
$this->assertSame(file_get_contents(__DIR__ . "/views/alt/view3.blade.php"), $result);
}

Expand Down Expand Up @@ -167,7 +166,7 @@ public function testInheritance()
*/
public function testInheritanceAltPath()
{
$this->engine->addLocation(__DIR__ . "/views/alt");
// $this->engine->setViewDirName("views/alt");
$result = $this->blade->fetch("view11");
$this->assertSame(file_get_contents(__DIR__ . "/views/view11.html"), $result);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/views/view11.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@extends("base-alt")
@extends("alt.base-alt")

@section("title", "Inheritance!")

0 comments on commit 06aa230

Please sign in to comment.