Skip to content

Commit

Permalink
[jan] Set replacement tasks for binaries and translation handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Oct 17, 2017
1 parent 29005d2 commit 9508996
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 50 deletions.
7 changes: 4 additions & 3 deletions lib/Horde/Pear/Package/Contents/List.php
Expand Up @@ -86,15 +86,16 @@ public function getContents()
);
$elements = array();
foreach ($list as $element) {
if ($this->_include->isIncluded($element)
&& !$this->_ignore->isIgnored($element)) {
if ($this->_include->isIncluded($element) &&
!$this->_ignore->isIgnored($element)) {
$file = substr($element->getPathname(), strlen($this->_root));
$elements[$file] = array(
'role' => $this->_role->getRole($file),
'replace' => $this->_role->getReplace($file, $this->_root),
'as' => $this->_install_as->getInstallAs($file, 'Horde_' . basename($this->_root))
);
}
}
return $elements;
}
}
}
12 changes: 11 additions & 1 deletion lib/Horde/Pear/Package/Contents/Role.php
Expand Up @@ -31,4 +31,14 @@ interface Horde_Pear_Package_Contents_Role
* @return string The role of the file.
*/
public function getRole($file);
}

/**
* Tell which replacement tasks are required for the specified file.
*
* @param string $file The file path inside the component.
* @param string $root The filesystem root of the component.
*
* @return array A list of <tasks:replace> attribute hashes.
*/
public function getReplace($file, $root);
}
34 changes: 34 additions & 0 deletions lib/Horde/Pear/Package/Contents/Role/HordeApplication.php
Expand Up @@ -52,4 +52,38 @@ public function getRole($file)
return 'horde';
}
}

/**
* Tell which replacement tasks are required for the specified file.
*
* @param string $file The file path inside the component.
* @param string $root The filesystem root of the component.
*
* @return array A list of <tasks:replace> attribute hashes.
*/
public function getReplace($file, $root)
{
$elements = explode('/', substr($file, 1));
$basedir = array_shift($elements);
switch ($basedir) {
case 'bin':
if (strpos(file_get_contents($root . '/' . $file), '#!/usr/bin/env php') === 0) {
return array(array(
'from' => '/usr/bin/env php',
'to' => 'php_bin',
'type' => 'pear-config'
));
}
break;
}
$file = array_pop($elements);
if ($file == 'Translation.php') {
return array(array(
'from' => '@data_dir@',
'to' => 'data_dir',
'type' => 'pear-config'
));
}
return array();
}
}
34 changes: 34 additions & 0 deletions lib/Horde/Pear/Package/Contents/Role/HordeComponent.php
Expand Up @@ -58,4 +58,38 @@ public function getRole($file)
return 'php';
}
}

/**
* Tell which replacement tasks are required for the specified file.
*
* @param string $file The file path inside the component.
* @param string $root The filesystem root of the component.
*
* @return array A list of <tasks:replace> attribute hashes.
*/
public function getReplace($file, $root)
{
$elements = explode('/', substr($file, 1));
$basedir = array_shift($elements);
switch ($basedir) {
case 'bin':
if (strpos(file_get_contents($root . '/' . $file), '#!/usr/bin/env php') === 0) {
return array(array(
'from' => '/usr/bin/env php',
'to' => 'php_bin',
'type' => 'pear-config'
));
}
break;
}
$file = array_pop($elements);
if ($file == 'Translation.php') {
return array(array(
'from' => '@data_dir@',
'to' => 'data_dir',
'type' => 'pear-config'
));
}
return array();
}
}
18 changes: 15 additions & 3 deletions lib/Horde/Pear/Package/Xml.php
Expand Up @@ -25,6 +25,9 @@ class Horde_Pear_Package_Xml
/** The package.xml namespace */
const XMLNAMESPACE = 'http://pear.php.net/dtd/package-2.0';

/** The package.xml tasks namespace */
const XMLTASKSNAMESPACE = 'http://pear.php.net/dtd/tasks-1.0';

/**
* The parsed XML.
*
Expand Down Expand Up @@ -916,7 +919,13 @@ public function append($elements, $parent)
if (is_string($element)) {
$element = $this->createText($element);
} else if (is_array($element)) {
$node = $element = $this->createNode($key, $element);
if (isset($element['namespace'])) {
$ns = $element['namespace'];
unset($element['namespace']);
} else {
$ns = self::XMLNAMESPACE;
}
$node = $element = $this->createNode($key, $element, $ns);
}
$parent->appendChild($element);
}
Expand All @@ -933,9 +942,12 @@ public function createComment($comment)
return $this->_xml->createComment($comment);
}

public function createNode($name, $attributes = array())
public function createNode($name, $attributes = array(), $ns = null)
{
$node = $this->_xml->createElementNS(self::XMLNAMESPACE, $name);
if (!$ns) {
$ns = self::XMLNAMESPACE;
}
$node = $this->_xml->createElementNS($ns, $name);
foreach ($attributes as $key => $value) {
$node->setAttribute($key, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Horde/Pear/Package/Xml/Contents.php
Expand Up @@ -203,4 +203,4 @@ public function delete($file)
$this->_filelist->removeChild($this->_install_list[$file]);
}
}
}
}
21 changes: 5 additions & 16 deletions lib/Horde/Pear/Package/Xml/Directory.php
Expand Up @@ -72,7 +72,7 @@ public function __construct(Horde_Pear_Package_Xml_Element_Directory $dir,
/**
* Return the directory node.
*
* @return DOMNode The directory node.
* @return Horde_Pear_Package_Xml_Element_Directory The directory node.
*/
public function getDirectory()
{
Expand Down Expand Up @@ -156,8 +156,6 @@ private function _prependDirectory($path)
*
* @param string $file The file name.
* @param array $params Additional file parameters.
*
* @return NULL
*/
public function addFile($file, $params)
{
Expand All @@ -170,24 +168,21 @@ public function addFile($file, $params)
*
* @param string $file The file name.
* @param array $params Additional file parameters.
*
* @return NULL
*/
private function _addFile($file, $params)
{
$this->_files[basename($file)] = $this->_element->insertFile(
basename($file),
$params['role'],
$this->_getFileInsertionPoint(basename($file))
$this->_getFileInsertionPoint(basename($file)),
isset($params['replace']) ? $params['replace'] : null
);
}

/**
* Delete a file from the list.
*
* @param string $file The file name.
*
* @return NULL
*/
public function deleteFile($file)
{
Expand All @@ -198,8 +193,6 @@ public function deleteFile($file)
* Delete a file from the list.
*
* @param string $file The file name.
*
* @return NULL
*/
private function _deleteFile($file)
{
Expand All @@ -212,8 +205,6 @@ private function _deleteFile($file)
* Delete a subdirectory from the list.
*
* @param string $dir The directory name.
*
* @return NULL
*/
public function deleteSubdirectory($dir)
{
Expand All @@ -223,8 +214,6 @@ public function deleteSubdirectory($dir)

/**
* Prune this directory if it is empty.
*
* @return NULL
*/
private function _prune()
{
Expand All @@ -241,7 +230,7 @@ private function _prune()
*
* @param array $tree The path elements that are required.
*
* @return DOMNode The parent directory for the file.
* @return self The parent directory for the file.
*/
public function getParent($tree)
{
Expand Down Expand Up @@ -322,4 +311,4 @@ private function _getFileInsertionPoint($new)
return null;
}
}
}
}
13 changes: 7 additions & 6 deletions lib/Horde/Pear/Package/Xml/Element/Directory.php
Expand Up @@ -194,15 +194,16 @@ public function getFiles()
* Insert a new file entry into the XML at the given point with the
* specified name and file role.
*
* @param string $name The name.
* @param string $role The role.
* @param DOMNode $point Insertion point.
* @param string $name The name.
* @param string $role The role.
* @param DOMNode $point Insertion point.
* @param array $replace Replacement tasks.
*
* @return Horde_Pear_Package_Xml_Element_File The inserted element.
*/
public function insertFile($name, $role, DOMNode $point = null)
public function insertFile($name, $role, DOMNode $point = null, $replace = array())
{
$element = $this->_xml->createElementFile($name, $this, $role);
$element = $this->_xml->createElementFile($name, $this, $role, $replace);
$element->insert($point);
return $element;
}
Expand Down Expand Up @@ -274,4 +275,4 @@ public function delete()
$this->_xml->removeWhitespace($dir->nextSibling);
$dir->parentNode->removeChild($dir);
}
}
}
37 changes: 30 additions & 7 deletions lib/Horde/Pear/Package/Xml/Element/File.php
Expand Up @@ -57,6 +57,13 @@ class Horde_Pear_Package_Xml_Element_File
*/
private $_role;

/**
* The replacement tasks for this file.
*
* @var array
*/
private $_replace;

/**
* The level in the tree.
*
Expand All @@ -67,17 +74,19 @@ class Horde_Pear_Package_Xml_Element_File
/**
* Constructor.
*
* @param string $name The name of
* the directory.
* @param Horde_Pear_Package_Xml_Element_Directory $parent The parent
* directory.
* @param string $role The file role.
* @param string $name The name of
* the directory.
* @param Horde_Pear_Package_Xml_Element_Directory $parent The parent
* directory.
* @param string $role The file role.
* @param array $replace Replacement tasks.
*/
public function __construct($name, $parent, $role = null)
public function __construct($name, $parent, $role = null, $replace = array())
{
$this->_name = $name;
$this->_role = $role;
$this->_parent = $parent;
$this->_replace = $replace;
$this->_xml = $parent->getDocument();
$this->_level = $parent->getLevel() + 1;
}
Expand Down Expand Up @@ -138,6 +147,20 @@ public function insert(DOMNode $point = null)
$point
)
);

if ($this->_replace) {
foreach ($this->_replace as $replace) {
$replace['namespace'] = Horde_Pear_Package_Xml::XMLTASKSNAMESPACE;
$this->_xml->append(
array(
"\n " . str_repeat(" ", $this->_level),
'tasks:replace' => $replace,
"\n " . str_repeat(" ", $this->_level),
),
$this->_file
);
}
}
}

/**
Expand All @@ -152,4 +175,4 @@ public function delete()
$this->_xml->removeWhitespace($file->previousSibling);
$dir->removeChild($file);
}
}
}
4 changes: 2 additions & 2 deletions package.xml
Expand Up @@ -21,7 +21,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Set replacement tasks for binaries and translation handlers.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -667,7 +667,7 @@
<date>2017-10-16</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Set replacement tasks for binaries and translation handlers.
</notes>
</release>
</changelog>
Expand Down
6 changes: 4 additions & 2 deletions test/Horde/Pear/Unit/Package/Contents/ListTest.php
Expand Up @@ -36,11 +36,13 @@ public function testList()
array(
'/lib/Old.php' => array(
'role' => 'php',
'as' => 'Old.php'
'as' => 'Old.php',
'replace' => array()
),
'/lib/Stays.php' => array(
'role' => 'php',
'as' => 'Stays.php'
'as' => 'Stays.php',
'replace' => array()
),
),
$this->_getList()->getContents()
Expand Down

0 comments on commit 9508996

Please sign in to comment.