Skip to content

Commit

Permalink
perf: 支持从配置文件设置视图的命名空间和注册视图组件
Browse files Browse the repository at this point in the history
  • Loading branch information
nfangxu committed Aug 25, 2020
1 parent d44b5dd commit b0e7759
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Factory/CompilerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ class CompilerFactory
{
public function __invoke(Container $container)
{
return new BladeCompiler(
$blade = new BladeCompiler(
$container->get(Filesystem::class),
$container->get(ConfigInterface::class)->get('view.config.cache_path')
);

// register view components
foreach ((array)$container->get(ConfigInterface::class)->get('view.components') as $alias => $class) {
$blade->component($class, $alias);
}

return $blade;
}
}
15 changes: 14 additions & 1 deletion src/Factory/FinderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ class FinderFactory
{
public function __invoke(Container $container)
{
return new Finder(
$finder = new Finder(
$container->get(Filesystem::class),
(array)$container->get(ConfigInterface::class)->get('view.config.view_path')
);

// register view namespace
foreach ((array)$container->get(ConfigInterface::class)->get('view.namespaces') as $namespace => $hints) {
foreach ($finder->getPaths() as $viewPath) {
if (is_dir($appPath = $viewPath . '/vendor/' . $namespace)) {
$finder->addNamespace($namespace, $appPath);
}
}

$finder->addNamespace($namespace, $hints);
}

return $finder;
}
}
79 changes: 79 additions & 0 deletions tests/ConfigRegisterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* Fangx's Packages
*
* @link https://nfangxu.com
* @document https://pkg.nfangxu.com
* @contact [email protected]
* @author nfangxu
* @license https://pkg.nfangxu.com/license
*/

namespace Fangx\Tests;

use Fangx\Tests\Stub\Alert;
use Fangx\Tests\Stub\AlertSlot;
use Fangx\View\Contract\FactoryInterface;
use Fangx\View\HyperfViewEngine;
use Hyperf\Config\Config;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Di\Container;
use Hyperf\Utils\ApplicationContext;
use Hyperf\View\Mode;
use PHPUnit\Framework\TestCase;

class ConfigRegisterTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

/** @var Container $container */
$container = ApplicationContext::getContainer();

$container->set(ConfigInterface::class, new Config([
'view' => [
'engine' => HyperfViewEngine::class,
'mode' => Mode::SYNC,
'config' => [
'view_path' => __DIR__ . '/storage/view/',
'cache_path' => __DIR__ . '/storage/cache/',
],
'components' => [
'alert' => Alert::class,
'alert-slot' => AlertSlot::class,
],
'namespaces' => [
'admin' => __DIR__ . '/admin'
],
],
]));
}

public function testRegisterComponents()
{
$this->assertSame('success', $this->view('simple_8', ['message' => 'success']));
$this->assertSame('success', $this->view('simple_9', ['message' => 'success']));
}

public function testRegisterNamespace()
{
$this->assertSame('from_admin', $this->view('admin::simple_3'));
$this->assertSame('from_vendor', $this->view('admin::simple_4'));
}

protected function view(string $view, $data = [], array $mergeData = []): string
{
$container = ApplicationContext::getContainer();

/** @var FactoryInterface $factory */
$factory = $container->get(FactoryInterface::class);

$content = $factory->make($view, $data, $mergeData)->render();

return trim($content);
}
}

0 comments on commit b0e7759

Please sign in to comment.