Skip to content

Commit 7fa05f3

Browse files
maryodg
authored andcommitted
Event: detecting source without filesystem check (#428)
Co-authored-by: Marek Stipek <[email protected]>
1 parent 3a962de commit 7fa05f3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Dibi/Event.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public function __construct(Connection $connection, int $type, ?string $sql = nu
7070

7171
$dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
7272
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
73-
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
73+
if (
74+
isset($row['file'])
75+
&& preg_match('~\.(php.?|phtml)$~', $row['file'])
76+
&& substr($row['file'], 0, strlen($dibiDir)) !== $dibiDir
77+
) {
7478
$this->source = [$row['file'], (int) $row['line']];
7579
break;
7680
}

tests/dibi/Event.source.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tester\Assert;
6+
7+
require __DIR__ . '/bootstrap.php';
8+
9+
10+
$conn = new Dibi\Connection($config);
11+
12+
$event = new Dibi\Event($conn, Dibi\Event::CONNECT);
13+
Assert::same([__FILE__, __LINE__ - 1], $event->source);
14+
15+
eval('$event = new Dibi\Event($conn, Dibi\Event::CONNECT);');
16+
Assert::same([__FILE__, __LINE__ - 1], $event->source);
17+
18+
array_map(function () use ($conn) {
19+
$event = new Dibi\Event($conn, Dibi\Event::CONNECT);
20+
Assert::same([__FILE__, __LINE__ - 1], $event->source);
21+
}, [null]);

0 commit comments

Comments
 (0)