Skip to content

Commit

Permalink
Repairs events that were not logged
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstandiford committed Dec 1, 2021
1 parent 6a025f7 commit c96507c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 220 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"autoload": {
"psr-4": {"Underpin\\Debug_Bar_Extension\\": "lib/"},
"files": [
"debug-bar.php"
]
Expand Down
75 changes: 19 additions & 56 deletions debug-bar.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,26 @@
<?php

use Underpin\Abstracts\Underpin;
use Underpin\Abstracts\Event_Type;
use Underpin\Loaders\Logger;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

// Add this loader.
add_action( 'underpin/before_setup', function ( $file, $class ) {
add_action( 'plugins_loaded', function () use ( $file, $class ) {
if ( ! defined( 'UNDERPIN_DEBUG_BAR_EXTENSION_ROOT_DIR' ) ) {
define( 'UNDERPIN_DEBUG_BAR_EXTENSION_ROOT_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! class_exists( 'Debug_Bar_Panel' ) ) {
return;
}
require_once( UNDERPIN_DEBUG_BAR_EXTENSION_ROOT_DIR . 'lib/factories/Debug_Bar_Panel_Instance.php' );
require_once( UNDERPIN_DEBUG_BAR_EXTENSION_ROOT_DIR . 'lib/factories/Debug_Bar_Panel_Logger_Instance.php' );
require_once( UNDERPIN_DEBUG_BAR_EXTENSION_ROOT_DIR . 'lib/loaders/Debug_Bars.php' );
\Underpin\underpin()->get( $file, $class )->loaders()->add( 'debug_bars', [
'registry' => '\Underpin_Debug_Bar_Extension\Loaders\Debug_Bars',
] );
} );
}, 3, 2 );

// When a logger instance is added, also add a debug bar instance.
add_action( 'underpin/loader_registered', function ( $key, $value, $loader, $parent_id ) {
add_action( 'plugins_loaded', function () use ( $parent_id, $loader, $key ) {

if ( ! class_exists( 'Debug_Bar_Panel' ) ) {
return;
}

if ( $loader === 'Underpin_Logger\Loaders\Logger' ) {
$name = Underpin::get_by_id( $parent_id )->name;

// Due to an irritating limitation in the debug bar plugin, all class names must be unique, and free of invalid
//characters, including a / character.
// This frustration forces us to use eval, as there is no other way to create a unique class name, without slashes
// Without manually creating new classes every time.
// IF you are reading this, and have an idea on how this can be done without eval, we'd love to hear it.

// Malform the class into an MD5. This keeps anyone from running anything cute.
$class = 'Underpin_Debug_Panel_Instance_' . md5( $key . '_' . $parent_id );
eval( 'class ' . $class . ' extends Underpin_Debug_Bar_Panel_Logger_Instance{}' );

Underpin::get_by_id( $parent_id )->debug_bars()->add( $key, [
'class' => $class,
'args' => [
'title' => "$name ${key}s",
'parent_id' => $parent_id,
'logger_key' => $key,
],
] );

}
} );
}, 10, 4 );

add_action( 'underpin/logger/after_logged_item', function ( \Underpin_Logger\Factories\Log_Item $logged_item, $logger ) {
do_action( 'qm/' . $logger->psr_level, $logged_item->code . ':' . $logged_item->message . "\n" . json_encode( $logged_item->data, JSON_PRETTY_PRINT ) );
}, 10, 2 );
// Attach query monitor to logged events.
Logger::instance()->attach( 'event:logged', new \Underpin\Factories\Observer( 'query_monitor', [
'update' => function ( Logger $logger, \Underpin\Factories\Simple_Storage $storage ) {
/* @var \Underpin\Factories\Log_Item $event */
$event = $storage->event;
/* @var Event_Type $event_type */
$event_type = $storage->event_type;

$data = array_merge( [
'volume' => $event_type->volume,
'group' => $event_type->group,
'type' => $event_type->type,
], $event->data );

do_action( 'qm/' . $event_type->psr_level, $event->code . ':' . $event->message . "\n" . json_encode( $data, JSON_PRETTY_PRINT ) );
},
] ) );
27 changes: 0 additions & 27 deletions lib/factories/Debug_Bar_Panel_Instance.php

This file was deleted.

54 changes: 0 additions & 54 deletions lib/factories/Debug_Bar_Panel_Logger_Instance.php

This file was deleted.

60 changes: 0 additions & 60 deletions lib/loaders/Debug_Bars.php

This file was deleted.

23 changes: 0 additions & 23 deletions templates/events/wrapper.php

This file was deleted.

0 comments on commit c96507c

Please sign in to comment.