Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View Extender (add namespace) #2134

Merged
merged 9 commits into from
Jul 17, 2020
43 changes: 43 additions & 0 deletions src/Extend/View.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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 View implements ExtenderInterface
{
private $addNamespaces = [];
private $replaceNamespaces = [];
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Register a new namespace of laravel views.
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string $namespace
* @param string|array $hints
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function addNamespace($namespace, $hints)
{
$this->addNamespaces[$namespace] = $hints;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
$factory = $container->make(Factory::class);

foreach ($this->addNamespaces as $namespace => $hints) {
$factory->addNamespace($namespace, $hints);
}
}
}
39 changes: 39 additions & 0 deletions tests/integration/extenders/ViewTest.php
Original file line number Diff line number Diff line change
@@ -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 ViewTest extends TestCase
{
/**
* @test
*/
public function custom_view_namespace_does_not_exist_by_default()
{
$this->expectException(\InvalidArgumentException::class);
$this->assertEquals('<html><body>Hello World!</body></html>', trim($this->app()->getContainer()->make(Factory::class)->make('integration.test::test')->render()));
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @test
*/
public function custom_view_namespace_can_be_added_by_extender()
{
$this->extend(
(new Extend\View)
->addNamespace('integration.test', dirname(__FILE__, 2).'/resources/views')
);

$this->assertEquals('<html><body>Hello World!</body></html>', trim($this->app()->getContainer()->make(Factory::class)->make('integration.test::test')->render()));
}
}
1 change: 1 addition & 0 deletions tests/integration/resources/views/test.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>Hello World!</body></html>
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved