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

feat/fix: allow replacing of blade template namespaces #3167

Merged
merged 19 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Flarum\Locale\LocaleManager;
use Flarum\Settings\Event\Saved;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Laminas\Stratigility\MiddlewarePipe;

class AdminServiceProvider extends AbstractServiceProvider
Expand Down Expand Up @@ -121,9 +122,9 @@ public function register()
/**
* {@inheritdoc}
*/
public function boot()
public function boot(ViewFactory $views)
{
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');
$views->addNamespace('flarum.admin', __DIR__.'/../../views');

$events = $this->container->make('events');

Expand Down
31 changes: 28 additions & 3 deletions src/Extend/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory;

/**
* Views are PHP files that use the Laravel Blade syntax for creation of server-side generated HTML.
*
* Flarum's core uses them for error pages, the installer, HTML emails, and the skeletons for the forum and admin sites.
*/
class View implements ExtenderInterface, LifecycleInterface
{
private $namespaces = [];
private $prependNamespaces = [];

/**
* 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/8.x/Illuminate/View/Factory.html#method_make
* You can also pass variables into a view. For more information, see: https://laravel.com/api/8.x/Illuminate/View/Factory.html#method_make
*
* @param string $namespace: The name of the namespace.
* @param string|string[] $hints: This is a path (or an array of paths) to the folder(s)
Expand All @@ -41,12 +45,33 @@ public function namespace(string $namespace, $hints): self
return $this;
}

/**
* Extend an existing namespace of Laravel views.
*
* To extend an existing namespace, you will need to put views in a folder in your extension,
* and register that folder under the existing namespace with this extender.
*
* @param string $namespace: The name of the namespace.
* @param string|string[] $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 self
*/
public function extendNamespace(string $namespace, $hints): self
{
$this->prependNamespaces[$namespace] = $hints;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
$container->resolving(Factory::class, function (Factory $view) {
foreach ($this->namespaces as $namespace => $hints) {
$view->addNamespace($namespace, $hints);
}
foreach ($this->prependNamespaces as $namespace => $hints) {
$view->prependNamespace($namespace, $hints);
}
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/Forum/ForumServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Laminas\Stratigility\MiddlewarePipe;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand Down Expand Up @@ -131,11 +131,11 @@ public function register()
});
}

public function boot(Container $container, Dispatcher $events, Factory $view)
public function boot(Container $container, Dispatcher $events, ViewFactory $views)
{
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum');
$views->addNamespace('flarum.forum', __DIR__.'/../../views');

$view->share([
$views->share([
davwheat marked this conversation as resolved.
Show resolved Hide resolved
'translator' => $container->make(TranslatorInterface::class),
'settings' => $container->make(SettingsRepositoryInterface::class)
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/FrontendServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function (Container $container) {
*/
public function boot(Container $container, ViewFactory $views)
{
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum');
$views->addNamespace('flarum', __DIR__.'/../../views');

$views->share([
'translator' => $container->make('translator'),
Expand Down
5 changes: 3 additions & 2 deletions src/Install/InstallServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory as ViewFactory;

class InstallServiceProvider extends AbstractServiceProvider
{
Expand All @@ -29,9 +30,9 @@ public function register()
/**
* {@inheritdoc}
*/
public function boot(Container $container, RouteHandlerFactory $route)
public function boot(Container $container, RouteHandlerFactory $route, ViewFactory $views)
{
$this->loadViewsFrom(__DIR__.'/../../views/install', 'flarum.install');
$views->addNamespace('flarum.install', __DIR__.'/../../views/install');

$this->populateRoutes($container->make('flarum.install.routes'), $route);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Update/UpdateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\View\Factory as ViewFactory;

class UpdateServiceProvider extends AbstractServiceProvider
{
Expand All @@ -30,9 +31,9 @@ public function register()
});
}

public function boot()
public function boot(ViewFactory $views)
{
$this->loadViewsFrom(__DIR__.'/../../views/install', 'flarum.update');
$views->addNamespace('flarum.update', __DIR__.'/../../views/install');
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/views/override/frontend/app.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>We have overridden the core app view.</body></html>
30 changes: 30 additions & 0 deletions tests/integration/extenders/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,34 @@ public function custom_view_namespace_can_be_added_by_extender()

$this->assertEquals('<html><body>Hello World!</body></html>', trim($this->app()->getContainer()->make(Factory::class)->make('integration.test::test')->render()));
}

/**
* @test
*/
public function can_add_view_to_namespace_by_prepend_extender()
{
$this->extend(
(new Extend\View)
->extendNamespace('flarum', dirname(__FILE__, 3).'/fixtures/views')
);

$this->assertEquals('<html><body>Hello World!</body></html>', trim($this->app()->getContainer()->make(Factory::class)->make('flarum::test')->render()));
}

/**
* @test
*/
public function can_override_view_in_namespace_by_prepend_extender()
{
$this->extend(
(new Extend\View)
->extendNamespace('flarum', dirname(__FILE__, 3).'/fixtures/views/override')
);

$response = $this->send(
$this->request('GET', '/')
);

$this->assertEquals('<html><body>We have overridden the core app view.</body></html>', trim($response->getBody()->getContents()));
}
}