public function testGetMethodsForCall_SpecificPlaceholdersHavePriorityOverPotentialFrontmatter() { // Frontmatter must not accidentally replace a @toc@ call even though it *theoretically* would be frontmatter. $collection = new PlaceholderCollection([new TOC(), new Frontmatter()]); $document = new Document("---\ntoc: foo\n---\n@toc@"); $call = Call::extractFirstCall($document, Placeholder::PRIORITY_FIRST); $methods = $collection->getMethodsForCall($call); $this->assertCount(0, $methods); $tocCall = Call::extractFirstCall($document, Placeholder::PRIORITY_LAST); $methods = $collection->getMethodsForCall($tocCall); $this->assertCount(1, $methods); $this->assertSame(TOC::class, get_class($methods[0]->getPlaceholder())); }
/** * applyPlaceholdersAtPriority by calling only the placeholders that are relevant at a certain priority. * * @param Document $document The document to modify. * @param int $priority The priority stage to consider. * Placeholders are only called when they've * listed that priority. See * `Placeholder::getCallPriorities()` * @param \nochso\WriteMe\Placeholder\PlaceholderCollection $placeholders */ private function applyPlaceholdersAtPriority(Document $document, $priority, PlaceholderCollection $placeholders) { $offset = 0; $call = Call::extractFirstCall($document, $priority, $offset); while ($call !== null) { $methods = $placeholders->getMethodsForCall($call); $isReplaced = false; foreach ($methods as $method) { $method->call($call); if ($call->isReplaced()) { $isReplaced = true; break; } } if ($isReplaced) { // Anything could have changed in content. Start from the beginning. $offset = 0; } else { // The call was skipped by all placeholders. Ignore it at this priority. $offset = $call->getEndPositionOfRawCall(); } $call = Call::extractFirstCall($document, $priority, $offset); } }