Debian Codegraph @

vendor/wikimedia/remex-html/src/TreeBuilder/TreeMutationTracer.php

raw
1 <?php
2
3 namespace Wikimedia\RemexHtml\TreeBuilder;
4
5 use Wikimedia\RemexHtml\Tokenizer\Attributes;
6
7 /**
8 * This is a debugging helper class which calls the supplied callback function
9 * each time there is a TreeHandler event, giving a descriptive message. It
10 * then forwards the event through to the supplied handler.
11 */
12 class TreeMutationTracer implements TreeHandler {
13 private TreeHandler $handler;
14
15 /** @var callable */
16 private $callback;
17
18 /** @var int */
19 private $verbosity;
20
21 /**
22 * Constructor.
23 *
24 * @param TreeHandler $handler The next pipeline stage
25 * @param callable $callback The message output function
26 * @param int $verbosity Set to non-zero to call dump() on the handler
27 * before and after each event.
28 */
29 public function __construct( TreeHandler $handler, callable $callback, $verbosity = 0 ) {
30 $this->handler = $handler;
31 $this->callback = $callback;
32 $this->verbosity = $verbosity;
33 }
34
35 /**
36 * Send a message
37 *
38 * @param string $msg
39 */
40 private function trace( $msg ) {
41 ( $this->callback )( "[Tree] $msg" );
42 }
43
44 /**
45 * Send a message for an event
46 *
47 * @param string $funcName
48 * @param array $args
49 */
50 private function traceEvent( $funcName, $args ) {
51 $this->trace( TraceFormatter::$funcName( ...$args ) );
52 }
53
54 private function handleMutation( string $funcName, array $args ) {
55 $this->traceEvent( $funcName, $args );
56 $this->before();
57 $this->handler->$funcName( ...$args );
58 $this->after();
59 }
60
61 private function handleSimple( string $funcName, array $args ) {
62 $this->traceEvent( $funcName, $args );
63 $this->handler->$funcName( ...$args );
64 }
65
66 /**
67 * A helper called before the underlying handler is called.
68 */
69 private function before() {
70 if ( $this->verbosity > 0 ) {
71 // @phan-suppress-next-line PhanUndeclaredMethod
72 $this->trace( "Before: " . $this->handler->dump() . "\n" );
73 }
74 }
75
76 /**
77 * A helper called after the underlying handler is called.
78 */
79 private function after() {
80 if ( $this->verbosity > 0 ) {
81 // @phan-suppress-next-line PhanUndeclaredMethod
82 $this->trace( "After: " . $this->handler->dump() . "\n" );
83 }
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public function startDocument( $fns, $fn ) {
90 $this->handleSimple( __FUNCTION__, func_get_args() );
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public function endDocument( $pos ) {
97 $this->handleSimple( __FUNCTION__, func_get_args() );
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function characters( $preposition, $refNode, $text, $start, $length,
104 $sourceStart, $sourceLength
105 ) {
106 $this->handleMutation( __FUNCTION__, func_get_args() );
107 }
108
109 /**
110 * @inheritDoc
111 */
112 public function insertElement( $preposition, $refNode, Element $element, $void,
113 $sourceStart, $sourceLength
114 ) {
115 $this->handleMutation( __FUNCTION__, func_get_args() );
116 }
117
118 /**
119 * @inheritDoc
120 */
121 public function endTag( Element $element, $sourceStart, $sourceLength ) {
122 $this->handleMutation( __FUNCTION__, func_get_args() );
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) {
129 $this->handleMutation( __FUNCTION__, func_get_args() );
130 }
131
132 /**
133 * @inheritDoc
134 */
135 public function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) {
136 $this->handleMutation( __FUNCTION__, func_get_args() );
137 }
138
139 /**
140 * @inheritDoc
141 */
142 public function error( $text, $pos ) {
143 $this->handleSimple( __FUNCTION__, func_get_args() );
144 }
145
146 /**
147 * @inheritDoc
148 */
149 public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) {
150 $this->handleMutation( __FUNCTION__, func_get_args() );
151 }
152
153 /**
154 * @inheritDoc
155 */
156 public function removeNode( Element $element, $sourceStart ) {
157 $this->handleMutation( __FUNCTION__, func_get_args() );
158 }
159
160 /**
161 * @inheritDoc
162 */
163 public function reparentChildren( Element $element, Element $newParent, $sourceStart ) {
164 $this->handleMutation( __FUNCTION__, func_get_args() );
165 }
166 }