PearPackageExtractor.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Downloader;
  12. use Composer\Util\Filesystem;
  13. /**
  14. * Extractor for pear packages.
  15. *
  16. * Composer cannot rely on tar files structure when place it inside package target dir. Correct source files
  17. * disposition must be read from package.xml
  18. * This extract pear package source files to target dir.
  19. *
  20. * @author Alexey Prilipko <palex@farpost.com>
  21. */
  22. class PearPackageExtractor
  23. {
  24. private static $rolesWithoutPackageNamePrefix = array('php', 'script', 'www');
  25. /** @var Filesystem */
  26. private $filesystem;
  27. private $file;
  28. public function __construct($file)
  29. {
  30. if (!is_file($file)) {
  31. throw new \UnexpectedValueException('PEAR package file is not found at '.$file);
  32. }
  33. $this->filesystem = new Filesystem();
  34. $this->file = $file;
  35. }
  36. /**
  37. * Installs PEAR source files according to package.xml definitions and removes extracted files
  38. *
  39. * @param string $target target install location. all source installation would be performed relative to target path.
  40. * @param array $roles types of files to install. default role for PEAR source files are 'php'.
  41. * @param array $vars used for replacement tasks
  42. * @throws \RuntimeException
  43. * @throws \UnexpectedValueException
  44. *
  45. */
  46. public function extractTo($target, array $roles = array('php' => '/', 'script' => '/bin'), $vars = array())
  47. {
  48. $extractionPath = $target.'/tarball';
  49. try {
  50. $archive = new \PharData($this->file);
  51. $archive->extractTo($extractionPath, null, true);
  52. if (!is_file($this->combine($extractionPath, '/package.xml'))) {
  53. throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.');
  54. }
  55. $fileCopyActions = $this->buildCopyActions($extractionPath, $roles, $vars);
  56. $this->copyFiles($fileCopyActions, $extractionPath, $target, $roles, $vars);
  57. $this->filesystem->removeDirectory($extractionPath);
  58. } catch (\Exception $exception) {
  59. throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception);
  60. }
  61. }
  62. /**
  63. * Perform copy actions on files
  64. *
  65. * @param array $files array of copy actions ('from', 'to') with relative paths
  66. * @param $source string path to source dir.
  67. * @param $target string path to destination dir
  68. * @param array $roles array [role => roleRoot] relative root for files having that role
  69. * @param array $vars list of values can be used for replacement tasks
  70. */
  71. private function copyFiles($files, $source, $target, $roles, $vars)
  72. {
  73. foreach ($files as $file) {
  74. $from = $this->combine($source, $file['from']);
  75. $to = $this->combine($target, $roles[$file['role']]);
  76. $to = $this->combine($to, $file['to']);
  77. $tasks = $file['tasks'];
  78. $this->copyFile($from, $to, $tasks, $vars);
  79. }
  80. }
  81. private function copyFile($from, $to, $tasks, $vars)
  82. {
  83. if (!is_file($from)) {
  84. throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.');
  85. }
  86. $this->filesystem->ensureDirectoryExists(dirname($to));
  87. if (0 == count($tasks)) {
  88. $copied = copy($from, $to);
  89. } else {
  90. $content = file_get_contents($from);
  91. $replacements = array();
  92. foreach ($tasks as $task) {
  93. $pattern = $task['from'];
  94. $varName = $task['to'];
  95. if (isset($vars[$varName])) {
  96. $replacements[$pattern] = $vars[$varName];
  97. }
  98. }
  99. $content = strtr($content, $replacements);
  100. $copied = file_put_contents($to, $content);
  101. }
  102. if (false === $copied) {
  103. throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to));
  104. }
  105. }
  106. /**
  107. * Builds list of copy and list of remove actions that would transform extracted PEAR tarball into installed package.
  108. *
  109. * @param $source string path to extracted files.
  110. * @param $role string package file types to extract.
  111. * @return array array of 'source' => 'target', where source is location of file in the tarball (relative to source
  112. * path, and target is destination of file (also relative to $source path)
  113. * @throws \RuntimeException
  114. */
  115. private function buildCopyActions($source, array $roles, $vars)
  116. {
  117. /** @var $package \SimpleXmlElement */
  118. $package = simplexml_load_file($this->combine($source, 'package.xml'));
  119. if(false === $package)
  120. throw new \RuntimeException('Package definition file is not valid.');
  121. $packageSchemaVersion = $package['version'];
  122. if ('1.0' == $packageSchemaVersion) {
  123. $children = $package->release->filelist->children();
  124. $packageName = (string) $package->name;
  125. $packageVersion = (string) $package->release->version;
  126. $sourceDir = $packageName . '-' . $packageVersion;
  127. $result = $this->buildSourceList10($children, $roles, $sourceDir, '', null, $packageName);
  128. } elseif ('2.0' == $packageSchemaVersion || '2.1' == $packageSchemaVersion) {
  129. $children = $package->contents->children();
  130. $packageName = (string) $package->name;
  131. $packageVersion = (string) $package->version->release;
  132. $sourceDir = $packageName . '-' . $packageVersion;
  133. $result = $this->buildSourceList20($children, $roles, $sourceDir, '', null, $packageName);
  134. $namespaces = $package->getNamespaces();
  135. $package->registerXPathNamespace('ns', $namespaces['']);
  136. $releaseNodes = $package->xpath('ns:phprelease');
  137. $this->applyRelease($result, $releaseNodes, $vars);
  138. } else {
  139. throw new \RuntimeException('Unsupported schema version of package definition file.');
  140. }
  141. return $result;
  142. }
  143. private function applyRelease(&$actions, $releaseNodes, $vars)
  144. {
  145. foreach ($releaseNodes as $releaseNode) {
  146. $requiredOs = $releaseNode->installconditions && $releaseNode->installconditions->os && $releaseNode->installconditions->os->name ? (string) $releaseNode->installconditions->os->name : '';
  147. if ($requiredOs && $vars['os'] != $requiredOs) {
  148. continue;
  149. }
  150. if ($releaseNode->filelist) {
  151. foreach ($releaseNode->filelist->children() as $action) {
  152. if ('install' == $action->getName()) {
  153. $name = (string) $action['name'];
  154. $as = (string) $action['as'];
  155. if (isset($actions[$name])) {
  156. $actions[$name]['to'] = $as;
  157. }
  158. } elseif ('ignore' == $action->getName()) {
  159. $name = (string) $action['name'];
  160. unset($actions[$name]);
  161. } else {
  162. // unknown action
  163. }
  164. }
  165. }
  166. break;
  167. }
  168. }
  169. private function buildSourceList10($children, $targetRoles, $source = '', $target = '', $role = null, $packageName)
  170. {
  171. $result = array();
  172. // enumerating files
  173. foreach ($children as $child) {
  174. /** @var $child \SimpleXMLElement */
  175. if ($child->getName() == 'dir') {
  176. $dirSource = $this->combine($source, (string) $child['name']);
  177. $dirTarget = $child['baseinstalldir'] ? : $target;
  178. $dirRole = $child['role'] ? : $role;
  179. $dirFiles = $this->buildSourceList10($child->children(), $targetRoles, $dirSource, $dirTarget, $dirRole, $packageName);
  180. $result = array_merge($result, $dirFiles);
  181. } elseif ($child->getName() == 'file') {
  182. $fileRole = (string) $child['role'] ? : $role;
  183. if (isset($targetRoles[$fileRole])) {
  184. $fileName = (string) ($child['name'] ? : $child[0]); // $child[0] means text content
  185. $fileSource = $this->combine($source, $fileName);
  186. $fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName);
  187. if (!in_array($fileRole, self::$rolesWithoutPackageNamePrefix)) {
  188. $fileTarget = $packageName . '/' . $fileTarget;
  189. }
  190. $result[(string) $child['name']] = array('from' => $fileSource, 'to' => $fileTarget, 'role' => $fileRole, 'tasks' => array());
  191. }
  192. }
  193. }
  194. return $result;
  195. }
  196. private function buildSourceList20($children, $targetRoles, $source = '', $target = '', $role = null, $packageName)
  197. {
  198. $result = array();
  199. // enumerating files
  200. foreach ($children as $child) {
  201. /** @var $child \SimpleXMLElement */
  202. if ('dir' == $child->getName()) {
  203. $dirSource = $this->combine($source, $child['name']);
  204. $dirTarget = $child['baseinstalldir'] ? : $target;
  205. $dirRole = $child['role'] ? : $role;
  206. $dirFiles = $this->buildSourceList20($child->children(), $targetRoles, $dirSource, $dirTarget, $dirRole, $packageName);
  207. $result = array_merge($result, $dirFiles);
  208. } elseif ('file' == $child->getName()) {
  209. $fileRole = (string) $child['role'] ? : $role;
  210. if (isset($targetRoles[$fileRole])) {
  211. $fileSource = $this->combine($source, (string) $child['name']);
  212. $fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']);
  213. $fileTasks = array();
  214. foreach ($child->children('http://pear.php.net/dtd/tasks-1.0') as $taskNode) {
  215. if ('replace' == $taskNode->getName()) {
  216. $fileTasks[] = array('from' => (string) $taskNode->attributes()->from, 'to' => (string) $taskNode->attributes()->to);
  217. }
  218. }
  219. if (!in_array($fileRole, self::$rolesWithoutPackageNamePrefix)) {
  220. $fileTarget = $packageName . '/' . $fileTarget;
  221. }
  222. $result[(string) $child['name']] = array('from' => $fileSource, 'to' => $fileTarget, 'role' => $fileRole, 'tasks' => $fileTasks);
  223. }
  224. }
  225. }
  226. return $result;
  227. }
  228. private function combine($left, $right)
  229. {
  230. return rtrim($left, '/') . '/' . ltrim($right, '/');
  231. }
  232. }