itsgoingd / clockwork

Clockwork - php dev tools in your browser - server-side component
https://underground.works/clockwork
MIT License
5.7k stars 320 forks source link

No output in devtools extension when using `dump()` #631

Closed FeBe95 closed 1 month ago

FeBe95 commented 1 year ago

Description

Whenever I use a dump() statement in my PHP code, nothing is logged in the embedded clockwork console in my browser devtools panel. The web-interface in myapp.localhost/clockwork works perfectly fine though.

Using the network tab of my browser's devtools, I can see that the X-Clockwork headers and response cookies are missing whenever I am using a dump() call.

Example code

routes/web.php:

Route::get('test', "TestController@show");

app/Http/Controllers/TestController.php:

namespace App\Http\Controllers;

class TestController extends Controller {
    public function show(): string
    {
        dump("Output Test");
        return "Hello World!";
    }
}

Expected Result

Dumping data with dump() should not interfere with the logging behavior of the browser extension. The necessary data does get generated and saved, as it is available in the standalone web-interface. I understand if it doesn't work with Laravel's dd() function, as this function call exit(1) and thus ends the PHP process directly.

Is there something I need to configure in Laravel or Clockwork to get this working?

Actual Result

Browser:

grafik

Browser Extension (DevTools Panel):

grafik

Web-Interface:

grafik

itsgoingd commented 1 year ago

Hey, thanks for the detailed report.

Unfortunately I think this is a sort-of limitation of how dump works. Looks like dump immediately sends the output to the browser, this means headers are also sent at the point when you call dump.

Clockwork headers are set at the end of the request processing, but since at that point headers were already sent, this has no effect. The web UI still works, since it does not use the Clockwork headers at all and instead polls the Clockwork api for new requests.

FeBe95 commented 1 year ago

Hello and thanks for your reply. Would it technically be possible to set the necessary clockwork headers at the start of each request, so a dump() call wouldn't interfere with them?

itsgoingd commented 1 year ago

Yeah, it is technically possible, there is no nice Laravel way to set the headers at the right time, but you could just use the underlying header php function at the start of a global middleware:

header('X-Clockwork-Id: ' . $this->app['clockwork']->request()->id);
header('X-Clockwork-Version: ' . \Clockwork\Clockwork::VERSION);

I don't think this is something we want to do in Clockwork though.