PHP code example of monkeyscloud / monkeyslegion-devtools

1. Go to this page and download the library: Download monkeyscloud/monkeyslegion-devtools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

monkeyscloud / monkeyslegion-devtools example snippets


use MonkeysLegion\DevTools\DevToolsServiceProvider;

$devtools = new DevToolsServiceProvider();
$profiler = $devtools->boot([
    'enabled'     => true,
    'environment' => 'local',
    'sample_rate' => 1.0,
    'storage'     => ['driver' => 'file', 'path' => 'var/devtools/profiles'],
    'collectors'  => [
        'request'    => true,
        'route'      => true,
        'middleware'  => true,
        'query'      => true,
        'cache'      => true,
        'event'      => true,
        'exception'  => true,
    ],
    'toolbar' => ['enabled' => true],
]);

// Add to your PSR-15 pipeline
$middleware = $devtools->createMiddleware();

// Query profiling — N+1 is detected automatically
$queryCollector = $profiler->getCollector('query');
$queryCollector->recordQuery('SELECT * FROM users WHERE id = ?', durationMs: 2.3, connection: 'mysql');

// Cache tracking — hot keys are ranked automatically
$cacheCollector = $profiler->getCollector('cache');
$cacheCollector->recordOperation('redis', 'user:42', 'get', hit: true, durationMs: 0.4);

// Event timeline — storms are flagged automatically
$eventCollector = $profiler->getCollector('event');
$eventCollector->recordDispatch('App\Event\OrderPlaced', [
    ['name' => 'SendConfirmation', 'duration_ms' => 45.2],
    ['name' => 'UpdateInventory', 'duration_ms' => 12.1],
]);

use MonkeysLegion\DevTools\Contract\CollectorInterface;
use MonkeysLegion\DevTools\Profiler\ProfileContext;

final class MyCollector implements CollectorInterface
{
    public function name(): string { return 'custom'; }
    public function label(): string { return 'Custom'; }
    public function icon(): string { return '🔧'; }
    public function priority(): int { return 500; }
    public function isEnabled(): bool { return true; }

    public function start(ProfileContext $context): void { /* setup */ }
    public function stop(ProfileContext $context): void { /* teardown */ }
    public function collect(ProfileContext $context): array { return [...]; }
}

$profiler->addCollector(new MyCollector());

use MonkeysLegion\DevTools\Attribute\{Profile, IgnoreProfile, Redact};

// Force profiling on a specific route with a label
#[Profile(name: 'checkout.process', althCheck(): Response { }

// Mark sensitive constructor parameters
public function __construct(
    #[Redact] private readonly string $apiSecret,
    #[Redact(replacement: '***')] private readonly string $dbPassword,
) {}

// Profile model — zero getters, all computed
$profile->durationMs           // float: endedAt - startedAt
$profile->durationFormatted    // "42.7ms" | "1.23s" | "850μs"
$profile->isError              // statusCode >= 400
$profile->isSlow               // durationMs > threshold
$profile->statusBadge          // 🟢 🔵 🟠 🔴 ⚪
$profile->memoryPeakFormatted  // "12.4 MB"
$profile->memoryDelta          // bytes used during request
$profile->createdAtFormatted   // "2026-04-27 03:42:53.087"

// Profiler engine — live state hooks
$profiler->isActive            // currently profiling?
$profiler->collectorCount      // number of registered collectors
$profiler->collectorNames      // ['request', 'query', 'cache', ...]

// QueryCollector — computed analytics
$collector->queryCount         // total queries recorded
$collector->totalDurationMs    // sum of all query times
$collector->duplicateCount     // grouped duplicate queries
$collector->hasNPlusOne        // automatic N+1 flag
$collector->slowestQueryMs     // max single query time

// CacheCollector — computed metrics
$collector->hitRatio           // hits / total (float)
$collector->hitRatioFormatted  // "85.7%"
$collector->operationCount     // total operations tracked

// EventCollector — computed state
$collector->hasStorm           // event storm detected?
$collector->failedListenerCount // listeners that threw
$collector->totalListenerMs    // aggregate listener time

// ServiceProvider — boot state
$provider->booted              // has boot() been called?
$provider->profiler            // resolved Profiler instance
$provider->toolbar             // resolved ToolbarRenderer

public private(set) string $id;
public private(set) bool $booted = false;
public private(set) ?Profiler $profiler = null;

use MonkeysLegion\DevTools\Toolbar\AbstractPanel;
use MonkeysLegion\DevTools\Profiler\Profile;

final class MyPanel extends AbstractPanel
{
    public function id(): string { return 'custom'; }
    public function label(): string { return 'Custom'; }
    public function icon(): string { return '🔧'; }
    public function priority(): int { return 400; }

    public function badge(Profile $profile): string { return '3 items'; }
    public function badgeSeverity(Profile $profile): string { return 'ok'; }

    public function render(Profile $profile): string
    {
        return $this->section('Data', $this->renderTable([
            'Key' => 'Value',
        ]));
    }
}