Fileset.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /*
  3. * $Id: Fileset.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/system/io/PhingFile.php';
  22. /**
  23. * Builds list of files for PEAR_PackageFileManager using a Phing FileSet.
  24. *
  25. * Some code here is taken from PEAR_PackageFileManager_File -- getting results from flat
  26. * array into the assoc array expected from getFileList().
  27. *
  28. * @author Greg Beaver
  29. * @author Hans Lellelid <hans@xmpl.org>
  30. * @package phing.tasks.ext.pearpackage
  31. * @version $Revision: 905 $
  32. */
  33. class PEAR_PackageFileManager_Fileset {
  34. /**
  35. * Curent Phing Project.
  36. * @var Project
  37. */
  38. private $project;
  39. /**
  40. * FileSets to use.
  41. * @var array FileSet[]
  42. */
  43. private $filesets = array();
  44. /**
  45. * Set up the FileSet filelist generator
  46. *
  47. * 'project' and 'filesets' are the only options that this class uses.
  48. *
  49. * @param PEAR_PackageFileManager
  50. * @param array
  51. */
  52. function __construct($options)
  53. {
  54. if (!is_array($options)) {
  55. $options = $options->getOptions();
  56. }
  57. $this->project = $options['phing_project'];
  58. $this->filesets = $options['phing_filesets'];
  59. }
  60. /**
  61. * Generate the <filelist></filelist> section
  62. * of the package file.
  63. *
  64. * This function performs the backend generation of the array
  65. * containing all files in this package
  66. * @return array structure of all files to include
  67. */
  68. function getFileList() {
  69. $allfiles = array();
  70. foreach($this->filesets as $fs) {
  71. $ds = $fs->getDirectoryScanner($this->project);
  72. $files = $ds->getIncludedFiles();
  73. // We need to store these files keyed by the basedir from DirectoryScanner
  74. // so that we can resolve the fullpath of the file later.
  75. if (isset($allfiles[$ds->getBasedir()]))
  76. {
  77. $allfiles[$ds->getBasedir()] = array_merge($allfiles[$ds->getBasedir()], $files);
  78. }
  79. else
  80. {
  81. $allfiles[$ds->getBasedir()] = $files;
  82. }
  83. }
  84. $struc = array();
  85. foreach($allfiles as $basedir => $files) {
  86. foreach($files as $file) {
  87. // paths are relative to $basedir above
  88. $path = strtr(dirname($file), DIRECTORY_SEPARATOR, '/');
  89. if (!$path || $path == '.') {
  90. $path = '/'; // for array index
  91. }
  92. $parts = explode('.', basename($file));
  93. $ext = array_pop($parts);
  94. if (strlen($ext) == strlen($file)) {
  95. $ext = '';
  96. }
  97. $f = new PhingFile($basedir, $file);
  98. $struc[$path][] = array('file' => basename($file),
  99. 'ext' => $ext,
  100. 'path' => (($path == '/') ? basename($file) : $path . '/' . basename($file)),
  101. 'fullpath' => $f->getAbsolutePath());
  102. }
  103. }
  104. uksort($struc,'strnatcasecmp');
  105. foreach($struc as $key => $ind) {
  106. usort($ind, array($this, 'sortfiles'));
  107. $struc[$key] = $ind;
  108. }
  109. $tempstruc = $struc;
  110. $struc = array('/' => $tempstruc['/']);
  111. $bv = 0;
  112. foreach($tempstruc as $key => $ind) {
  113. $save = $key;
  114. if ($key != '/') {
  115. $struc['/'] = $this->setupDirs($struc['/'], explode('/', $key), $tempstruc[$key]);
  116. }
  117. }
  118. uksort($struc['/'], array($this, 'mystrucsort'));
  119. return $struc;
  120. }
  121. /**
  122. * Recursively move contents of $struc into associative array
  123. *
  124. * The contents of $struc have many indexes like 'dir/subdir/subdir2'.
  125. * This function converts them to
  126. * array('dir' => array('subdir' => array('subdir2')))
  127. * @param array struc is array('dir' => array of files in dir,
  128. * 'dir/subdir' => array of files in dir/subdir,...)
  129. * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')
  130. * @return array same as struc but with array('dir' =>
  131. * array(file1,file2,'subdir' => array(file1,...)))
  132. */
  133. private function setupDirs($struc, $dir, $contents) {
  134. if (!count($dir)) {
  135. foreach($contents as $dir => $files) {
  136. if (is_string($dir)) {
  137. if (strpos($dir, '/')) {
  138. $test = true;
  139. $a = $contents[$dir];
  140. unset($contents[$dir]);
  141. $b = explode('/', $dir);
  142. $c = array_shift($b);
  143. if (isset($contents[$c])) {
  144. $contents[$c] = $this->setDir($contents[$c], $this->setupDirs(array(), $b, $a));
  145. } else {
  146. $contents[$c] = $this->setupDirs(array(), $b, $a);
  147. }
  148. }
  149. }
  150. }
  151. return $contents;
  152. }
  153. $me = array_shift($dir);
  154. if (!isset($struc[$me])) {
  155. $struc[$me] = array();
  156. }
  157. $struc[$me] = $this->setupDirs($struc[$me], $dir, $contents);
  158. return $struc;
  159. }
  160. /**
  161. * Recursively add all the subdirectories of $contents to $dir without erasing anything in
  162. * $dir
  163. * @param array
  164. * @param array
  165. * @return array processed $dir
  166. */
  167. function setDir($dir, $contents)
  168. {
  169. while(list($one,$two) = each($contents)) {
  170. if (isset($dir[$one])) {
  171. $dir[$one] = $this->setDir($dir[$one], $contents[$one]);
  172. } else {
  173. $dir[$one] = $two;
  174. }
  175. }
  176. return $dir;
  177. }
  178. /**
  179. * Sorting functions for the file list
  180. * @param string
  181. * @param string
  182. * @access private
  183. */
  184. function sortfiles($a, $b)
  185. {
  186. return strnatcasecmp($a['file'],$b['file']);
  187. }
  188. function mystrucsort($a, $b)
  189. {
  190. if (is_numeric($a) && is_string($b)) return 1;
  191. if (is_numeric($b) && is_string($a)) return -1;
  192. if (is_numeric($a) && is_numeric($b))
  193. {
  194. if ($a > $b) return 1;
  195. if ($a < $b) return -1;
  196. if ($a == $b) return 0;
  197. }
  198. return strnatcasecmp($a,$b);
  199. }
  200. }