|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Leuverink\Bundle; |
| 4 | + |
| 5 | +use SplFileInfo; |
| 6 | +use Leuverink\Bundle\Traits\Constructable; |
| 7 | +use Illuminate\Foundation\Http\Events\RequestHandled; |
| 8 | +use Leuverink\Bundle\Contracts\BundleManager as BundleManagerContract; |
| 9 | + |
| 10 | +class InjectCore |
| 11 | +{ |
| 12 | + use Constructable; |
| 13 | + |
| 14 | + /** Injects a inline script tag containing Bundle's core inside every full-page response */ |
| 15 | + public function __invoke(RequestHandled $handled) |
| 16 | + { |
| 17 | + $html = $handled->response->getContent(); |
| 18 | + |
| 19 | + // Skip if request doesn't return a full page |
| 20 | + if (! str_contains($html, '</html>')) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // Skip if core was included before |
| 25 | + if (str_contains($html, '<!--[BUNDLE-CORE]-->')) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + // Bundle it up & wrap in script tag |
| 30 | + $script = $this->wrapInScriptTag( |
| 31 | + file_get_contents($this->bundle()) |
| 32 | + ); |
| 33 | + |
| 34 | + // Inject into response |
| 35 | + $originalContent = $handled->response->original; |
| 36 | + |
| 37 | + $handled->response->setContent( |
| 38 | + $this->injectAssets($html, $script) |
| 39 | + ); |
| 40 | + |
| 41 | + $handled->response->original = $originalContent; |
| 42 | + } |
| 43 | + |
| 44 | + public function bundle(): SplFileInfo |
| 45 | + { |
| 46 | + return $this->manager()->bundle( |
| 47 | + $this->core() |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** Get an instance of the BundleManager */ |
| 52 | + protected function manager(): BundleManagerContract |
| 53 | + { |
| 54 | + return BundleManager::new(); |
| 55 | + } |
| 56 | + |
| 57 | + /** Injects Bundle's core into given html string (taken from Livewire's injection mechanism) */ |
| 58 | + protected function injectAssets(string $html, string $core): string |
| 59 | + { |
| 60 | + $html = str($html); |
| 61 | + |
| 62 | + if ($html->test('/<\s*\/\s*head\s*>/i')) { |
| 63 | + return $html |
| 64 | + ->replaceMatches('/(<\s*\/\s*head\s*>)/i', $core . '$1') |
| 65 | + ->toString(); |
| 66 | + } |
| 67 | + |
| 68 | + return $html |
| 69 | + ->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1' . $core) |
| 70 | + ->toString(); |
| 71 | + } |
| 72 | + |
| 73 | + /** Wrap the contents in a inline script tag */ |
| 74 | + protected function wrapInScriptTag($contents): string |
| 75 | + { |
| 76 | + return <<< HTML |
| 77 | + <!--[BUNDLE-CORE]--> |
| 78 | + <script type="module" data-bundle="core"> |
| 79 | + {$contents} |
| 80 | + </script> |
| 81 | + <!--[ENDBUNDLE]>--> |
| 82 | + HTML; |
| 83 | + } |
| 84 | + |
| 85 | + protected function core(): string |
| 86 | + { |
| 87 | + $timeout = $this->manager()->config()->get('import_resolution_timeout'); |
| 88 | + |
| 89 | + return <<< JS |
| 90 | +
|
| 91 | + //-------------------------------------------------------------------------- |
| 92 | + // Expose x_import_modules map |
| 93 | + //-------------------------------------------------------------------------- |
| 94 | + if(!window.x_import_modules) window.x_import_modules = {}; |
| 95 | +
|
| 96 | +
|
| 97 | + //-------------------------------------------------------------------------- |
| 98 | + // Expose _import function |
| 99 | + //-------------------------------------------------------------------------- |
| 100 | + window._import = async function(alias, exportName = 'default') { |
| 101 | +
|
| 102 | + // Wait for module to become available (account for invoking from non-deferred script) |
| 103 | + const module = await poll( |
| 104 | + () => window.x_import_modules[alias], |
| 105 | + {$timeout}, 5, alias |
| 106 | + ) |
| 107 | +
|
| 108 | + if(module === undefined) { |
| 109 | + console.info('When invoking _import() from a script tag make sure it has type="module"') |
| 110 | + throw `BUNDLE ERROR: '\${alias}' not found`; |
| 111 | + } |
| 112 | +
|
| 113 | + return module[exportName] !== undefined |
| 114 | + // Return export if it exists |
| 115 | + ? module[exportName] |
| 116 | + // Otherwise the entire module |
| 117 | + : module |
| 118 | + }; |
| 119 | +
|
| 120 | +
|
| 121 | + //-------------------------------------------------------------------------- |
| 122 | + // Non-blocking polling mechanism |
| 123 | + //-------------------------------------------------------------------------- |
| 124 | + async function poll(success, timeout, interval, ref) { |
| 125 | + const startTime = new Date().getTime(); |
| 126 | +
|
| 127 | + while (true) { |
| 128 | + // If the success callable returns something truthy, return |
| 129 | + let result = success() |
| 130 | + if (result) return result; |
| 131 | +
|
| 132 | + // Check if timeout has elapsed |
| 133 | + const elapsedTime = new Date().getTime() - startTime; |
| 134 | + if (elapsedTime >= timeout) { |
| 135 | + throw `BUNDLE TIMEOUT: '\${ref}' could not be resolved`; |
| 136 | + } |
| 137 | +
|
| 138 | + // Wait for a set interval |
| 139 | + await new Promise(resolve => setTimeout(resolve, interval)); |
| 140 | + } |
| 141 | + }; |
| 142 | +
|
| 143 | + JS; |
| 144 | + } |
| 145 | +} |
0 commit comments