Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117929403
D4576.1775461511.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
D4576.1775461511.diff
View Options
diff --git a/src/app/Listeners/SqlDebug.php b/src/app/Listeners/SqlDebug.php
new file mode 100644
--- /dev/null
+++ b/src/app/Listeners/SqlDebug.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace App\Listeners;
+
+use Illuminate\Database\Events\QueryExecuted;
+use Illuminate\Database\Events\TransactionBeginning;
+use Illuminate\Database\Events\TransactionCommitted;
+use Illuminate\Database\Events\TransactionRolledBack;
+use Illuminate\Events\Dispatcher;
+
+class SqlDebug
+{
+ /**
+ * Register the listeners for the subscriber.
+ *
+ * @return array<string, string>
+ */
+ public function subscribe(Dispatcher $events): array
+ {
+ return [
+ QueryExecuted::class => 'handle',
+ TransactionBeginning::class => 'handle',
+ TransactionCommitted::class => 'handle',
+ TransactionRolledBack::class => 'handle'
+ ];
+ }
+
+ /**
+ * Handle the event.
+ *
+ * @param object $event An event object
+ */
+ public function handle(object $event): void
+ {
+ switch(get_class($event)) {
+ case TransactionBeginning::class:
+ $query = 'begin';
+ break;
+ case TransactionCommitted::class:
+ $query = 'commit';
+ break;
+ case TransactionRolledBack::class:
+ $query = 'rollback';
+ break;
+ default:
+ $query = sprintf(
+ '%s [%s]: %.4f sec.',
+ $event->sql,
+ self::serializeSQLBindings($event->bindings, $event->sql),
+ $event->time / 1000
+ );
+ }
+
+ \Log::debug("[SQL] {$query}");
+ }
+
+ /**
+ * Serialize a bindings array to a string.
+ */
+ private static function serializeSQLBindings(array $array, string $sql): string
+ {
+ $ipv = preg_match('/ip([46])nets/', $sql, $m) ? $m[1] : null;
+
+ $serialized = array_map(function ($entry) use ($ipv) {
+ if ($entry instanceof \DateTime) {
+ return $entry->format('Y-m-d h:i:s');
+ } elseif ($ipv && is_string($entry) && strlen($entry) == ($ipv == 6 ? 16 : 4)) {
+ // binary IP address? use HEX representation
+ return '0x' . bin2hex($entry);
+ }
+
+ return $entry;
+ }, $array);
+
+ return implode(', ', $serialized);
+ }
+}
diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php
--- a/src/app/Providers/AppServiceProvider.php
+++ b/src/app/Providers/AppServiceProvider.php
@@ -4,7 +4,6 @@
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Blade;
-use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;
@@ -20,27 +19,6 @@
Passport::ignoreRoutes();
}
- /**
- * Serialize a bindings array to a string.
- */
- private static function serializeSQLBindings(array $array, string $sql): string
- {
- $ipv = preg_match('/ip([46])nets/', $sql, $m) ? $m[1] : null;
-
- $serialized = array_map(function ($entry) use ($ipv) {
- if ($entry instanceof \DateTime) {
- return $entry->format('Y-m-d h:i:s');
- } elseif ($ipv && is_string($entry) && strlen($entry) == ($ipv == 6 ? 16 : 4)) {
- // binary IP address? use HEX representation
- return '0x' . bin2hex($entry);
- }
-
- return $entry;
- }, $array);
-
- return implode(', ', $serialized);
- }
-
/**
* Load the override config and apply it
*
@@ -90,20 +68,6 @@
Schema::defaultStringLength(191);
- // Log SQL queries in debug mode
- if (\config('app.debug')) {
- DB::listen(function ($query) {
- \Log::debug(
- sprintf(
- '[SQL] %s [%s]: %.4f sec.',
- $query->sql,
- self::serializeSQLBindings($query->bindings, $query->sql),
- $query->time / 1000
- )
- );
- });
- }
-
// Register some template helpers
Blade::directive(
'theme_asset',
diff --git a/src/app/Providers/EventServiceProvider.php b/src/app/Providers/EventServiceProvider.php
--- a/src/app/Providers/EventServiceProvider.php
+++ b/src/app/Providers/EventServiceProvider.php
@@ -2,9 +2,6 @@
namespace App\Providers;
-use Illuminate\Support\Facades\Event;
-use Illuminate\Auth\Events\Registered;
-use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
@@ -15,9 +12,15 @@
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
- Registered::class => [
- SendEmailVerificationNotification::class,
- ],
+ ];
+
+ /**
+ * The subscriber classes to register.
+ *
+ * @var array
+ */
+ protected $subscribe = [
+ \App\Listeners\SqlDebug::class,
];
/**
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Apr 6, 7:45 AM (11 h, 6 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18822832
Default Alt Text
D4576.1775461511.diff (5 KB)
Attached To
Mode
D4576: Better approach to log SQL queries
Attached
Detach File
Event Timeline