PHP code example of ndrx / profiler

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

    

ndrx / profiler example snippets

 php
$profiler->start('foo', 'Bar');
$profiler->stop('foo');
$this->profiler->monitor('Foobar', function() {
   // very long process
});
 php
$profiler->debug('No beer');
$profiler->info('No beer');
$profiler->notice('No beer');
$profiler->alert('No beer');
$profiler->error('No beer');
$profiler->emergency('No beer');
$profiler->critical('No beer');
 php
$profiles = $profiler->getDatasource()->all(0, 10);
 php
$id = '1576efef8ea36c74b533238affc3eaec7f94561d';
$profile = $profiler->getProfile($id);
 php
$profile = $profiler->getDatasource()->clear();
 php
$profiler = ProfilerFactory::build([
    // ...
    ProfilerFactory::LOGGER => Ndrx\Profiler\Components\Logs\Monolog::class
]);

// $log is your instance of Monolog\Logger
$log->pushHandler($profiler->getLogger();
 php


namespace /Your/Namespace;

use Ndrx\Profiler\Collectors\Collector;
use Ndrx\Profiler\Collectors\Contracts\StartCollectorInterface;

class Foo extends Collector implements StartCollectorInterface
{
    /**
     * Fetch data
     * @return mixed
     */
    public function resolve()
    {
        $this->data = 'bar';
    }

    /**
     * The path in the final json
     * @example
     *  path /aa/bb
     *  will be transformed to
     *  {
     *     aa : {
     *              bb: <VALUE OF RESOLVE>
     *       }
     *  }
     * @return string
     */
    public function getPath()
    {
        return 'foo';
    }
}
 php


namespace /Your/Namespace;

use Ndrx\Profiler\Collectors\Collector;
use Ndrx\Profiler\Collectors\Contracts\FinalCollectorInterface;

class Foo extends Collector implements FinalCollectorInterface
{
    /**
     * Fetch data
     * @return mixed
     */
    public function resolve()
    {
        $this->data = 'bar';
    }

    /**
     * The path in the final json
     * @example
     *  path /aa/bb
     *  will be transformed to
     *  {
     *     aa : {
     *              bb: <VALUE OF RESOLVE>
     *       }
     *  }
     * @return string
     */
    public function getPath()
    {
        return 'foo';
    }
}
 php


namespace Ndrx\Profiler\Collectors\Data;

use Ndrx\Profiler\Collectors\StreamCollector;
use Ndrx\Profiler\Events\Log as LogEvent;
use Ndrx\Profiler\JsonPatch;

class Log extends StreamCollector
{

    protected function registerListeners()
    {
        // add a listener to your event dispatcher, the profiler has a build-in dispatcher than use can use
        $this->process->getDispatcher()->addListener(LogEvent::EVENT_NAME, function (LogEvent $event) {
            // fetch event data
            $this->data = $event->toArray();
            // stream to the data source
            $this->stream();
        });
    }

    /**
     * The path in the final json
     * @example
     *  path /aa/bb
     *  will be transformed to
     *  {
     *     aa : {
     *              bb: <VALUE OF RESOLVE>
     *       }
     *  }
     * @return mixed
     */
    public function getPath()
    {
        return 'logs';
    }

    /**
     * Write data in the datasource and clean current buffer
     * @return mixed
     */
    public function stream()
    {
        // generation of the json patch from data
        $patch = $this->jsonPatch->generate($this->getPath(), JsonPatch::ACTION_ADD, $this->data, true);
        // save the json patch in the datasource
        $this->dataSource->save($this->process, [$patch]);
        // clean data array to avoid duplicate entry
        $this->data = [];
    }
}