/
Assets.php
82 lines (68 loc) · 2.08 KB
/
Assets.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
<?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\Frontend\Content;
use Flarum\Foundation\Config;
use Flarum\Frontend\Compiler\CompilerInterface;
use Flarum\Frontend\Document;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;
class Assets
{
protected $container;
protected $config;
/**
* @var \Flarum\Frontend\Assets
*/
protected $assets;
public function __construct(Container $container, Config $config)
{
$this->container = $container;
$this->config = $config;
}
public function forFrontend(string $name)
{
$this->assets = $this->container->make('flarum.assets.'.$name);
return $this;
}
public function __invoke(Document $document, Request $request)
{
$locale = $request->getAttribute('locale');
$compilers = [
'js' => [$this->assets->makeJs(), $this->assets->makeLocaleJs($locale)],
'css' => [$this->assets->makeCss(), $this->assets->makeLocaleCss($locale)]
];
if ($this->config->inDebugMode()) {
$this->forceCommit(Arr::flatten($compilers));
}
$document->js = array_merge($document->js, $this->getUrls($compilers['js']));
$document->css = array_merge($document->css, $this->getUrls($compilers['css']));
}
/**
* Force compilation of assets when in debug mode.
*
* @param array $compilers
*/
private function forceCommit(array $compilers)
{
/** @var CompilerInterface $compiler */
foreach ($compilers as $compiler) {
$compiler->commit(true);
}
}
/**
* @param CompilerInterface[] $compilers
* @return string[]
*/
private function getUrls(array $compilers)
{
return array_filter(array_map(function (CompilerInterface $compiler) {
return $compiler->getUrl();
}, $compilers));
}
}