Skip to content

Commit

Permalink
[jan] Add Horde_Pear_Package_Xml#addAuthor() and addDependency().
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Nov 7, 2017
1 parent 6817a4d commit 37e717e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/Horde/Pear/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
identifier: LGPL-2.1
uri: http://www.horde.org/licenses/lgpl21
notes: |
[jan] Add Horde_Pear_Package_Xml#addAuthor() and addDependency().
[jan] Add Horde_Pear_Package_Xml#addVersion().
[jan] Add Horde_Pear_Package_Xml#setNotes().
[jan] Set replacement tasks for binaries and translation handlers.
Expand Down
69 changes: 61 additions & 8 deletions lib/Horde/Pear/Package/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,15 +469,14 @@ public function setNotes(array $notes)
{
$thisVersion = $this->getVersion();
foreach ($notes as $version => $info) {
$new_notes = "\n* "
. implode("\n* ", explode("\n", trim($info['notes'])))
. "\n ";
$new_notes = implode("\n* ", explode("\n", trim($info['notes'])));
if ($new_notes) {
$new_notes = "\n* " . $new_notes;
}
$new_notes .= "\n ";
if ($version == $thisVersion) {
$this->replaceTextNode('/p:package/p:notes', $new_notes);
}
if (version_compare($version, $thisVersion, '>')) {
continue;
}
if ($node = $this->_fetchRelease($version)) {
$this->replaceTextNodeRelativeTo(
'./p:notes', $node, $new_notes . ' '
Expand Down Expand Up @@ -560,6 +559,59 @@ public function setState($rel_state = null, $api_state = null)
}
}

/**
* Adds a new author to the package.xml
*
* @param string $name A name.
* @param string $user A user name.
* @param string $email An email address.
* @param boolean $active Still active?
*/
public function addAuthor($name, $user, $email, $active)
{
$date = $this->findNode('/p:package/p:date');
$lead = $this->_xml->createElementNS(self::XMLNAMESPACE, 'lead');
$this->_appendChild($lead, 'name', $name, "\n ");
$this->_appendChild($lead, 'user', $user, "\n ");
$this->_appendChild($lead, 'email', $email, "\n ");
$this->_appendChild($lead, 'active', $active ? 'yes' : 'no', "\n ");
$this->_insertWhitespace($lead, "\n ");
$date->parentNode->insertBefore($lead, $date);
$date->parentNode->insertBefore(
$this->_xml->createTextNode("\n "),
$date
);
}

/**
* Adds a new dependency to the package.xml
*
* @param string $required A dependency requirement, either 'required' or
* 'optional'.
* @param string $type A dependency type, either 'package' or
* 'extension'.
* @param array $contraints A list of dependency constraints, like 'name'
* or 'min'.
*/
public function addDependency($required, $type, $constraints)
{
$deps = $this->findNode('/p:package/p:dependencies/p:' . $required);
if (!$deps) {
$deps = $this->_xml->createElementNS(self::XMLNAMESPACE, $required);
$dependencies = $this->findNode('/p:package/p:dependencies');
$this->_insertWhiteSpace($dependencies, ' ');
$this->_insertWhiteSpace($deps, "\n ");
$dependencies->appendChild($deps);
$this->_insertWhiteSpace($dependencies, "\n ");
}
$package = $this->_appendChild($deps, $type, '', ' ');
foreach ($constraints as $constraint => $version) {
$this->_appendChild($package, $constraint, $version, "\n ");
}
$this->_insertWhiteSpace($package, "\n ");
$this->_insertWhiteSpace($deps, "\n ");
}

/**
* Add the next version to the package.xml
*
Expand Down Expand Up @@ -872,7 +924,7 @@ public function getNodeTextRelativeTo($path, DOMNode $context)
public function replaceTextNode($path, $value)
{
if ($node = $this->findNode($path)) {
$this->_xml->documentElement->replaceChild(
$node->parentNode->replaceChild(
$this->_replacementNode($node, $value), $node
);
}
Expand Down Expand Up @@ -926,7 +978,7 @@ private function _replacementNode($old_node, $value)
* @param string $value The text content of the new node.
* @param string $ws Additional white space that should be inserted.
*
* @return NULL
* @return DOMNode The appended child.
*/
private function _appendChild($parent, $name, $value, $ws = '')
{
Expand All @@ -937,6 +989,7 @@ private function _appendChild($parent, $name, $value, $ws = '')
$text = $this->_xml->createTextNode($value);
$new_node->appendChild($text);
$parent->appendChild($new_node);
return $new_node;
}

/**
Expand Down

0 comments on commit 37e717e

Please sign in to comment.