| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\Serializer; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 6 |
use Wikimedia\RemexHtml\TreeBuilder\Element; |
| 7 |
use Wikimedia\RemexHtml\TreeBuilder\TraceFormatter; |
| 8 |
|
| 9 |
class SerializerWithTracer extends Serializer { |
| 10 |
/** @var callable|null */ |
| 11 |
private $traceCallback; |
| 12 |
/** @var int */ |
| 13 |
private $verbosity; |
| 14 |
|
| 15 |
/** |
| 16 |
* @param Formatter $formatter |
| 17 |
* @param callable|null $errorCallback |
| 18 |
* @param callable|null $traceCallback |
| 19 |
* @param int $verbosity |
| 20 |
*/ |
| 21 |
public function __construct( Formatter $formatter, $errorCallback = null, $traceCallback = null, |
| 22 |
$verbosity = 0 |
| 23 |
) { |
| 24 |
$this->traceCallback = $traceCallback; |
| 25 |
$this->verbosity = $verbosity; |
| 26 |
parent::__construct( $formatter, $errorCallback ); |
| 27 |
} |
| 28 |
|
| 29 |
private function handle( string $funcName, array $args ) { |
| 30 |
$this->trace( TraceFormatter::$funcName( ...$args ) ); |
| 31 |
parent::$funcName( ...$args ); |
| 32 |
if ( $this->verbosity > 0 && $funcName !== 'endDocument' ) { |
| 33 |
$this->trace( "Dump after $funcName: " . $this->dump() ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
private function trace( string $msg ) { |
| 38 |
( $this->traceCallback )( "[Serializer] $msg" ); |
| 39 |
} |
| 40 |
|
| 41 |
/** @inheritDoc */ |
| 42 |
public function startDocument( $fragmentNamespace, $fragmentName ) { |
| 43 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 44 |
} |
| 45 |
|
| 46 |
/** @inheritDoc */ |
| 47 |
public function endDocument( $pos ) { |
| 48 |
if ( count( $this->nodes ) ) { |
| 49 |
$nodeTags = ''; |
| 50 |
foreach ( $this->nodes as $node ) { |
| 51 |
if ( $nodeTags !== '' ) { |
| 52 |
$nodeTags .= ', '; |
| 53 |
} |
| 54 |
$nodeTags .= $node->getDebugTag(); |
| 55 |
} |
| 56 |
$this->trace( "endDocument: unclosed elements: $nodeTags" ); |
| 57 |
} else { |
| 58 |
$this->trace( "endDocument: no unclosed elements" ); |
| 59 |
} |
| 60 |
|
| 61 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 62 |
} |
| 63 |
|
| 64 |
/** @inheritDoc */ |
| 65 |
public function characters( $preposition, $refElement, $text, $start, $length, |
| 66 |
$sourceStart, $sourceLength |
| 67 |
) { |
| 68 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 69 |
} |
| 70 |
|
| 71 |
/** @inheritDoc */ |
| 72 |
public function insertElement( $preposition, $refElement, Element $element, $void, |
| 73 |
$sourceStart, $sourceLength |
| 74 |
) { |
| 75 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 76 |
} |
| 77 |
|
| 78 |
/** @inheritDoc */ |
| 79 |
public function endTag( Element $element, $sourceStart, $sourceLength ) { |
| 80 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 81 |
} |
| 82 |
|
| 83 |
/** @inheritDoc */ |
| 84 |
public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 85 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 86 |
} |
| 87 |
|
| 88 |
/** @inheritDoc */ |
| 89 |
public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) { |
| 90 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 91 |
} |
| 92 |
|
| 93 |
/** @inheritDoc */ |
| 94 |
public function error( $text, $pos ) { |
| 95 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 96 |
} |
| 97 |
|
| 98 |
/** @inheritDoc */ |
| 99 |
public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
| 100 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 101 |
} |
| 102 |
|
| 103 |
/** @inheritDoc */ |
| 104 |
public function removeNode( Element $element, $sourceStart ) { |
| 105 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 106 |
} |
| 107 |
|
| 108 |
/** @inheritDoc */ |
| 109 |
public function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
| 110 |
$this->handle( __FUNCTION__, func_get_args() ); |
| 111 |
} |
| 112 |
} |