Skip to content

Commit 9b8ea3c

Browse files
authored
Add livewire support (#10)
* add livewire support * cs
1 parent 25b6f6b commit 9b8ea3c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Middleware/RenderTorchlight.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Torchlight\Middleware;
44

55
use Closure;
6+
use Illuminate\Http\JsonResponse;
67
use Illuminate\Http\Request;
78
use Illuminate\Http\Response;
89
use Illuminate\Support\Str;
@@ -21,11 +22,30 @@ public function handle(Request $request, Closure $next)
2122
{
2223
$response = $next($request);
2324

25+
if ($response instanceof JsonResponse && class_exists('\\Livewire\\Livewire') && \Livewire\Livewire::isLivewireRequest()) {
26+
return $this->handleLivewireRequest($response);
27+
}
28+
2429
// Must be a regular, HTML response.
2530
if (!$response instanceof Response || !Str::contains($response->headers->get('content-type'), 'html')) {
2631
return $response;
2732
}
2833

2934
return BladeManager::renderResponse($response);
3035
}
36+
37+
protected function handleLivewireRequest(JsonResponse $response)
38+
{
39+
if (!BladeManager::getBlocks()) {
40+
return $response;
41+
}
42+
43+
$data = $response->getData();
44+
45+
$html = BladeManager::renderContent(data_get($data, 'effects.html'));
46+
47+
data_set($data, 'effects.html', $html);
48+
49+
return $response->setData($data);
50+
}
3151
}

src/TorchlightServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Torchlight\Blade\BladeManager;
1010
use Torchlight\Blade\CodeComponent;
1111
use Torchlight\Commands\Install;
12+
use Torchlight\Middleware\RenderTorchlight;
1213

1314
class TorchlightServiceProvider extends ServiceProvider
1415
{
@@ -18,6 +19,7 @@ public function boot()
1819
$this->registerCommands();
1920
$this->publishConfig();
2021
$this->registerBladeComponent();
22+
$this->registerLivewire();
2123
}
2224

2325
public function bindManagerSingleton()
@@ -60,6 +62,15 @@ public function registerBladeComponent()
6062
]);
6163
}
6264

65+
public function registerLivewire()
66+
{
67+
if (class_exists('\\Livewire\\Livewire')) {
68+
\Livewire\Livewire::addPersistentMiddleware([
69+
RenderTorchlight::class,
70+
]);
71+
}
72+
}
73+
6374
public function register()
6475
{
6576
$this->mergeConfigFrom(__DIR__ . '/../config/torchlight.php', 'torchlight');

0 commit comments

Comments
 (0)