Add PEAR package extractor that honours package.xml formats

main
Alexey Prilipko 12 years ago
parent bc7e983b1e
commit 01a49ea49e

@ -0,0 +1,192 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Downloader;
use Composer\Util\Filesystem;
/**
* Extractor for pear packages.
*
* Composer cannot rely on tar files structure when place it inside package target dir. Correct source files
* disposition must be read from package.xml
* This extract pear package source files to target dir.
*
* @author Alexey Prilipko <palex@farpost.com>
*/
class PearPackageExtractor
{
/** @var Filesystem */
private $filesystem;
private $file;
public function __construct($file)
{
if (!is_file($file)) {
throw new \UnexpectedValueException('PEAR package file is not found at '.$file);
}
$this->file = $file;
}
/**
* Installs PEAR source files according to package.xml definitions and removes extracted files
*
* @param $file string path to downloaded PEAR archive file
* @param $target string target install location. all source installation would be performed relative to target path.
* @param $role string type of files to install. default role for PEAR source files are 'php'.
*
* @throws \RuntimeException
*/
public function extractTo($target, $role = 'php')
{
$this->filesystem = new Filesystem();
$extractionPath = $target.'/tarball';
try {
$archive = new \PharData($this->file);
$archive->extractTo($extractionPath, null, true);
if (!is_file($this->combine($extractionPath, '/package.xml'))) {
throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.');
}
$fileCopyActions = $this->buildCopyActions($extractionPath, $role);
$this->copyFiles($fileCopyActions, $extractionPath, $target);
$this->filesystem->removeDirectory($extractionPath);
} catch (\Exception $exception) {
throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception);
}
}
/**
* Perform copy actions on files
*
* @param $files array array('from', 'to') with relative paths
* @param $source string path to source dir.
* @param $target string path to destination dir
*/
private function copyFiles($files, $source, $target)
{
foreach ($files as $file) {
$from = $this->combine($source, $file['from']);
$to = $this->combine($target, $file['to']);
$this->copyFile($from, $to);
}
}
private function copyFile($from, $to)
{
if (!is_file($from)) {
throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.');
}
$this->filesystem->ensureDirectoryExists(dirname($to));
if (!copy($from, $to)) {
throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to));
}
}
/**
* Builds list of copy and list of remove actions that would transform extracted PEAR tarball into installed package.
*
* @param $source string path to extracted files.
* @param $role string package file types to extract.
* @return array array of 'source' => 'target', where source is location of file in the tarball (relative to source
* path, and target is destination of file (also relative to $source path)
* @throws \RuntimeException
*/
private function buildCopyActions($source, $role)
{
/** @var $package \SimpleXmlElement */
$package = simplexml_load_file($this->combine($source, 'package.xml'));
if(false === $package)
throw new \RuntimeException('Package definition file is not valid.');
$packageSchemaVersion = $package['version'];
if ($packageSchemaVersion == '1.0') {
$children = $package->release->filelist->children();
$packageName = (string) $package->name;
$packageVersion = (string) $package->release->version;
$sourceDir = $packageName . '-' . $packageVersion;
$result = $this->buildSourceList10($children, $role, $sourceDir);
} elseif ($packageSchemaVersion == '2.0') {
$children = $package->contents->children();
$packageName = (string) $package->name;
$packageVersion = (string) $package->version->release;
$sourceDir = $packageName . '-' . $packageVersion;
$result = $this->buildSourceList20($children, $role, $sourceDir);
} else {
throw new \RuntimeException('Unsupported schema version of package definition file.');
}
return $result;
}
private function buildSourceList10($children, $targetRole, $source = '', $target = '', $role = null)
{
$result = array();
// enumerating files
foreach ($children as $child) {
/** @var $child \SimpleXMLElement */
if ($child->getName() == 'dir') {
$dirSource = $this->combine($source, (string) $child['name']);
$dirTarget = $child['baseinstalldir'] ? : $target;
$dirRole = $child['role'] ? : $role;
$dirFiles = $this->buildSourceList10($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
$result = array_merge($result, $dirFiles);
} elseif ($child->getName() == 'file') {
if (($child['role'] ? : $role) == $targetRole) {
$fileName = (string) ($child['name'] ? : $child[0]); // $child[0] means text content
$fileSource = $this->combine($source, $fileName);
$fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName);
$result[] = array('from' => $fileSource, 'to' => $fileTarget);
}
}
}
return $result;
}
private function buildSourceList20($children, $targetRole, $source = '', $target = '', $role = null)
{
$result = array();
// enumerating files
foreach ($children as $child) {
/** @var $child \SimpleXMLElement */
if ($child->getName() == 'dir') {
$dirSource = $this->combine($source, $child['name']);
$dirTarget = $child['baseinstalldir'] ? : $target;
$dirRole = $child['role'] ? : $role;
$dirFiles = $this->buildSourceList20($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
$result = array_merge($result, $dirFiles);
} elseif ($child->getName() == 'file') {
if (($child['role'] ? : $role) == $targetRole) {
$fileSource = $this->combine($source, (string) $child['name']);
$fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']);
$result[] = array('from' => $fileSource, 'to' => $fileTarget);
}
}
}
return $result;
}
private function combine($left, $right)
{
return rtrim($left, '/') . '/' . ltrim($right, '/');
}
}

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0">
<package version="1.0" packagerversion="1.4.0a4">
<!-- stripped version of package-->
<name>PEAR_Frontend_Gtk</name>
<summary>Gtk (Desktop) PEAR Package Manager</summary>
<release>
<version>0.4.0</version>
<date>2005-03-14</date>
<license>PHP License</license>
<state>beta</state>
<notes>Implement channels, support PEAR 1.4.0 (Greg Beaver)
Tidy up logging a little.
</notes>
<filelist>
<file role="php" baseinstalldir="PEAR/Frontend" md5sum="3edf486653e75f4aac78c1d9d52e72ad" name="Gtk.php"/>
<file role="php" baseinstalldir="PEAR/Frontend" md5sum="d9847b8a0f3e2987f250c5112ef4032a" name="Gtk/Config.php"/>
<file role="php" baseinstalldir="PEAR/Frontend" md5sum="a65ef13fb4c2080404b465af28f67fe3" name="Gtk/xpm/black_close_icon.xpm"/>
</filelist>
</release>
</package>

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<package packagerversion="1.6.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
<!-- stripped version of package-->
<name>Net_URL</name>
<channel>pear.php.net</channel>
<summary>Easy parsing of Urls</summary>
<description>Provides easy parsing of URLs and their constituent parts.</description>
<version>
<release>1.0.15</release>
<api>1.0.15</api>
</version>
<contents>
<dir name="/">
<file baseinstalldir="Net" md5sum="af793351a5f00e31a2df697b54cfbc02" name="docs/example.php" role="doc" />
<file baseinstalldir="Net" md5sum="0488b5531c31332113100971be7ba2d9" name="docs/6470.php" role="doc" />
<file baseinstalldir="Net" md5sum="c7e690d656b56cc48a12399331a35b27" name="URL.php" role="php" />
</dir>
</contents>
</package>

@ -0,0 +1,60 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Downloader;
use Composer\Downloader\PearPackageExtractor;
class PearPackageExtractorTest extends \PHPUnit_Framework_TestCase
{
public function testShouldExtractPackage_1_0()
{
$extractor = $this->getMockForAbstractClass('Composer\Downloader\PearPackageExtractor', array(), '', false);
$method = new \ReflectionMethod($extractor, 'buildCopyActions');
$method->setAccessible(true);
$fileActions = $method->invoke($extractor, __DIR__ . '/Fixtures/Package_v1.0', 'php');
$expectedFileActions = array(
0 => Array(
'from' => 'PEAR_Frontend_Gtk-0.4.0/Gtk.php',
'to' => 'PEAR/Frontend/Gtk.php',
),
1 => Array(
'from' => 'PEAR_Frontend_Gtk-0.4.0/Gtk/Config.php',
'to' => 'PEAR/Frontend/Gtk/Config.php',
),
2 => Array(
'from' => 'PEAR_Frontend_Gtk-0.4.0/Gtk/xpm/black_close_icon.xpm',
'to' => 'PEAR/Frontend/Gtk/xpm/black_close_icon.xpm',
)
);
$this->assertSame($expectedFileActions, $fileActions);
}
public function testShouldExtractPackage_2_0()
{
$extractor = $this->getMockForAbstractClass('Composer\Downloader\PearPackageExtractor', array(), '', false);
$method = new \ReflectionMethod($extractor, 'buildCopyActions');
$method->setAccessible(true);
$fileActions = $method->invoke($extractor, __DIR__ . '/Fixtures/Package_v2.0', 'php');
$expectedFileActions = array(
0 => Array(
'from' => 'Net_URL-1.0.15/URL.php',
'to' => 'Net/URL.php',
)
);
$this->assertSame($expectedFileActions, $fileActions);
}
}
Loading…
Cancel
Save