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 6, 2022
1 parent 1585255 commit 2e2d409
Show file tree
Hide file tree
Showing 2 changed files with 30 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
25 changes: 25 additions & 0 deletions tests/dibi/Event.source.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

declare(strict_types=1);

use Dibi\Event;
use Tester\Assert;

require __DIR__ . '/bootstrap.php';

$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");

$conn->onEvent[] = function (Event $event) use (&$expectedLine) {
Assert::same([__FILE__, $expectedLine], $event->source);
};

$expectedLine = __LINE__ + 1;
$conn->connect();

$expectedLine = __LINE__ + 1;
eval('$conn->nativeQuery("SELECT 1");');

0 comments on commit 2e2d409

Please sign in to comment.