|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Returns the intersection of a file list and set of filesets. |
| 5 | + * |
| 6 | + * Example usage: |
| 7 | + * |
| 8 | + * <filterFileListByFileSet fileList="${files}" returnProperty="filteredFileList" root="${repo.root}"> |
| 9 | + * <fileset refid="files.php.custom.modules"/> |
| 10 | + * <fileset refid="files.php.custom.themes"/> |
| 11 | + * <fileset refid="files.php.tests"/> |
| 12 | + * </filterFileListByFileSet> |
| 13 | + */ |
| 14 | +require_once 'phing/Task.php'; |
| 15 | + |
| 16 | +class FilterFileListByFileSetTask extends Task { |
| 17 | + |
| 18 | + /** |
| 19 | + * The return value. |
| 20 | + * |
| 21 | + * @var null |
| 22 | + */ |
| 23 | + protected $return_property = null; |
| 24 | + |
| 25 | + public function setFileList($fileList) |
| 26 | + { |
| 27 | + $this->fileList = $fileList; |
| 28 | + } |
| 29 | + |
| 30 | + public function setRoot($root) |
| 31 | + { |
| 32 | + $this->root = $root; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Nested adder, adds a set of files (nested fileset attribute). |
| 37 | + * |
| 38 | + * @param FileSet $fs |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + public function addFileSet(FileSet $fs) |
| 42 | + { |
| 43 | + $this->filesets[] = $fs; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * The Phing property the return code should be assigned to. |
| 48 | + * |
| 49 | + * @param string $str The Phing property. |
| 50 | + * |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function setReturnProperty($str) |
| 54 | + { |
| 55 | + $this->return_property = $str; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * The main entry point method. |
| 61 | + * |
| 62 | + * @throws BuildException |
| 63 | + * @return bool $return |
| 64 | + */ |
| 65 | + public function main() { |
| 66 | + |
| 67 | + if (!isset($this->fileList)) { |
| 68 | + throw new BuildException("You must set the file property."); |
| 69 | + } |
| 70 | + |
| 71 | + if (!isset($this->root)) { |
| 72 | + throw new BuildException("You must set the project root."); |
| 73 | + } |
| 74 | + |
| 75 | + if (count($this->filesets) == 0) { |
| 76 | + throw new BuildException("You must define a fileset."); |
| 77 | + } |
| 78 | + |
| 79 | + $this->fileListFiles = array_map(array($this, 'prependProjectPath'), explode("\n", $this->fileList)); |
| 80 | + $this->fileSetFiles = $this->getFilesetFiles(); |
| 81 | + $filteredList = array_intersect($this->fileSetFiles, $this->fileListFiles); |
| 82 | + |
| 83 | + // Return the Behat exit value to a Phing property if specified. |
| 84 | + if (!empty($this->return_property)) { |
| 85 | + $this->getProject() |
| 86 | + ->setProperty($this->return_property, implode($filteredList, ',')); |
| 87 | + } |
| 88 | + |
| 89 | + return (bool) $filteredList; |
| 90 | + } |
| 91 | + |
| 92 | + protected function prependProjectPath($relative_path) { |
| 93 | + return $this->root. DIRECTORY_SEPARATOR . $relative_path; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Return the list of files to parse |
| 98 | + * |
| 99 | + * @see PhpCodeSnifferTask |
| 100 | + * |
| 101 | + * @return string[] list of absolute files to parse |
| 102 | + */ |
| 103 | + protected function getFilesetFiles() |
| 104 | + { |
| 105 | + $files = array(); |
| 106 | + |
| 107 | + foreach ($this->filesets as $fs) { |
| 108 | + $dir = $fs->getDir($this->project)->getAbsolutePath(); |
| 109 | + foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) { |
| 110 | + $file_path = $dir . DIRECTORY_SEPARATOR . $filename; |
| 111 | + $files[] = $file_path; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return $files; |
| 116 | + } |
| 117 | +} |
0 commit comments