Skip to content

Commit

Permalink
show query; bold log title
Browse files Browse the repository at this point in the history
  • Loading branch information
anshu-krishna committed Sep 12, 2022
1 parent 61e7ab2 commit bbb1116
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
31 changes: 15 additions & 16 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
use Krishna\Neo4j\Protocol\Reply\Success;

$showLogs = array_key_exists('log', $_GET);
if(!$showLogs) { echo '<h3>Note: Add <u>?log</u> to the GET request to see the socket logs.</h3>'; }

// Create Bolt
$bolt = (new BoltMaker(
auth: AuthToken::basic('neo4j', 'open'),
logger: $showLogs ? new Logger(): null
))->makeBolt();

if(!$showLogs) {
echo '<h3>Note: Add <u>?log</u> to the GET request to see the socket logs.</h3>';
}

echo '<strong>Running Bolt version:', $bolt::VERSION, '</strong><br>';

// Run Query
$run = $bolt->query(<<<CYPHER
$query = <<<CYPHER
match (p:Person)-[a:ACTED_IN]->(m:Movie)
return
p as person,
a.roles as roles,
{title: m.title, year: m.released} as movie
limit 2
CYPHER);
CYPHER;
$bolt->logger?->log(str_replace("\r", '', $query), 'Cypher Query');

// Run Query
$run = $bolt->query($query);

if(!$run instanceof Success) {
echo 'Query Error: ', $run->message;
Expand All @@ -35,24 +35,23 @@

$table = [<<<HTML
<table border="1">
<tr>
<th>Person</th> <th>Born</th> <th>Role(s)</th> <th>Movie</th> <th>Released</th>
</tr>
<thead><tr><th>Person</th> <th>Born</th> <th>Role(s)</th> <th>Movie</th> <th>Released</th></tr></thead>
<tbody>
HTML];
// Pull results
foreach($bolt->pull() as $i) {
$roles = implode(', ', $i->roles);
$table[] = <<<"ROW"
<tr>
<td>{$i->person->properties['name']}</td>
<td>{$i->person->properties['born']}</td>
<td>{$roles}</td>
<td>{$i->movie['title']}</td>
<td>{$i->movie['year']}</td>
<td>{$i->person->properties['name']}</td>
<td>{$i->person->properties['born']}</td>
<td>{$roles}</td>
<td>{$i->movie['title']}</td>
<td>{$i->movie['year']}</td>
</tr>
ROW;
}

$table[] = '</table>';
$table[] = '</tbody></table>';

echo implode('', $table);
18 changes: 10 additions & 8 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public static function hexify(string $data, int $rowSize = 20, int $wordSize = 4
}
return trim(implode('', $hex));
}
public function log(string $str, ?string $title = null): void {
public static function htmlEncode(string $text): string {
return htmlentities( $text, flags: ENT_SUBSTITUTE | ENT_HTML5, encoding: 'UTF-8' );
}
public function log(string $text, ?string $title = null): void {
if($this->callback === null) {
$str = ($title === null ? '' : "{$title}:\n") . $str;
echo '<pre>', htmlentities(
$str,
flags: ENT_SUBSTITUTE | ENT_HTML5,
encoding: 'UTF-8'
), '</pre>';
} else { ($this->callback)($str, $title); }
echo '<pre>';
if($title !== null) {
echo '<strong>', static::htmlEncode($title), ":</strong>\n";
}
echo static::htmlEncode($text), '</pre>';
} else { ($this->callback)($text, $title); }
}

public function logRead(string|Buffer $content, ?string $title = null): void {
Expand Down

0 comments on commit bbb1116

Please sign in to comment.