/
View.php
99 lines (88 loc) · 3.54 KB
/
View.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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 Flarum\Foundation\Paths;
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.
*
* 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
*
* @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 namespace(string $namespace, $hints): self
{
$this->namespaces[$namespace] = $hints;
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);
}
});
}
/**
* @param Container $container
* @param Extension $extension
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function onEnable(Container $container, Extension $extension)
{
$storagePath = $container->make(Paths::class)->storage;
array_map('unlink', glob($storagePath.'/views/*'));
}
/**
* @param Container $container
* @param Extension $extension
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function onDisable(Container $container, Extension $extension)
{
$storagePath = $container->make(Paths::class)->storage;
array_map('unlink', glob($storagePath.'/views/*'));
}
}