handler = $handler; $this->callback = $callback; $this->verbosity = $verbosity; } /** * Send a message * * @param string $msg */ private function trace( $msg ) { ( $this->callback )( "[Tree] $msg" ); } /** * Send a message for an event * * @param string $funcName * @param array $args */ private function traceEvent( $funcName, $args ) { $this->trace( TraceFormatter::$funcName( ...$args ) ); } private function handleMutation( string $funcName, array $args ) { $this->traceEvent( $funcName, $args ); $this->before(); $this->handler->$funcName( ...$args ); $this->after(); } private function handleSimple( string $funcName, array $args ) { $this->traceEvent( $funcName, $args ); $this->handler->$funcName( ...$args ); } /** * A helper called before the underlying handler is called. */ private function before() { if ( $this->verbosity > 0 ) { // @phan-suppress-next-line PhanUndeclaredMethod $this->trace( "Before: " . $this->handler->dump() . "\n" ); } } /** * A helper called after the underlying handler is called. */ private function after() { if ( $this->verbosity > 0 ) { // @phan-suppress-next-line PhanUndeclaredMethod $this->trace( "After: " . $this->handler->dump() . "\n" ); } } /** * @inheritDoc */ public function startDocument( $fns, $fn ) { $this->handleSimple( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function endDocument( $pos ) { $this->handleSimple( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function characters( $preposition, $refNode, $text, $start, $length, $sourceStart, $sourceLength ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function insertElement( $preposition, $refNode, Element $element, $void, $sourceStart, $sourceLength ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function endTag( Element $element, $sourceStart, $sourceLength ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function comment( $preposition, $refNode, $text, $sourceStart, $sourceLength ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function error( $text, $pos ) { $this->handleSimple( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function removeNode( Element $element, $sourceStart ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } /** * @inheritDoc */ public function reparentChildren( Element $element, Element $newParent, $sourceStart ) { $this->handleMutation( __FUNCTION__, func_get_args() ); } }