PHP code example of phpner / laravel-profiler

1. Go to this page and download the library: Download phpner/laravel-profiler 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/ */

    

phpner / laravel-profiler example snippets


// app/Http/Kernel.php
protected $middleware = [
    // ...
    \Phpner\MemoryProfiler\Middleware\HttpProfiler::class,
];

use Illuminate\Console\Command;
use Phpner\MemoryProfiler\Console\Concerns\ProfilesMemory;

class ImportUsers extends Command
{
    use ProfilesMemory;

    protected $signature = 'users:import';

    public function handle(): int
    {
        return $this->withMemoryProfiling(function () {
            // your command logic
            $this->info('Importing...');
            // ...
            return 0; // exit code
        });
    }
}

use Phpner\MemoryProfiler\MemoryProfiler; // service
use Phpner\MemoryProfiler\Facades\MemoryProfiler as MP; // facade

// 1) Service
public function show(MemoryProfiler $profiler)
{
    $ctx = $profiler->start('code');
    // ... your code ...
    $profiler->stop($ctx, [
        'label' => 'users.show',
        'meta'  => ['id' => 42],
    ]);
}

// 2) Facade
$ctx = MP::start('code');
// expensive work
MP::stop($ctx, ['label' => 'expensive.segment']);

return [
    'enabled'          => env('MEMORY_PROFILER_ENABLED', true),
    'driver'           => env('MEMORY_PROFILER_DRIVER', 'log'), // log | telescope | both
    'threshold'        => env('MEMORY_PROFILER_THRESHOLD', 0),  // bytes; 0 = log everything
    'response_headers' => env('MEMORY_PROFILER_HEADERS', true),
];