ForgeDebugBar Module

Overview

ForgeDebugBar is a powerful development tool that provides real-time debugging and performance monitoring capabilities for your Forge application. It offers a comprehensive set of features to help you analyze and optimize your application's behavior during development.

Features

The debug bar provides extensive information about your application's execution:

  • Exception Tracking: Monitor and analyze application exceptions and errors
  • View Information: Track rendered views and their render times
  • Route Analysis: Examine current route and routing information
  • Query Monitor: View executed database queries and their performance
  • Session Data: Inspect session variables and data
  • Request Details: Access request parameters and headers
  • Timeline: Visualize application execution timeline
  • Messages: View application logs and debug messages
  • System Info: Monitor PHP version and memory usage

Configuration

The debug bar is configured through the config/forge_debug_bar.php file:

// config/forge_debug_bar.php
return [
    'enabled' => true,
    'resources' => [
        'css' => ['css/debugbar.css'],
        'js' => ['js/debugbar.js'],
    ]
];

The debug bar is automatically enabled when FORGE_APP_DEBUG is set to true in your environment configuration.

Manual Data Collectors

While most collectors work automatically, three collectors require manual usage through the Debug helper class:

1. Message Collector

use Forge\Core\Helpers\Debug;

// Log a message with optional label
Debug::message('User login successful', 'info');
Debug::message(['user_id' => 1, 'action' => 'login'], 'debug');

2. Exception Collector

use Forge\Core\Helpers\Debug;

try {
    // Your code here
} catch (\Throwable $exception) {
    Debug::exceptionCollector($exception);
    throw $exception;
}

3. Timeline Collector

use Forge\Core\Helpers\Debug;

// Track a new event in the timeline
Debug::addEvent('database-query', 'Database', [
    'query' => 'SELECT * FROM users',
    'time' => 0.5
]);

Usage Example

Here's a practical example combining multiple debug features:

use Forge\Core\Helpers\Debug;

class UserController
{
    public function show(int $id)
    {
        try {
            // Log the start of the operation
            Debug::message("Fetching user with ID: {$id}", 'info');

            // Add timeline event for database query
            Debug::addEvent('fetch-user', 'Database', [
                'user_id' => $id
            ]);

            $user = $this->userRepository->find($id);

            if (!$user) {
                throw new UserNotFoundException("User {$id} not found");
            }

            // Log successful retrieval
            Debug::message([
                'user_found' => true,
                'user_data' => $user->toArray()
            ], 'debug');

            return $this->view->render('user.profile', ['user' => $user]);

        } catch (\Throwable $e) {
            // Collect the exception
            Debug::exceptionCollector($e);
            throw $e;
        }
    }
}