PearPackageExtractor.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /** @var Filesystem */
  25. private $filesystem;
  26. private $file;
  27. public function __construct($file)
  28. {
  29. if (!is_file($file)) {
  30. throw new \UnexpectedValueException('PEAR package file is not found at '.$file);
  31. }
  32. $this->file = $file;
  33. }
  34. /**
  35. * Installs PEAR source files according to package.xml definitions and removes extracted files
  36. *
  37. * @param $file string path to downloaded PEAR archive file
  38. * @param $target string target install location. all source installation would be performed relative to target path.
  39. * @param $role string type of files to install. default role for PEAR source files are 'php'.
  40. *
  41. * @throws \RuntimeException
  42. */
  43. public function extractTo($target, $role = 'php')
  44. {
  45. $this->filesystem = new Filesystem();
  46. $extractionPath = $target.'/tarball';
  47. try {
  48. $archive = new \PharData($this->file);
  49. $archive->extractTo($extractionPath, null, true);
  50. if (!is_file($this->combine($extractionPath, '/package.xml'))) {
  51. throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.');
  52. }
  53. $fileCopyActions = $this->buildCopyActions($extractionPath, $role);
  54. $this->copyFiles($fileCopyActions, $extractionPath, $target);
  55. $this->filesystem->removeDirectory($extractionPath);
  56. } catch (\Exception $exception) {
  57. throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception);
  58. }
  59. }
  60. /**
  61. * Perform copy actions on files
  62. *
  63. * @param $files array array('from', 'to') with relative paths
  64. * @param $source string path to source dir.
  65. * @param $target string path to destination dir
  66. */
  67. private function copyFiles($files, $source, $target)
  68. {
  69. foreach ($files as $file) {
  70. $from = $this->combine($source, $file['from']);
  71. $to = $this->combine($target, $file['to']);
  72. $this->copyFile($from, $to);
  73. }
  74. }
  75. private function copyFile($from, $to)
  76. {
  77. if (!is_file($from)) {
  78. throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.');
  79. }
  80. $this->filesystem->ensureDirectoryExists(dirname($to));
  81. if (!copy($from, $to)) {
  82. throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to));
  83. }
  84. }
  85. /**
  86. * Builds list of copy and list of remove actions that would transform extracted PEAR tarball into installed package.
  87. *
  88. * @param $source string path to extracted files.
  89. * @param $role string package file types to extract.
  90. * @return array array of 'source' => 'target', where source is location of file in the tarball (relative to source
  91. * path, and target is destination of file (also relative to $source path)
  92. * @throws \RuntimeException
  93. */
  94. private function buildCopyActions($source, $role)
  95. {
  96. /** @var $package \SimpleXmlElement */
  97. $package = simplexml_load_file($this->combine($source, 'package.xml'));
  98. if(false === $package)
  99. throw new \RuntimeException('Package definition file is not valid.');
  100. $packageSchemaVersion = $package['version'];
  101. if ('1.0' == $packageSchemaVersion) {
  102. $children = $package->release->filelist->children();
  103. $packageName = (string) $package->name;
  104. $packageVersion = (string) $package->release->version;
  105. $sourceDir = $packageName . '-' . $packageVersion;
  106. $result = $this->buildSourceList10($children, $role, $sourceDir);
  107. } elseif ('2.0' == $packageSchemaVersion || '2.1' == $packageSchemaVersion) {
  108. $children = $package->contents->children();
  109. $packageName = (string) $package->name;
  110. $packageVersion = (string) $package->version->release;
  111. $sourceDir = $packageName . '-' . $packageVersion;
  112. $result = $this->buildSourceList20($children, $role, $sourceDir);
  113. } else {
  114. throw new \RuntimeException('Unsupported schema version of package definition file.');
  115. }
  116. return $result;
  117. }
  118. private function buildSourceList10($children, $targetRole, $source = '', $target = '', $role = null)
  119. {
  120. $result = array();
  121. // enumerating files
  122. foreach ($children as $child) {
  123. /** @var $child \SimpleXMLElement */
  124. if ($child->getName() == 'dir') {
  125. $dirSource = $this->combine($source, (string) $child['name']);
  126. $dirTarget = $child['baseinstalldir'] ? : $target;
  127. $dirRole = $child['role'] ? : $role;
  128. $dirFiles = $this->buildSourceList10($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
  129. $result = array_merge($result, $dirFiles);
  130. } elseif ($child->getName() == 'file') {
  131. if (($child['role'] ? : $role) == $targetRole) {
  132. $fileName = (string) ($child['name'] ? : $child[0]); // $child[0] means text content
  133. $fileSource = $this->combine($source, $fileName);
  134. $fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName);
  135. $result[] = array('from' => $fileSource, 'to' => $fileTarget);
  136. }
  137. }
  138. }
  139. return $result;
  140. }
  141. private function buildSourceList20($children, $targetRole, $source = '', $target = '', $role = null)
  142. {
  143. $result = array();
  144. // enumerating files
  145. foreach ($children as $child) {
  146. /** @var $child \SimpleXMLElement */
  147. if ($child->getName() == 'dir') {
  148. $dirSource = $this->combine($source, $child['name']);
  149. $dirTarget = $child['baseinstalldir'] ? : $target;
  150. $dirRole = $child['role'] ? : $role;
  151. $dirFiles = $this->buildSourceList20($child->children(), $targetRole, $dirSource, $dirTarget, $dirRole);
  152. $result = array_merge($result, $dirFiles);
  153. } elseif ($child->getName() == 'file') {
  154. if (($child['role'] ? : $role) == $targetRole) {
  155. $fileSource = $this->combine($source, (string) $child['name']);
  156. $fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']);
  157. $result[] = array('from' => $fileSource, 'to' => $fileTarget);
  158. }
  159. }
  160. }
  161. return $result;
  162. }
  163. private function combine($left, $right)
  164. {
  165. return rtrim($left, '/') . '/' . ltrim($right, '/');
  166. }
  167. }