Skip to content

Commit

Permalink
feat: add getNthMatchingFileInStackTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
evolkmann committed Oct 1, 2024
1 parent f651f91 commit 13720db
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/DebugHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace VolkmannDesignCode\KirbyUtils;

class DebugHelpers {

static function getNthMatchingFileInStackTrace(string $pattern, int $nth = 0): string|null
{
$stackTrace = debug_backtrace();
$matches = [];

foreach ($stackTrace as $trace) {
if (isset($trace['file'])) {
if (preg_match($pattern, $trace['file'])) {
$matches[] = $trace['file'];

if (count($matches) - 1 === $nth) {
return $trace['file'];
}
}
}
}

return null;
}

}

0 comments on commit 13720db

Please sign in to comment.