Skip to content

Commit

Permalink
Add support for artisan commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritvars Zvejnieks committed Jul 21, 2023
1 parent ed59ded commit 8a1e730
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ return [
The default profile enabler uses `enabler.probability`, `enabler.exclude` and `enabler.include` config parameters to determine if the current request should be profiled.
Feel free to make your own class that implements `\Ritvarsz\LaravelXhgui\Support\SerializableClosure` to override the logic or extend the `DefaultProfileEnabler` to add more constraints.

## Todo:
- Support for profiling artisan commands.
- Support for profiling jobs.
## Profiling artisan commands
In order to profile an artisan command, it has to use the `\Ritvarsz\LaravelXhgui\Traits\Profilable` trait.

Example:
```php
class TestCmd extends Command
{
use \App\Traits\Profilable; // <- use the trait

protected $signature = 'test:cmd';
protected $description = 'Command description';

public function __construct()
{
$this->initXhprof(); // <- initialize the profiler in the constructor
parent::__construct();
}

public function handle()
{
/** Do something */
}
}
```
20 changes: 20 additions & 0 deletions src/Traits/Profilable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Ritvarsz\LaravelXhgui\Traits;

use Ritvarsz\LaravelXhgui\XHGuiMiddleware;

trait Profilable
{
public function initXhprof()
{
// Should only run in console
// HTTP requests are handled by XHGuiMiddleware
// Though, havent seen any issues with starting the profiler multiple times.
if (!app()->runningInConsole()) {
return;
}

XHGuiMiddleware::enableProfiler();
}
}
9 changes: 7 additions & 2 deletions src/XHGuiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class XHGuiMiddleware
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
self::enableProfiler();

return $next($request);
}

public static function enableProfiler()
{
$config = config('xhgui');

Expand All @@ -37,7 +44,5 @@ public function handle(Request $request, Closure $next)
$profilerConfig = new ProfilerConfig($config);
$profiler = new Profiler($profilerConfig);
$profiler->start();

return $next($request);
}
}

0 comments on commit 8a1e730

Please sign in to comment.