Skip to content

Commit

Permalink
Add support for change log generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 3, 2020
1 parent 0bb1c96 commit 6fb9b70
Showing 1 changed file with 104 additions and 7 deletions.
111 changes: 104 additions & 7 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,9 @@ public function getMethodSynopsisElement(array $aliasMap, DOMDocument $doc): ?DO
return null;
}

$methodSynopsis = $doc->createElement('methodsynopsis');
$methodSynopsis = $doc->createElement($this->name->isConstructor() ? "constructorsynopsis" : "methodsynopsis");

if ($this->alias || isset($aliasMap[$this->name->__toString()])) {
if ($this->alias || isset($aliasMap[$this->name->__toString()]) || $this->name->isConstructor()) {
$role = $doc->createAttribute("role");
$role->value = $this->isMethod() ? "oop" : "procedural";
$methodSynopsis->appendChild($role);
Expand Down Expand Up @@ -1558,7 +1558,7 @@ function generateMethodSynopses(array $funcMap, array $aliasMap): array {
* @param FuncInfo[] $aliasMap
* @return array<string, string>
*/
function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $aliasMap): array {
function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $aliasMap, Context $context): array {
$methodSynopses = [];

$it = new RecursiveIteratorIterator(
Expand All @@ -1577,7 +1577,7 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
continue;
}

if (strpos($xml, "<methodsynopsis") === false) {
if (strpos($xml, "<methodsynopsis") === false && strpos($xml, "<constructorsynopsis") === false) {
continue;
}

Expand All @@ -1596,9 +1596,15 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
$docComparator->preserveWhiteSpace = false;
$docComparator->formatOutput = true;

$methodSynopsisList = $doc->getElementsByTagName("methodsynopsis");
$methodSynopsisElements = [];
foreach ($doc->getElementsByTagName("constructorsynopsis") as $element) {
$methodSynopsisElements[] = $element;
}
foreach ($doc->getElementsByTagName("methodsynopsis") as $element) {
$methodSynopsisElements[] = $element;
}

foreach ($methodSynopsisList as $methodSynopsis) {
foreach ($methodSynopsisElements as $methodSynopsis) {
if (!$methodSynopsis instanceof DOMElement) {
continue;
}
Expand All @@ -1619,6 +1625,8 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
continue;
}

// Retrieve current signature

$params = [];
$list = $methodSynopsis->getElementsByTagName("methodparam");
foreach ($list as $i => $item) {
Expand Down Expand Up @@ -1646,6 +1654,8 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
$params[$paramName] = ["index" => $i, "type" => $paramTypes];
}

// Check if there is any change - short circuit if there is not any.

$xml1 = $doc->saveXML($methodSynopsis);
$xml1 = preg_replace("/&([A-Za-z0-9._{}%-]+?;)/", "REPLACED-ENTITY-$1", $xml1);
$docComparator->loadXML($xml1);
Expand All @@ -1662,6 +1672,8 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
continue;
}
// Update parameter references
$paramList = $doc->getElementsByTagName("parameter");
/** @var DOMElement $paramElement */
foreach ($paramList as $paramElement) {
Expand All @@ -1682,6 +1694,91 @@ function replaceMethodSynopses(string $targetDirectory, array $funcMap, array $a
$paramElement->textContent = $funcInfo->args[$index]->name;
}

// Add or update change log

$changelog = null;
$refsects = [];
/** @var DOMElement $refsect */
foreach ($doc->getElementsByTagName("refsect1") as $refsect) {
$role = $refsect->getAttribute("role");

if ($role === "changelog") {
$changelog = $refsect;
break;
}

$refsects[$role] = $refsect;
}

$change = "The " . ($funcInfo->isMethod() ? "method" : "function") . " signature has changed.";

if ($changelog === null) {
$changelog = $doc->createElement("refsect1");
$changelog->setAttribute("role", "changelog");

$fragment = $doc->createDocumentFragment();
$fragment->appendXML(
<<<XML
REPLACED-ENTITY-reftitle.changelog;
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>REPLACED-ENTITY-Version;</entry>
<entry>REPLACED-ENTITY-Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.0.0</entry>
<entry>
$change
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
XML . " "
);
$changelog->appendChild($fragment);

if (isset($refsects["examples"])) {
$doc->documentElement->insertBefore($changelog, $refsects["examples"]);
$doc->documentElement->insertBefore(new DOMText("\n\n "), $refsects["examples"]);
} else {
$doc->documentElement->appendChild(new DOMText("\n "));
$doc->documentElement->appendChild($changelog);
$doc->documentElement->appendChild(new DOMText("\n"));
}
} else {
$tbodies = $changelog->getElementsByTagName("tbody");
/** @var DOMElement $tbody */
$tbody = $tbodies->item(0);
if ($tbody) {
$fragment = $doc->createDocumentFragment();
$fragment->appendXML(
<<<XML
<row>
<entry>8.0.0</entry>
<entry>
$change
</entry>
</row>

XML . " "
);
if ($tbody->getElementsByTagName("row")->count() > 0) {
$tbody->insertBefore($fragment, $tbody->getElementsByTagName("row")->item(0));
} else {
$tbody->insertBefore($fragment, null);
}
}
}

// Return the updated XML

$replacedXml = $doc->saveXML();

$replacedXml = preg_replace("/REPLACED-ENTITY-([A-Za-z0-9._{}%-]+?;)/", "&$1", $replacedXml);
Expand Down Expand Up @@ -1911,7 +2008,7 @@ function(?ArgInfo $aliasArg, ?ArgInfo $aliasedArg) use ($aliasFunc, $aliasedFunc
}

if ($replaceMethodSynopses) {
$methodSynopses = replaceMethodSynopses($targetMethodSynopses, $funcMap, $aliasMap);
$methodSynopses = replaceMethodSynopses($targetMethodSynopses, $funcMap, $aliasMap, $context);

foreach ($methodSynopses as $filename => $content) {
if (file_put_contents($filename, $content)) {
Expand Down

0 comments on commit 6fb9b70

Please sign in to comment.