| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\Tokenizer; |
| 4 |
|
| 5 |
/** |
| 6 |
* This class provides a convenient iterative view of the token stream, |
| 7 |
* implemented as a Generator. It is intended to be used as follows: |
| 8 |
* |
| 9 |
* foreach ( TokenGenerator::generate( $html, [] ) as $token ) { |
| 10 |
* ... |
| 11 |
* } |
| 12 |
* |
| 13 |
* Performance is slightly slower than a plain TokenHandler, probably due to |
| 14 |
* the need to convert event parameters to associative arrays. |
| 15 |
*/ |
| 16 |
class TokenGenerator { |
| 17 |
/** @var TokenGeneratorHandler */ |
| 18 |
protected $handler; |
| 19 |
|
| 20 |
/** @var Tokenizer */ |
| 21 |
protected $tokenizer; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $text |
| 25 |
* @param array $options Options passed through to Tokenizer |
| 26 |
*/ |
| 27 |
protected function __construct( $text, $options ) { |
| 28 |
$this->handler = new TokenGeneratorHandler; |
| 29 |
$this->tokenizer = new Tokenizer( $this->handler, $text, $options ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get a Generator which iterates over all tokens in the supplied HTML |
| 34 |
* |
| 35 |
* @param string $text The HTML |
| 36 |
* @param array $options The Tokenizer options, see Tokenizer::__construct() |
| 37 |
* @return \Generator |
| 38 |
*/ |
| 39 |
public static function generate( $text, $options ) { |
| 40 |
$tg = new self( $text, $options ); |
| 41 |
$tg->tokenizer->beginStepping(); |
| 42 |
while ( $tg->tokenizer->step() ) { |
| 43 |
foreach ( $tg->handler->tokens as $token ) { |
| 44 |
yield $token; |
| 45 |
} |
| 46 |
$tg->handler->tokens = []; |
| 47 |
} |
| 48 |
foreach ( $tg->handler->tokens as $token ) { |
| 49 |
yield $token; |
| 50 |
} |
| 51 |
} |
| 52 |
} |