|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Recoded\WordPressBlockParser; |
| 4 | + |
| 5 | +use Generator; |
| 6 | +use IteratorAggregate; |
| 7 | +use Recoded\WordPressBlockParser\Value\BlockClosing; |
| 8 | +use Recoded\WordPressBlockParser\Value\BlockOpening; |
| 9 | +use Recoded\WordPressBlockParser\Value\SelfClosingBlock; |
| 10 | + |
| 11 | +/** |
| 12 | + * @implements \IteratorAggregate<int, \Recoded\WordPressBlockParser\Value\Token> |
| 13 | + */ |
| 14 | +final class Tokenizer implements IteratorAggregate |
| 15 | +{ |
| 16 | + public const PATTERN = '/<!--\s+(?P<closer>\/)?wp:(?P<namespace>[a-z][a-z0-9_-]*\/)?(?P<name>[a-z][a-z0-9_-]*)\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P<void>\/)?-->/s'; |
| 17 | + |
| 18 | + private int $currentOffset = 0; |
| 19 | + |
| 20 | + /** |
| 21 | + * Create a new Tokenizer instance. |
| 22 | + * |
| 23 | + * @param string $content |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + public function __construct( |
| 27 | + public readonly string $content, |
| 28 | + ) { |
| 29 | + // |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a new tokenizer from content string. |
| 34 | + * |
| 35 | + * @param string $content |
| 36 | + * @return self |
| 37 | + */ |
| 38 | + public static function create(string $content): self |
| 39 | + { |
| 40 | + return new self($content); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Iterate over the tokens. |
| 45 | + * |
| 46 | + * @return \Generator |
| 47 | + */ |
| 48 | + public function getIterator(): Generator |
| 49 | + { |
| 50 | + do { |
| 51 | + $result = preg_match( |
| 52 | + '/<!--\s+(?P<closer>\/)?wp:(?P<namespace>[a-z][a-z0-9_-]*\/)?(?P<name>[a-z][a-z0-9_-]*)\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+\/?-->).)*+)?}\s+)?(?P<void>\/)?-->/s', |
| 53 | + $this->content, |
| 54 | + $matches, |
| 55 | + PREG_OFFSET_CAPTURE, |
| 56 | + $this->currentOffset |
| 57 | + ); |
| 58 | + |
| 59 | + if ($result === false || $result === 0) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + [$match, $offset] = $matches[0]; |
| 64 | + |
| 65 | + $length = strlen($match); |
| 66 | + $hasAttributes = isset($matches['attrs']) && $matches['attrs'][1] !== -1; |
| 67 | + /** @var array<string, mixed> $attributes */ |
| 68 | + $attributes = $hasAttributes |
| 69 | + ? json_decode($matches['attrs'][0], associative: true) |
| 70 | + : []; |
| 71 | + $namespace = (isset($matches['namespace']) && -1 !== $matches['namespace'][1]) |
| 72 | + ? $matches['namespace'][0] |
| 73 | + : 'core/'; |
| 74 | + $name = $matches['name'][0]; |
| 75 | + |
| 76 | + if (isset($matches['void']) && $matches['void'][1] !== -1) { |
| 77 | + yield new SelfClosingBlock( |
| 78 | + namespace: $namespace, |
| 79 | + name: $name, |
| 80 | + attributes: $attributes, |
| 81 | + startsAt: $offset, |
| 82 | + length: $length, |
| 83 | + ); |
| 84 | + } elseif (isset($matches['closer']) && $matches['closer'][1] !== -1) { |
| 85 | + yield new BlockClosing( |
| 86 | + namespace: $namespace, |
| 87 | + name: $name, |
| 88 | + startsAt: $offset, |
| 89 | + length: $length, |
| 90 | + ); |
| 91 | + } else { |
| 92 | + yield new BlockOpening( |
| 93 | + namespace: $namespace, |
| 94 | + name: $name, |
| 95 | + attributes: $attributes, |
| 96 | + startsAt: $offset, |
| 97 | + length: $length, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + $this->currentOffset = $offset + $length; |
| 102 | + } while ($result !== false && $result !== 0); |
| 103 | + } |
| 104 | +} |
0 commit comments