Skip to content

Commit

Permalink
Detecting event source without filesystem check
Browse files Browse the repository at this point in the history
  • Loading branch information
maryo committed Nov 7, 2022
1 parent 1585255 commit 93e84bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Dibi/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public function __construct(Connection $connection, int $type, ?string $sql = nu

$dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
if (isset($row['file']) && is_file($row['file']) && !str_starts_with($row['file'], $dibiDir)) {
if (
isset($row['file'])
&& !str_starts_with($row['file'], $dibiDir)
&& !str_ends_with($row['file'], ": eval()'d code")
) {
$this->source = [$row['file'], (int) $row['line']];
break;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/dibi/Event.source.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @dataProvider ../databases.ini
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/bootstrap.php';

$conn = new Dibi\Connection($config);

$event = new Dibi\Event($conn, Dibi\Event::CONNECT);
Assert::same([__FILE__, __LINE__ - 1], $event->source);

eval('$event = new Dibi\Event($conn, Dibi\Event::CONNECT);');
Assert::same([__FILE__, __LINE__ - 1], $event->source);

array_map(function () use ($conn) {
$event = new Dibi\Event($conn, Dibi\Event::CONNECT);
Assert::same([__FILE__, __LINE__ - 1], $event->source);
}, [null]);

0 comments on commit 93e84bc

Please sign in to comment.