| Total Complexity | 39 |
| Total Lines | 253 |
| Duplicated Lines | 0 % |
| Coverage | 48.51% |
| Changes | 0 | ||
| 1 | <?php |
||
| 43 | class UpToDateTask extends Task implements Condition |
||
| 44 | { |
||
| 45 | use FileListAware; |
||
| 46 | use FileSetAware; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $property; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $value; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var File |
||
| 60 | */ |
||
| 61 | private $sourceFile; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var File |
||
| 65 | */ |
||
| 66 | private $targetFile; |
||
| 67 | |||
| 68 | protected $mapperElement = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The property to set if the target file is more up-to-date than |
||
| 72 | * (each of) the source file(s). |
||
| 73 | * |
||
| 74 | * @param string $property the name of the property to set if Target is up-to-date. |
||
| 75 | */ |
||
| 76 | 2 | public function setProperty($property) |
|
| 77 | { |
||
| 78 | 2 | $this->property = $property; |
|
| 79 | 2 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Get property name |
||
| 83 | * |
||
| 84 | * @return string property the name of the property to set if Target is up-to-date. |
||
| 85 | */ |
||
| 86 | 2 | public function getProperty() |
|
| 87 | { |
||
| 88 | 2 | return $this->property; |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The value to set the named property to if the target file is more |
||
| 93 | * up-to-date than (each of) the source file(s). Defaults to 'true'. |
||
| 94 | * |
||
| 95 | * @param mixed $value the value to set the property to if Target is up-to-date |
||
| 96 | */ |
||
| 97 | 2 | public function setValue($value) |
|
| 98 | { |
||
| 99 | 2 | $this->value = $value; |
|
| 100 | 2 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the value, or "true" if a specific value wasn't provided. |
||
| 104 | */ |
||
| 105 | 2 | private function getValue() |
|
| 106 | { |
||
| 107 | 2 | return $this->value ?? "true"; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The file which must be more up-to-date than (each of) the source file(s) |
||
| 112 | * if the property is to be set. |
||
| 113 | * |
||
| 114 | * @param string|File $file the file we are checking against. |
||
| 115 | */ |
||
| 116 | 2 | public function setTargetFile($file) |
|
| 117 | { |
||
| 118 | 2 | if (is_string($file)) { |
|
| 119 | 2 | $file = new File($file); |
|
| 120 | } |
||
| 121 | 2 | $this->targetFile = $file; |
|
| 122 | 2 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * The file that must be older than the target file |
||
| 126 | * if the property is to be set. |
||
| 127 | * |
||
| 128 | * @param string|File $file the file we are checking against the target file. |
||
| 129 | */ |
||
| 130 | 2 | public function setSrcfile($file) |
|
| 136 | 2 | } |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Defines the FileNameMapper to use (nested mapper element). |
||
| 140 | */ |
||
| 141 | public function createMapper() |
||
| 142 | { |
||
| 143 | if ($this->mapperElement !== null) { |
||
| 144 | throw new BuildException( |
||
| 145 | "Cannot define more than one mapper", |
||
| 146 | $this->getLocation() |
||
| 147 | ); |
||
| 148 | } |
||
| 149 | $this->mapperElement = new Mapper($this->getProject()); |
||
| 150 | |||
| 151 | return $this->mapperElement; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Evaluate (all) target and source file(s) to |
||
| 156 | * see if the target(s) is/are up-to-date. |
||
| 157 | * |
||
| 158 | * @throws BuildException |
||
| 159 | * @return boolean |
||
| 160 | */ |
||
| 161 | 2 | public function evaluate() |
|
| 162 | { |
||
| 163 | 2 | if (count($this->filesets) == 0 && count($this->filelists) == 0 && $this->sourceFile === null) { |
|
| 164 | throw new BuildException( |
||
| 165 | "At least one srcfile or a nested " |
||
| 166 | . "<fileset> or <filelist> element must be set." |
||
| 167 | ); |
||
| 168 | } |
||
| 169 | |||
| 170 | 2 | if ((count($this->filesets) > 0 || count($this->filelists) > 0) && $this->sourceFile !== null) { |
|
| 171 | throw new BuildException( |
||
| 172 | "Cannot specify both the srcfile " |
||
| 173 | . "attribute and a nested <fileset> " |
||
| 174 | . "or <filelist> element." |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | 2 | if ($this->targetFile === null && $this->mapperElement === null) { |
|
| 179 | throw new BuildException( |
||
| 180 | "The targetfile attribute or a nested " |
||
| 181 | . "mapper element must be set." |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | |||
| 185 | // if the target file is not there, then it can't be up-to-date |
||
| 186 | 2 | if ($this->targetFile !== null && !$this->targetFile->exists()) { |
|
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | // if the source file isn't there, throw an exception |
||
| 191 | 2 | if ($this->sourceFile !== null && !$this->sourceFile->exists()) { |
|
| 192 | throw new BuildException( |
||
| 193 | $this->sourceFile->getAbsolutePath() |
||
| 194 | . " not found." |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | 2 | $upToDate = true; |
|
| 199 | 2 | for ($i = 0, $size = count($this->filesets); $i < $size && $upToDate; $i++) { |
|
| 200 | $fs = $this->filesets[$i]; |
||
| 201 | $ds = $fs->getDirectoryScanner($this->project); |
||
| 202 | $upToDate = $upToDate && $this->scanDir( |
||
| 203 | $fs->getDir($this->project), |
||
| 204 | $ds->getIncludedFiles() |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | 2 | for ($i = 0, $size = count($this->filelists); $i < $size && $upToDate; $i++) { |
|
| 209 | $fl = $this->filelists[$i]; |
||
| 210 | $srcFiles = $fl->getFiles($this->project); |
||
| 211 | $upToDate = $upToDate && $this->scanDir( |
||
| 212 | $fl->getDir($this->project), |
||
| 213 | $srcFiles |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | 2 | if ($this->sourceFile !== null) { |
|
| 218 | 2 | if ($this->mapperElement === null) { |
|
| 219 | 2 | $upToDate = $upToDate && |
|
| 220 | 2 | ($this->targetFile->lastModified() >= $this->sourceFile->lastModified()); |
|
| 221 | } else { |
||
| 222 | $sfs = new SourceFileScanner($this); |
||
| 223 | $upToDate = $upToDate && |
||
| 224 | count( |
||
| 225 | $sfs->restrict( |
||
| 226 | $this->sourceFile->getAbsolutePath(), |
||
|
|
|||
| 227 | null, |
||
| 228 | null, |
||
| 229 | $this->mapperElement->getImplementation() |
||
| 230 | ) |
||
| 231 | ) === 0; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | 2 | return $upToDate; |
|
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Sets property to true if target file(s) have a more recent timestamp |
||
| 241 | * than (each of) the corresponding source file(s). |
||
| 242 | * |
||
| 243 | * @throws BuildException |
||
| 244 | */ |
||
| 245 | 2 | public function main() |
|
| 271 | ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | 2 | } |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param File $srcDir |
||
| 278 | * @param $files |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | protected function scanDir(File $srcDir, $files) |
||
| 298 |