buggregator/trap
Buggregator Trap enhances PHP debugging with instant Symfony VarDumper integrations, handy helper functions, and a lightweight local Buggregator server (no Docker). Connect to any Buggregator server and pair with the PhpStorm plugin for a smooth workflow.
Installation:
composer require --dev buggregator/trap -W
This adds Trap as a dev dependency and autoloads its functionality.
First Use: Start the local server in a terminal:
vendor/bin/trap
The server will listen on default ports (9912, 9913, 1025, 8000) and display debug output in the console.
First Debug Call:
In your Laravel code, replace dump() or dd() with:
trap($yourVariable);
This sends the dump to the local Trap server (default sender: console).
dd(): Use td($var) to dump and die (equivalent to trap($var)->die()).trap()->stackTrace() to inspect the current call stack.127.0.0.1 (no config needed).public function show(Request $request, $id)
{
$user = User::findOrFail($id);
tr($user); // Shortcut for `trap($user)->return()`
$response = $this->processData($user);
return tr($response); // Dump response without breaking flow
}
tr() for quick debugging without breaking execution.trap($order)->if($order->isHighPriority())
->depth(2)
->times(1);
Start the server with multiple senders (e.g., console + file):
vendor/bin/trap -sconsole -sfile
storage/logs/trap/ for later review.Trap automatically captures HTTP traffic to 127.0.0.1. For custom hosts:
trap()->http()->host('api.example.test');
Configure Monolog to use Trap as a handler:
$monolog = new Monolog\Logger('name');
$monolog->pushHandler(new \Buggregator\Trap\Handler\TrapHandler());
sentry sender to forward errors to both Sentry and Trap.Bind Trap’s dumper to Laravel’s container for global access:
$this->app->singleton('trap', function () {
return new \Buggregator\Trap\Dumper\Dumper();
});
$app['trap'].Create middleware to trap requests/responses:
public function handle($request, Closure $next)
{
$response = $next($request);
tr($request->all(), response: $response->getContent());
return $response;
}
app/Http/Kernel.php under $routeMiddleware.Extend Trap’s functionality in custom Artisan commands:
public function handle()
{
$this->info('Starting debug session...');
trap()->stackTrace()->depth(5);
}
Use Trap to debug tests without cluttering output:
public function testUserCreation()
{
$user = User::create([...]);
tr($user); // Debug in terminal
$this->assertDatabaseHas('users', $user->toArray());
}
--ui flag for a web interface during tests.Port Conflicts:
9912, 9913, 1025, and 8000. If these are occupied:
vendor/bin/trap -p9999 --ui=8081
TRAP_TCP_PORTS, TRAP_UI_PORT) or specify custom ports.HTTP Traffic Capture:
127.0.0.1 by default. For other hosts:
trap()->http()->host('test.example.com');
Performance Overhead:
trap() calls in production.->times(N) to limit dumps in loops.Phar vs. Composer:
trap.phar) may not autoload Laravel services. Prefer Composer installation for Laravel projects.Symfony VarDumper Conflicts:
symfony/var-dumper, Trap extends its functionality. Ensure no duplicate handlers are registered.Stack Trace Depth:
trap($this)->depth(3);
Conditional Dumping:
->if() to debug specific branches:
trap($user)->if($user->isAdmin());
Return Values:
->return() to dump and return the variable:
$response = tr($this->getData());
UI Mode:
vendor/bin/trap --ui
http://localhost:8000 (or custom port).File Sender:
runtime/trap/ by default. Customize with:
vendor/bin/trap -sfile -d/path/to/custom/dir
Custom Senders:
\Buggregator\Trap\Sender\SenderInterface to create custom handlers (e.g., Slack, Datadog).Protocol Handlers:
\Buggregator\Trap\Protocol\ProtocolInterface.Laravel Service Provider:
public function boot()
{
if ($this->app->environment('local')) {
trap()->http()->host($this->app->url());
}
}
Monolog/Sentry Handlers:
Phar Customization:
build-phar script in the package’s scripts/ directory.TRAP_TCP_HOST: Override the default 127.0.0.1.TRAP_LOG_LEVEL: Set log level (e.g., debug, info). Default: debug.trap()->clear() if needed./) or escape backslashes (\\) in file paths for cross-platform compatibility.Debugging Exceptions:
try {
// Risky code
} catch (\Exception $e) {
td($e, trace: true); // Dump exception + stack trace
}
Database Queries:
$query = User::query()->where(...);
tr($query->toSql(), $query->getBindings());
Queue Jobs:
$job = new ProcessOrder($order);
tr($job)->afterDispatch(); // Debug after job is dispatched
Blade Templates:
@php
tr($user, compact('loop'));
@endphp
API Responses:
return tr(response()->json($data))->header('X-Debug', 'true');
How can I help you explore Laravel packages today?