| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
use Wikimedia\RemexHtml\Tokenizer\TokenHandler; |
| 7 |
use Wikimedia\RemexHtml\Tokenizer\Tokenizer; |
| 8 |
|
| 9 |
/** |
| 10 |
* This is a debugging helper class which calls a callback function with a |
| 11 |
* descriptive message each time a token event comes from the Tokenizer. The |
| 12 |
* messages include information about the current state and transitions of the |
| 13 |
* Dispatcher which is the next stage in the pipeline. |
| 14 |
*/ |
| 15 |
class DispatchTracer implements TokenHandler { |
| 16 |
/** @var string */ |
| 17 |
private $input; |
| 18 |
private Dispatcher $dispatcher; |
| 19 |
/** @var callable */ |
| 20 |
private $callback; |
| 21 |
|
| 22 |
public function __construct( string $input, Dispatcher $dispatcher, callable $callback ) { |
| 23 |
$this->input = $input; |
| 24 |
$this->dispatcher = $dispatcher; |
| 25 |
$this->callback = $callback; |
| 26 |
} |
| 27 |
|
| 28 |
private function trace( string $msg ) { |
| 29 |
( $this->callback )( "[Dispatch] $msg" ); |
| 30 |
} |
| 31 |
|
| 32 |
private function excerpt( string $text ): string { |
| 33 |
if ( strlen( $text ) > 20 ) { |
| 34 |
$text = substr( $text, 0, 20 ) . '...'; |
| 35 |
} |
| 36 |
return str_replace( "\n", "\\n", $text ); |
| 37 |
} |
| 38 |
|
| 39 |
private function wrap( string $funcName, int $sourceStart, int $sourceLength, array $args ) { |
| 40 |
$prevHandler = $this->getHandlerName(); |
| 41 |
$excerpt = $this->excerpt( substr( $this->input, $sourceStart, $sourceLength ) ); |
| 42 |
$msg = "$funcName $prevHandler \"$excerpt\""; |
| 43 |
$this->trace( $msg ); |
| 44 |
$this->dispatcher->$funcName( ...$args ); |
| 45 |
$handler = $this->getHandlerName(); |
| 46 |
if ( $prevHandler !== $handler ) { |
| 47 |
$this->trace( "$prevHandler -> $handler" ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
private function getHandlerName(): string { |
| 52 |
$handler = $this->dispatcher->getHandler(); |
| 53 |
$name = $handler ? get_class( $handler ) : 'NULL'; |
| 54 |
$slashPos = strrpos( $name, '\\' ); |
| 55 |
if ( $slashPos === false ) { |
| 56 |
return $name; |
| 57 |
} else { |
| 58 |
return substr( $name, $slashPos + 1 ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** @inheritDoc */ |
| 63 |
public function startDocument( Tokenizer $tokenizer, $ns, $name ) { |
| 64 |
$prevHandler = $this->getHandlerName(); |
| 65 |
$nsMsg = $ns === null ? 'NULL' : $ns; |
| 66 |
$nameMsg = $name === null ? 'NULL' : $name; |
| 67 |
$this->trace( "startDocument $prevHandler $nsMsg $nameMsg" ); |
| 68 |
$this->dispatcher->startDocument( $tokenizer, $ns, $name ); |
| 69 |
$handler = $this->getHandlerName(); |
| 70 |
if ( $prevHandler !== $handler ) { |
| 71 |
$this->trace( "$prevHandler -> $handler" ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** @inheritDoc */ |
| 76 |
public function endDocument( $pos ) { |
| 77 |
$this->wrap( __FUNCTION__, $pos, 0, func_get_args() ); |
| 78 |
} |
| 79 |
|
| 80 |
/** @inheritDoc */ |
| 81 |
public function error( $text, $pos ) { |
| 82 |
$handler = $this->getHandlerName(); |
| 83 |
$this->trace( "error $handler \"$text\"" ); |
| 84 |
$this->dispatcher->error( $text, $pos ); |
| 85 |
} |
| 86 |
|
| 87 |
/** @inheritDoc */ |
| 88 |
public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 89 |
$this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 90 |
} |
| 91 |
|
| 92 |
/** @inheritDoc */ |
| 93 |
public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 94 |
$this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 95 |
} |
| 96 |
|
| 97 |
/** @inheritDoc */ |
| 98 |
public function endTag( $name, $sourceStart, $sourceLength ) { |
| 99 |
$this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 100 |
} |
| 101 |
|
| 102 |
/** @inheritDoc */ |
| 103 |
public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 104 |
$this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 105 |
} |
| 106 |
|
| 107 |
/** @inheritDoc */ |
| 108 |
public function comment( $text, $sourceStart, $sourceLength ) { |
| 109 |
$this->wrap( __FUNCTION__, $sourceStart, $sourceLength, func_get_args() ); |
| 110 |
} |
| 111 |
} |