View Extender (add namespace) (#2134)

This commit is contained in:
Alexander Skvortsov 2020-07-17 05:59:00 -04:00 committed by GitHub
parent 3117d2ad7a
commit b5e891df30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,51 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory;
class ViewNamespace implements ExtenderInterface
{
private $adds = [];
/**
* Register a new namespace of Laravel views.
*
* Views are php files that use the Laravel Blade syntax for creation of server-side generated html.
* Flarum core uses them for error pages, the installer, HTML emails, and the skeletons for the forum and admin sites.
* To create and use views in your extension, you will need to put them in a folder, and register that folder as a namespace.
*
* Views can then be used in your extension by injecting an instance of `Illuminate\Contracts\View\Factory`,
* and calling its `make` method. The `make` method takes the view parameter in the format NAMESPACE::VIEW_NAME.
* You can also pass variables into a view: for more information, see https://laravel.com/api/6.x/Illuminate/View/Factory.html#method_make
*
* @param string $namespace: The name of the namespace.
* @param string|array $hints: This is a path (or an array of paths) to the folder(s)
* where view files are stored, relative to the extend.php file.
* @return $this
*/
public function add($namespace, $hints)
{
$this->adds[$namespace] = $hints;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$factory = $container->make(Factory::class);
foreach ($this->adds as $namespace => $hints) {
$factory->addNamespace($namespace, $hints);
}
}
}

1
tests/fixtures/views/test.blade.php vendored Normal file
View File

@ -0,0 +1 @@
<html><body>Hello World!</body></html>

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\extenders;
use Flarum\Extend;
use Flarum\Tests\integration\TestCase;
use Illuminate\Contracts\View\Factory;
class ViewNamespaceTest extends TestCase
{
/**
* @test
*/
public function custom_view_namespace_does_not_exist_by_default()
{
$this->expectException(\InvalidArgumentException::class);
$this->app()->getContainer()->make(Factory::class)->make('integration.test::test');
}
/**
* @test
*/
public function custom_view_namespace_can_be_added_by_extender()
{
$this->extend(
(new Extend\ViewNamespace)
->add('integration.test', dirname(__FILE__, 3).'/fixtures/views')
);
$this->assertEquals('<html><body>Hello World!</body></html>', trim($this->app()->getContainer()->make(Factory::class)->make('integration.test::test')->render()));
}
}