/
SupportAutoInjectedAssets.php
95 lines (75 loc) · 3.25 KB
/
SupportAutoInjectedAssets.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
<?php
namespace Livewire\Features\SupportAutoInjectedAssets;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Livewire\ComponentHook;
use Livewire\Features\SupportScriptsAndAssets\SupportScriptsAndAssets;
use Livewire\Mechanisms\FrontendAssets\FrontendAssets;
use function Livewire\on;
class SupportAutoInjectedAssets extends ComponentHook
{
static $hasRenderedAComponentThisRequest = false;
static $forceAssetInjection = false;
static function provide()
{
on('flush-state', function () {
static::$hasRenderedAComponentThisRequest = false;
static::$forceAssetInjection = false;
});
app('events')->listen(RequestHandled::class, function ($handled) {
// If this is a successful HTML response...
if (! str($handled->response->headers->get('content-type'))->contains('text/html')) return;
if (! method_exists($handled->response, 'status') || $handled->response->status() !== 200) return;
$assetsHead = '';
$assetsBody = '';
$assets = array_values(SupportScriptsAndAssets::getAssets());
// If there are additional head assets, inject those...
if (count($assets) > 0) {
foreach ($assets as $asset) {
$assetsHead .= $asset."\n";
}
}
// If we're injecting Livewire assets...
if (static::shouldInjectLivewireAssets()) {
$assetsHead .= FrontendAssets::styles()."\n";
$assetsBody .= FrontendAssets::scripts()."\n";
}
if ($assetsHead === '' && $assetsBody === '') return;
$html = $handled->response->getContent();
if (str($html)->contains('</html>')) {
$originalContent = $handled->response->original;
$handled->response->setContent(static::injectAssets($html, $assetsHead, $assetsBody));
$handled->response->original = $originalContent;
}
});
}
protected static function shouldInjectLivewireAssets()
{
if (! static::$forceAssetInjection && config('livewire.inject_assets', true) === false) return false;
if ((! static::$hasRenderedAComponentThisRequest) && (! static::$forceAssetInjection)) return false;
if (app(FrontendAssets::class)->hasRenderedScripts) return false;
return true;
}
protected static function getLivewireAssets()
{
$livewireStyles = FrontendAssets::styles();
$livewireScripts = FrontendAssets::scripts();
}
public function dehydrate()
{
static::$hasRenderedAComponentThisRequest = true;
}
static function injectAssets($html, $assetsHead, $assetsBody)
{
$html = str($html);
if ($html->test('/<\s*\/\s*head\s*>/i') && $html->test('/<\s*\/\s*body\s*>/i')) {
return $html
->replaceMatches('/(<\s*\/\s*head\s*>)/i', $assetsHead.'$1')
->replaceMatches('/(<\s*\/\s*body\s*>)/i', $assetsBody.'$1')
->toString();
}
return $html
->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1'.$assetsHead)
->replaceMatches('/(<\s*\/\s*html\s*>)/i', $assetsBody.'$1')
->toString();
}
}