|
@@ -35,22 +35,22 @@ class PearPackageExtractor
|
|
throw new \UnexpectedValueException('PEAR package file is not found at '.$file);
|
|
throw new \UnexpectedValueException('PEAR package file is not found at '.$file);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ $this->filesystem = new Filesystem();
|
|
$this->file = $file;
|
|
$this->file = $file;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* Installs PEAR source files according to package.xml definitions and removes extracted files
|
|
* 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'.
|
|
|
|
- *
|
|
|
|
|
|
+ * @param string $target target install location. all source installation would be performed relative to target path.
|
|
|
|
+ * @param array $roles types of files to install. default role for PEAR source files are 'php'.
|
|
|
|
+ * @param array $vars used for replacement tasks
|
|
* @throws \RuntimeException
|
|
* @throws \RuntimeException
|
|
|
|
+ * @throws \UnexpectedValueException
|
|
|
|
+ *
|
|
*/
|
|
*/
|
|
- public function extractTo($target, $role = 'php')
|
|
|
|
|
|
+ public function extractTo($target, array $roles = array('php' => '/', 'script' => '/bin'), $vars = array())
|
|
{
|
|
{
|
|
- $this->filesystem = new Filesystem();
|
|
|
|
-
|
|
|
|
$extractionPath = $target.'/tarball';
|
|
$extractionPath = $target.'/tarball';
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -61,8 +61,8 @@ class PearPackageExtractor
|
|
throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.');
|
|
throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.');
|
|
}
|
|
}
|
|
|
|
|
|
- $fileCopyActions = $this->buildCopyActions($extractionPath, $role);
|
|
|
|
- $this->copyFiles($fileCopyActions, $extractionPath, $target);
|
|
|
|
|
|
+ $fileCopyActions = $this->buildCopyActions($extractionPath, $roles, $vars);
|
|
|
|
+ $this->copyFiles($fileCopyActions, $extractionPath, $target, $roles, $vars);
|
|
$this->filesystem->removeDirectory($extractionPath);
|
|
$this->filesystem->removeDirectory($extractionPath);
|
|
} catch (\Exception $exception) {
|
|
} catch (\Exception $exception) {
|
|
throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception);
|
|
throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception);
|
|
@@ -72,20 +72,24 @@ class PearPackageExtractor
|
|
/**
|
|
/**
|
|
* Perform copy actions on files
|
|
* Perform copy actions on files
|
|
*
|
|
*
|
|
- * @param $files array array('from', 'to') with relative paths
|
|
|
|
|
|
+ * @param array $files array of copy actions ('from', 'to') with relative paths
|
|
* @param $source string path to source dir.
|
|
* @param $source string path to source dir.
|
|
* @param $target string path to destination dir
|
|
* @param $target string path to destination dir
|
|
|
|
+ * @param array $roles array [role => roleRoot] relative root for files having that role
|
|
|
|
+ * @param array $vars list of values can be used for replacement tasks
|
|
*/
|
|
*/
|
|
- private function copyFiles($files, $source, $target)
|
|
|
|
|
|
+ private function copyFiles($files, $source, $target, $roles, $vars)
|
|
{
|
|
{
|
|
foreach ($files as $file) {
|
|
foreach ($files as $file) {
|
|
$from = $this->combine($source, $file['from']);
|
|
$from = $this->combine($source, $file['from']);
|
|
- $to = $this->combine($target, $file['to']);
|
|
|
|
- $this->copyFile($from, $to);
|
|
|
|
|
|
+ $to = $this->combine($target, $roles[$file['role']]);
|
|
|
|
+ $to = $this->combine($to, $file['to']);
|
|
|
|
+ $tasks = $file['tasks'];
|
|
|
|
+ $this->copyFile($from, $to, $tasks, $vars);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private function copyFile($from, $to)
|
|
|
|
|
|
+ private function copyFile($from, $to, $tasks, $vars)
|
|
{
|
|
{
|
|
if (!is_file($from)) {
|
|
if (!is_file($from)) {
|
|
throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.');
|
|
throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.');
|
|
@@ -93,7 +97,24 @@ class PearPackageExtractor
|
|
|
|
|
|
$this->filesystem->ensureDirectoryExists(dirname($to));
|
|
$this->filesystem->ensureDirectoryExists(dirname($to));
|
|
|
|
|
|
- if (!copy($from, $to)) {
|
|
|
|
|
|
+ if (0 == count($tasks)) {
|
|
|
|
+ $copied = copy($from, $to);
|
|
|
|
+ } else {
|
|
|
|
+ $content = file_get_contents($from);
|
|
|
|
+ $replacements = array();
|
|
|
|
+ foreach ($tasks as $task) {
|
|
|
|
+ $pattern = $task['from'];
|
|
|
|
+ $varName = $task['to'];
|
|
|
|
+ if (isset($vars[$varName])) {
|
|
|
|
+ $replacements[$pattern] = $vars[$varName];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $content = strtr($content, $replacements);
|
|
|
|
+
|
|
|
|
+ $copied = file_put_contents($to, $content);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (false === $copied) {
|
|
throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to));
|
|
throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -107,7 +128,7 @@ class PearPackageExtractor
|
|
* path, and target is destination of file (also relative to $source path)
|
|
* path, and target is destination of file (also relative to $source path)
|
|
* @throws \RuntimeException
|
|
* @throws \RuntimeException
|
|
*/
|
|
*/
|
|
- private function buildCopyActions($source, $role)
|
|
|
|
|
|
+ private function buildCopyActions($source, array $roles, $vars)
|
|
{
|
|
{
|
|
/** @var $package \SimpleXmlElement */
|
|
/** @var $package \SimpleXmlElement */
|
|
$package = simplexml_load_file($this->combine($source, 'package.xml'));
|
|
$package = simplexml_load_file($this->combine($source, 'package.xml'));
|
|
@@ -120,13 +141,18 @@ class PearPackageExtractor
|
|
$packageName = (string) $package->name;
|
|
$packageName = (string) $package->name;
|
|
$packageVersion = (string) $package->release->version;
|
|
$packageVersion = (string) $package->release->version;
|
|
$sourceDir = $packageName . '-' . $packageVersion;
|
|
$sourceDir = $packageName . '-' . $packageVersion;
|
|
- $result = $this->buildSourceList10($children, $role, $sourceDir);
|
|
|
|
|
|
+ $result = $this->buildSourceList10($children, $roles, $sourceDir);
|
|
} elseif ('2.0' == $packageSchemaVersion || '2.1' == $packageSchemaVersion) {
|
|
} elseif ('2.0' == $packageSchemaVersion || '2.1' == $packageSchemaVersion) {
|
|
$children = $package->contents->children();
|
|
$children = $package->contents->children();
|
|
$packageName = (string) $package->name;
|
|
$packageName = (string) $package->name;
|
|
$packageVersion = (string) $package->version->release;
|
|
$packageVersion = (string) $package->version->release;
|
|
$sourceDir = $packageName . '-' . $packageVersion;
|
|
$sourceDir = $packageName . '-' . $packageVersion;
|
|
- $result = $this->buildSourceList20($children, $role, $sourceDir);
|
|
|
|
|
|
+ $result = $this->buildSourceList20($children, $roles, $sourceDir);
|
|
|
|
+
|
|
|
|
+ $namespaces = $package->getNamespaces();
|
|
|
|
+ $package->registerXPathNamespace('ns', $namespaces['']);
|
|
|
|
+ $releaseNodes = $package->xpath('ns:phprelease');
|
|
|
|
+ $this->applyRelease($result, $releaseNodes, $vars);
|
|
} else {
|
|
} else {
|
|
throw new \RuntimeException('Unsupported schema version of package definition file.');
|
|
throw new \RuntimeException('Unsupported schema version of package definition file.');
|
|
}
|
|
}
|
|
@@ -134,7 +160,34 @@ class PearPackageExtractor
|
|
return $result;
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
|
|
- private function buildSourceList10($children, $targetRole, $source = '', $target = '', $role = null)
|
|
|
|
|
|
+ private function applyRelease(&$actions, $releaseNodes, $vars)
|
|
|
|
+ {
|
|
|
|
+ foreach ($releaseNodes as $releaseNode) {
|
|
|
|
+ $requiredOs = (string) ($releaseNode->installconditions && $releaseNode->installconditions->os && $releaseNode->installconditions->os->name) ?: '';
|
|
|
|
+ if ($requiredOs && $vars['os'] != $requiredOs)
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ if ($releaseNode->filelist) {
|
|
|
|
+ foreach ($releaseNode->filelist->children() as $action) {
|
|
|
|
+ if ('install' == $action->getName()) {
|
|
|
|
+ $name = (string) $action['name'];
|
|
|
|
+ $as = (string) $action['as'];
|
|
|
|
+ if (isset($actions[$name])) {
|
|
|
|
+ $actions[$name]['to'] = $as;
|
|
|
|
+ }
|
|
|
|
+ } elseif ('ignore' == $action->getName()) {
|
|
|
|
+ $name = (string) $action['name'];
|
|
|
|
+ unset($actions[$name]);
|
|
|
|
+ } else {
|
|
|
|
+ // unknown action
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function buildSourceList10($children, $targetRoles, $source = '', $target = '', $role = null)
|
|
{
|
|
{
|
|
$result = array();
|
|
$result = array();
|
|
|
|
|
|
@@ -145,14 +198,15 @@ class PearPackageExtractor
|
|
$dirSource = $this->combine($source, (string) $child['name']);
|
|
$dirSource = $this->combine($source, (string) $child['name']);
|
|
$dirTarget = $child['baseinstalldir'] ? : $target;
|
|
$dirTarget = $child['baseinstalldir'] ? : $target;
|
|
$dirRole = $child['role'] ? : $role;
|
|
$dirRole = $child['role'] ? : $role;
|
|
- $dirFiles = $this->buildSourceList10($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
|
|
|
|
|
|
+ $dirFiles = $this->buildSourceList10($child->children(), $targetRoles, $dirSource, $dirTarget, $dirRole);
|
|
$result = array_merge($result, $dirFiles);
|
|
$result = array_merge($result, $dirFiles);
|
|
} elseif ($child->getName() == 'file') {
|
|
} elseif ($child->getName() == 'file') {
|
|
- if (($child['role'] ? : $role) == $targetRole) {
|
|
|
|
|
|
+ $fileRole = (string) $child['role'] ? : $role;
|
|
|
|
+ if (isset($targetRoles[$fileRole])) {
|
|
$fileName = (string) ($child['name'] ? : $child[0]); // $child[0] means text content
|
|
$fileName = (string) ($child['name'] ? : $child[0]); // $child[0] means text content
|
|
$fileSource = $this->combine($source, $fileName);
|
|
$fileSource = $this->combine($source, $fileName);
|
|
$fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName);
|
|
$fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName);
|
|
- $result[] = array('from' => $fileSource, 'to' => $fileTarget);
|
|
|
|
|
|
+ $result[(string) $child['name']] = array('from' => $fileSource, 'to' => $fileTarget, 'role' => $fileRole, 'tasks' => array());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -160,24 +214,31 @@ class PearPackageExtractor
|
|
return $result;
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
|
|
- private function buildSourceList20($children, $targetRole, $source = '', $target = '', $role = null)
|
|
|
|
|
|
+ private function buildSourceList20($children, $targetRoles, $source = '', $target = '', $role = null)
|
|
{
|
|
{
|
|
$result = array();
|
|
$result = array();
|
|
|
|
|
|
// enumerating files
|
|
// enumerating files
|
|
foreach ($children as $child) {
|
|
foreach ($children as $child) {
|
|
/** @var $child \SimpleXMLElement */
|
|
/** @var $child \SimpleXMLElement */
|
|
- if ($child->getName() == 'dir') {
|
|
|
|
|
|
+ if ('dir' == $child->getName()) {
|
|
$dirSource = $this->combine($source, $child['name']);
|
|
$dirSource = $this->combine($source, $child['name']);
|
|
$dirTarget = $child['baseinstalldir'] ? : $target;
|
|
$dirTarget = $child['baseinstalldir'] ? : $target;
|
|
$dirRole = $child['role'] ? : $role;
|
|
$dirRole = $child['role'] ? : $role;
|
|
- $dirFiles = $this->buildSourceList20($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
|
|
|
|
|
|
+ $dirFiles = $this->buildSourceList20($child->children(), $targetRoles, $dirSource, $dirTarget, $dirRole);
|
|
$result = array_merge($result, $dirFiles);
|
|
$result = array_merge($result, $dirFiles);
|
|
- } elseif ($child->getName() == 'file') {
|
|
|
|
- if (($child['role'] ? : $role) == $targetRole) {
|
|
|
|
|
|
+ } elseif ('file' == $child->getName()) {
|
|
|
|
+ $fileRole = (string) $child['role'] ? : $role;
|
|
|
|
+ if (isset($targetRoles[$fileRole])) {
|
|
$fileSource = $this->combine($source, (string) $child['name']);
|
|
$fileSource = $this->combine($source, (string) $child['name']);
|
|
$fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']);
|
|
$fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']);
|
|
- $result[] = array('from' => $fileSource, 'to' => $fileTarget);
|
|
|
|
|
|
+ $fileTasks = array();
|
|
|
|
+ foreach ($child->children('http://pear.php.net/dtd/tasks-1.0') as $taskNode) {
|
|
|
|
+ if ('replace' == $taskNode->getName()) {
|
|
|
|
+ $fileTasks[] = array('from' => (string) $taskNode->attributes()->from, 'to' => (string) $taskNode->attributes()->to);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $result[(string) $child['name']] = array('from' => $fileSource, 'to' => $fileTarget, 'role' => $fileRole, 'tasks' => $fileTasks);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|