Examples
1
Switching DB Drivers For Tests
<?php
use Patchwork\{redefine, getFunction, always};
$pdo = new PDO('sqlite:/tmp/demo.db');
redefine('pg_connect', always(null));
redefine('pg_query', function($query, ...$params) use ($pdo) {
$stmt = $pdo->prepare($query);
$stmt->execute($params);
return $stmt;
});
redefine('pg_fetch_assoc', function(PDOStatement $stmt) {
return $stmt->fetchAssoc();
});
# These will use SQLite instead:
pgsql_connect();
$result = pg_query('SELECT * FROM posts');
var_dump(pg_fetch_assoc($result));
2
A Do-It-Yourself Profiler
<?php
use function Patchwork\{redefine, relay, getMethod};
$profiling = fopen('profiling.csv', 'w');
redefine('App\*', function(...$args) use ($profiling) {
$begin = microtime(true);
$result = relay();
$end = microtime(true);
fputcsv($profiling, [getMethod(), $end - $begin]);
return $result;
});