Total Complexity | 102 |
Total Lines | 752 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ApplyTask often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApplyTask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class ApplyTask extends ExecTask |
||
48 | { |
||
49 | use ResourceAware; |
||
50 | |||
51 | public const SOURCEFILE_ID = '__SOURCEFILE__'; |
||
52 | protected $currentdirectory; |
||
53 | |||
54 | /** |
||
55 | * Whether output should be appended to or overwrite an existing file. |
||
56 | * |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $appendoutput = false; |
||
60 | |||
61 | /** |
||
62 | * Runs the command only once, appending all files as arguments |
||
63 | * else command will be executed once for every file. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | protected $parallel = false; |
||
68 | |||
69 | /** |
||
70 | * Whether source file name should be added to the end of command automatically. |
||
71 | * |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $addsourcefile = true; |
||
75 | |||
76 | /** |
||
77 | * Whether the filenames should be passed on the command line as relative pathnames (relative to the base directory of the corresponding fileset/list). |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | protected $relative = false; |
||
82 | |||
83 | protected $currentos; |
||
84 | protected $osvariant; |
||
85 | |||
86 | /** |
||
87 | * Logging level for status messages. |
||
88 | * |
||
89 | * @var int |
||
90 | */ |
||
91 | protected $loglevel; |
||
92 | |||
93 | /** |
||
94 | * Whether to use forward-slash as file-separator on the file names. |
||
95 | * |
||
96 | * @var bool |
||
97 | */ |
||
98 | protected $forwardslash = false; |
||
99 | |||
100 | /** |
||
101 | * Limit the amount of parallelism by passing at most this many sourcefiles at once |
||
102 | * (Set it to <= 0 for unlimited). |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | protected $maxparallel = 0; |
||
107 | |||
108 | protected static $types = [ |
||
109 | 'FILE' => 'file', |
||
110 | 'DIR' => 'dir', |
||
111 | 'BOTH' => 'both', |
||
112 | ]; |
||
113 | |||
114 | protected $type = 'file'; |
||
115 | /** |
||
116 | * @var CommandlineMarker |
||
117 | */ |
||
118 | protected $targetFilePos; |
||
119 | /** |
||
120 | * @var CommandlineMarker |
||
121 | */ |
||
122 | protected $srcFilePos; |
||
123 | protected $srcIsFirst = true; |
||
124 | |||
125 | protected $skipEmpty = false; |
||
126 | private $force = false; |
||
127 | private $mapper; |
||
128 | private $destDir; |
||
129 | |||
130 | /** |
||
131 | * @var Mapper |
||
132 | */ |
||
133 | private $mapperElement; |
||
134 | private $additionalCmds; |
||
135 | |||
136 | /** |
||
137 | * Set whether empty filesets will be skipped. If true and |
||
138 | * no source files have been found or are newer than their |
||
139 | * corresponding target files, the command will not be run. |
||
140 | * |
||
141 | * @param bool $skip whether to skip empty filesets |
||
142 | */ |
||
143 | public function setSkipEmptyFilesets(bool $skip): void |
||
144 | { |
||
145 | $this->skipEmpty = $skip; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Specify the directory where target files are to be placed. |
||
150 | * |
||
151 | * @param File $dest the File object representing the destination directory |
||
152 | */ |
||
153 | public function setDest(File $dest): void |
||
154 | { |
||
155 | $this->destDir = $dest; |
||
156 | } |
||
157 | |||
158 | public function setAppend(bool $append): void |
||
159 | { |
||
160 | $this->appendoutput = $append; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Run the command only once, appending all files as arguments. |
||
165 | * |
||
166 | * @param bool $parallel Identifier for files as arguments appending |
||
167 | */ |
||
168 | public function setParallel(bool $parallel) |
||
169 | { |
||
170 | $this->parallel = $parallel; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * To add the source filename at the end of command of automatically. |
||
175 | * |
||
176 | * @param bool $addsourcefile Identifier for adding source file at the end of command |
||
177 | */ |
||
178 | public function setAddsourcefile(bool $addsourcefile) |
||
179 | { |
||
180 | $this->addsourcefile = $addsourcefile; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Whether the filenames should be passed on the command line as relative |
||
185 | * pathnames (relative to the base directory of the corresponding fileset/list). |
||
186 | */ |
||
187 | public function setRelative(bool $relative): void |
||
188 | { |
||
189 | $this->relative = $relative; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Fail on command exits with a returncode other than zero. |
||
194 | * |
||
195 | * @param bool $failonerror Indicator to fail on error |
||
196 | */ |
||
197 | public function setFailonerror(bool $failonerror): void |
||
198 | { |
||
199 | $this->checkreturn = $failonerror; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Whether to use forward-slash as file-separator on the file names. |
||
204 | * |
||
205 | * @param bool $forwardslash Indicator to use forward-slash |
||
206 | */ |
||
207 | public function setForwardslash(bool $forwardslash): void |
||
208 | { |
||
209 | $this->forwardslash = $forwardslash; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Limit the amount of parallelism by passing at most this many sourcefiles at once. |
||
214 | */ |
||
215 | public function setMaxparallel(int $max) |
||
216 | { |
||
217 | $this->maxparallel = $max; |
||
218 | } |
||
219 | |||
220 | public function setForce(bool $force): void |
||
221 | { |
||
222 | $this->force = $force; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Set whether the command works only on files, directories or both. |
||
227 | * |
||
228 | * @param string $type a FileDirBoth EnumeratedAttribute |
||
229 | */ |
||
230 | public function setType(string $type): void |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Supports embedded <targetfile> element. |
||
237 | * |
||
238 | * @throws BuildException |
||
239 | * |
||
240 | * @return CommandlineMarker |
||
241 | */ |
||
242 | public function createTargetfile() |
||
243 | { |
||
244 | if (null !== $this->targetFilePos) { |
||
245 | throw new BuildException( |
||
246 | $this->getTaskType() . " doesn\\'t support multiple " |
||
247 | . 'targetfile elements.', |
||
248 | $this->getLocation() |
||
249 | ); |
||
250 | } |
||
251 | |||
252 | $this->targetFilePos = $this->commandline->createMarker(); |
||
253 | $this->srcIsFirst = (null !== $this->srcFilePos); |
||
254 | |||
255 | return $this->targetFilePos; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Supports embedded <srcfile> element. |
||
260 | * |
||
261 | * @throws BuildException |
||
262 | * |
||
263 | * @return CommandlineMarker |
||
264 | */ |
||
265 | public function createSrcfile() |
||
266 | { |
||
267 | if (null !== $this->srcFilePos) { |
||
268 | throw new BuildException( |
||
269 | $this->getTaskType() . " doesn\\'t support multiple " |
||
270 | . 'srcfile elements.', |
||
271 | $this->getLocation() |
||
272 | ); |
||
273 | } |
||
274 | |||
275 | $this->srcFilePos = $this->commandline->createMarker(); |
||
276 | |||
277 | return $this->srcFilePos; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @throws BuildException |
||
282 | * |
||
283 | * @return Mapper |
||
284 | */ |
||
285 | public function createMapper() |
||
286 | { |
||
287 | if (null !== $this->mapperElement) { |
||
288 | throw new BuildException( |
||
289 | 'Cannot define more than one mapper', |
||
290 | $this->getLocation() |
||
291 | ); |
||
292 | } |
||
293 | $this->mapperElement = new Mapper($this->getProject()); |
||
294 | |||
295 | return $this->mapperElement; |
||
296 | } |
||
297 | |||
298 | // T A S K M E T H O D S |
||
299 | |||
300 | /** |
||
301 | * Do work. |
||
302 | * |
||
303 | * @throws BuildException |
||
304 | */ |
||
305 | public function main() |
||
306 | { |
||
307 | try { |
||
308 | // Log |
||
309 | $this->log('Started ', $this->loglevel); |
||
310 | // Initialize // |
||
311 | $this->prepare(); |
||
312 | $haveExecuted = false; |
||
313 | $totalFiles = 0; |
||
314 | $totalDirs = 0; |
||
315 | $fileNames = []; |
||
316 | |||
317 | // Validate O.S. applicability |
||
318 | if ($this->isValidOs()) { |
||
319 | // Build the command // |
||
320 | $this->buildCommand(); |
||
321 | // Process // |
||
322 | // - FileSets |
||
323 | foreach ($this->filesets as $fs) { |
||
324 | $currentType = $this->type; |
||
325 | if ($fs instanceof DirSet) { |
||
326 | if ($this->type !== self::$types['DIR']) { |
||
327 | $this->log( |
||
328 | "Found a nested dirset but type is {$this->type} . " |
||
329 | . 'Temporarily switching to type="dir" on the' |
||
330 | . ' assumption that you really did mean' |
||
331 | . ' <dirset> not <fileset>.', |
||
332 | Project::MSG_DEBUG |
||
333 | ); |
||
334 | $currentType = 'dir'; |
||
335 | } |
||
336 | } |
||
337 | $base = $fs->getDir($this->project); |
||
338 | $ds = $fs->getDirectoryScanner($this->project); |
||
339 | if ($currentType !== self::$types['DIR']) { |
||
340 | $s = $this->getFiles($base, $ds); |
||
341 | foreach ($s as $fileName) { |
||
342 | ++$totalFiles; |
||
343 | $fileNames[] = $fileName; |
||
344 | $baseDirs[] = $base; |
||
345 | } |
||
346 | } |
||
347 | if ($currentType !== self::$types['FILE']) { |
||
348 | $s = $this->getDirs($base, $ds); |
||
349 | foreach ($s as $fileName) { |
||
350 | ++$totalDirs; |
||
351 | $fileNames[] = $fileName; |
||
352 | $baseDirs[] = $base; |
||
353 | } |
||
354 | } |
||
355 | if (0 === count($fileNames) && $this->skipEmpty) { |
||
356 | $this->logSkippingFileset($currentType, $ds, $base); |
||
357 | |||
358 | continue; |
||
359 | } |
||
360 | $this->process( |
||
361 | $fs->getDirectoryScanner($this->project)->getIncludedFiles(), |
||
362 | $fs->getDir($this->project) |
||
363 | ); |
||
364 | $haveExecuted = true; |
||
365 | } |
||
366 | unset($this->filesets); |
||
367 | // - FileLists |
||
368 | /** |
||
369 | * @var FileList $fl |
||
370 | */ |
||
371 | foreach ($this->filelists as $fl) { |
||
372 | ++$totalFiles; |
||
373 | $this->process($fl->getFiles($this->project), $fl->getDir($this->project)); |
||
374 | $haveExecuted = true; |
||
375 | } |
||
376 | unset($this->filelists); |
||
377 | } |
||
378 | if ($haveExecuted) { |
||
379 | $this->log( |
||
380 | 'Applied ' . $this->commandline->getExecutable() . ' to ' |
||
381 | . $totalFiles . ' file' |
||
382 | . (1 !== $totalFiles ? 's' : '') . ' and ' |
||
383 | . $totalDirs . ' director' |
||
384 | . (1 !== $totalDirs ? 'ies' : 'y') . '.', |
||
385 | $this->loglevel |
||
386 | ); |
||
387 | } |
||
388 | /// Cleanup // |
||
389 | $this->cleanup(); |
||
390 | // Log |
||
391 | $this->log('End ', $this->loglevel); |
||
392 | } catch (IOException | \InvalidArgumentException | UnexpectedValueException $e) { |
||
393 | throw new BuildException('Execute failed: ' . $e, $e, $this->getLocation()); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Prepares the filename per base directory and relative path information. |
||
399 | * |
||
400 | * @param array|string $filename |
||
401 | * @param string $basedir |
||
402 | * @param string $relative |
||
403 | * |
||
404 | * @throws IOException |
||
405 | * |
||
406 | * @return mixed processed filenames |
||
407 | */ |
||
408 | public function getFilePath($filename, $basedir, $relative) |
||
409 | { |
||
410 | // Validating the 'file' information |
||
411 | $files = (array) $filename; |
||
412 | |||
413 | // Processing the file information |
||
414 | foreach ($files as $index => $file) { |
||
415 | $absolutefilename = ((false === $relative) ? ($basedir . FileUtils::getSeparator()) : ''); |
||
416 | $absolutefilename .= $file; |
||
417 | if (false === $relative) { |
||
418 | $files[$index] = (new FileUtils())->normalize($absolutefilename); |
||
419 | } else { |
||
420 | $files[$index] = $absolutefilename; |
||
421 | } |
||
422 | } |
||
423 | |||
424 | return is_array($filename) ? $files : $files[0]; |
||
425 | } |
||
426 | |||
427 | // T A S K C O R E M E T H O D S |
||
428 | |||
429 | protected function getFiles(File $baseDir, DirectoryScanner $ds) |
||
430 | { |
||
431 | return $this->restrict($ds->getIncludedFiles(), $baseDir); |
||
432 | } |
||
433 | |||
434 | protected function getDirs(File $baseDir, DirectoryScanner $ds) |
||
435 | { |
||
436 | return $this->restrict($ds->getIncludedDirectories(), $baseDir); |
||
437 | } |
||
438 | |||
439 | protected function restrict($s, File $baseDir) |
||
440 | { |
||
441 | $sfs = new SourceFileScanner($this); |
||
442 | |||
443 | return (null === $this->mapper || $this->force) |
||
444 | ? $s |
||
445 | : $sfs->restrict($s, $baseDir, $this->destDir, $this->mapper); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Initializes the task operations, i.e. |
||
450 | * - Required information validation |
||
451 | * - Working directory. |
||
452 | * |
||
453 | * @throws BuildException |
||
454 | * @throws IOException |
||
455 | */ |
||
456 | protected function prepare() |
||
457 | { |
||
458 | // Log |
||
459 | $this->log('Initializing started ', $this->loglevel); |
||
460 | |||
461 | ///// Validating the required parameters ///// |
||
462 | |||
463 | if (!in_array($this->type, self::$types)) { |
||
464 | throw new BuildException('Type must be one of \'file\', \'dir\' or \'both\'.'); |
||
465 | } |
||
466 | |||
467 | // Executable |
||
468 | if (null === $this->commandline->getExecutable()) { |
||
|
|||
469 | $this->throwBuildException('Please provide "executable" information'); |
||
470 | } |
||
471 | |||
472 | // Retrieving the current working directory |
||
473 | $this->currentdirectory = getcwd(); |
||
474 | |||
475 | // Directory (in which the command should be executed) |
||
476 | if (null !== $this->dir) { |
||
477 | // Try expanding (any) symbolic links |
||
478 | if (!$this->dir->getCanonicalFile()->isDirectory()) { |
||
479 | $this->throwBuildException("'" . $this->dir . "' is not a valid directory"); |
||
480 | } |
||
481 | |||
482 | // Change working directory |
||
483 | $dirchangestatus = @chdir($this->dir->getPath()); |
||
484 | |||
485 | // Log |
||
486 | $this->log( |
||
487 | 'Working directory change ' . ($dirchangestatus ? 'successful' : 'failed') . ' to ' . $this->dir->getPath(), |
||
488 | $this->loglevel |
||
489 | ); |
||
490 | } |
||
491 | |||
492 | ///// Preparing the task environment ///// |
||
493 | |||
494 | // Getting current operationg system |
||
495 | $this->currentos = Phing::getProperty('os.name'); |
||
496 | |||
497 | // Log |
||
498 | $this->log('Operating System identified : ' . $this->currentos, $this->loglevel); |
||
499 | |||
500 | // Getting the O.S. type identifier |
||
501 | // Validating the 'filesystem' for determining the OS type [UNIX, WINNT and WIN32] |
||
502 | // (Another usage could be with 'os.name' for determination) |
||
503 | if ('WIN' === strtoupper(substr(Phing::getProperty('host.fstype'), 0, 3))) { |
||
504 | $this->osvariant = 'WIN'; // Probable Windows flavour |
||
505 | } else { |
||
506 | $this->osvariant = 'LIN'; // Probable GNU/Linux flavour |
||
507 | } |
||
508 | |||
509 | // Log |
||
510 | $this->log('Operating System variant identified : ' . $this->osvariant, $this->loglevel); |
||
511 | |||
512 | if (0 === count($this->filesets) && 0 === count($this->filelists) && 0 === count($this->getDirSets())) { |
||
513 | throw new BuildException( |
||
514 | 'no resources specified', |
||
515 | $this->getLocation() |
||
516 | ); |
||
517 | } |
||
518 | if (null !== $this->targetFilePos && null === $this->mapperElement) { |
||
519 | throw new BuildException( |
||
520 | 'targetfile specified without mapper', |
||
521 | $this->getLocation() |
||
522 | ); |
||
523 | } |
||
524 | if (null !== $this->destDir && null === $this->mapperElement) { |
||
525 | throw new BuildException( |
||
526 | 'dest specified without mapper', |
||
527 | $this->getLocation() |
||
528 | ); |
||
529 | } |
||
530 | |||
531 | if (null !== $this->mapperElement) { |
||
532 | $this->mapper = $this->mapperElement->getImplementation(); |
||
533 | $this->log('Mapper identified : ' . get_class($this->mapper), $this->loglevel); |
||
534 | } |
||
535 | |||
536 | $this->commandline->setEscape($this->escape); |
||
537 | |||
538 | // Log |
||
539 | $this->log('Initializing completed ', $this->loglevel); |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Builds the full command to execute and stores it in $realCommand. |
||
544 | * |
||
545 | * @throws BuildException |
||
546 | */ |
||
547 | protected function buildCommand() |
||
548 | { |
||
549 | // Log |
||
550 | $this->log('Command building started ', $this->loglevel); |
||
551 | |||
552 | // Building the executable |
||
553 | $this->realCommand = (string) $this->commandline; |
||
554 | |||
555 | $this->additionalCmds = ''; |
||
556 | |||
557 | // Adding the source filename at the end of command, validating the existing |
||
558 | // sourcefile position explicit mentioning |
||
559 | if (true === $this->addsourcefile) { |
||
560 | $this->realCommand .= ' ' . self::SOURCEFILE_ID; |
||
561 | } |
||
562 | |||
563 | // Setting command output redirection with content appending |
||
564 | if (null !== $this->output) { |
||
565 | $this->additionalCmds .= sprintf( |
||
566 | ' 1>%s %s', |
||
567 | $this->appendoutput ? '>' : '', |
||
568 | escapeshellarg($this->output->getPath()) |
||
569 | ); |
||
570 | } elseif ($this->spawn) { // Validating the 'spawn' configuration, and redirecting the output to 'null' |
||
571 | $this->additionalCmds .= sprintf(' %s', 'WIN' === $this->osvariant ? '> NUL' : '1>/dev/null'); |
||
572 | |||
573 | $this->log('For process spawning, setting Output nullification ', $this->loglevel); |
||
574 | } |
||
575 | |||
576 | // Setting command error redirection with content appending |
||
577 | if (null !== $this->error) { |
||
578 | $this->additionalCmds .= sprintf( |
||
579 | ' 2>%s %s', |
||
580 | $this->appendoutput ? '>' : '', |
||
581 | escapeshellarg($this->error->getPath()) |
||
582 | ); |
||
583 | } |
||
584 | |||
585 | // Setting the execution as a background process |
||
586 | if ($this->spawn) { |
||
587 | // Validating the O.S. variant |
||
588 | if ('WIN' === $this->osvariant) { |
||
589 | $this->additionalCmds = 'start /b ' . $this->additionalCmds; // MS Windows background process forking |
||
590 | } else { |
||
591 | $this->additionalCmds .= ' &'; // GNU/Linux background process forking |
||
592 | } |
||
593 | } |
||
594 | |||
595 | $this->additionalCmds = rtrim($this->additionalCmds); |
||
596 | |||
597 | // Log |
||
598 | $this->log('Command built : ' . $this->realCommand . $this->additionalCmds, $this->loglevel); |
||
599 | |||
600 | // Log |
||
601 | $this->log('Command building completed ', $this->loglevel); |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Runs cleanup tasks post execution |
||
606 | * - Restore working directory. |
||
607 | * |
||
608 | * @param null|mixed $return |
||
609 | * @param null|mixed $output |
||
610 | */ |
||
611 | protected function cleanup($return = null, $output = null): void |
||
612 | { |
||
613 | // Restore working directory |
||
614 | if (null !== $this->dir) { |
||
615 | @chdir($this->currentdirectory); |
||
616 | } |
||
617 | } |
||
618 | |||
619 | private function logSkippingFileset($currentType, DirectoryScanner $ds, File $base) |
||
620 | { |
||
621 | $includedCount = ( |
||
622 | ($currentType !== self::$types['DIR']) ? $ds->getIncludedFilesCount() : 0 |
||
623 | ) + ( |
||
624 | ($currentType !== self::$types['FILES']) ? $ds->getIncludedDirectoriesCount() : 0 |
||
625 | ); |
||
626 | $this->log( |
||
627 | 'Skipping fileset for directory ' . $base . '. It is ' |
||
628 | . (($includedCount > 0) ? 'up to date.' : 'empty.'), |
||
629 | $this->loglevel |
||
630 | ); |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * Processes the files list with provided information for execution. |
||
635 | * |
||
636 | * @param array $srcFiles File list for processing |
||
637 | * @param string $basedir Base directory of the file list |
||
638 | * |
||
639 | * @throws BuildException |
||
640 | * @throws IOException |
||
641 | * @throws \InvalidArgumentException |
||
642 | */ |
||
643 | private function process($srcFiles, $basedir) |
||
644 | { |
||
645 | // Log |
||
646 | $this->log("Processing files with base directory ({$basedir}) ", $this->loglevel); |
||
647 | $targets = []; |
||
648 | if (null !== $this->targetFilePos) { |
||
649 | $addedFiles = []; |
||
650 | foreach ($srcFiles as $count => $file) { |
||
651 | if (null !== $this->mapper) { |
||
652 | $subTargets = $this->mapper->main($file); |
||
653 | if (null !== $subTargets) { |
||
654 | foreach ($subTargets as $subTarget) { |
||
655 | if ($this->relative) { |
||
656 | $name = $subTarget; |
||
657 | } else { |
||
658 | $name = (new File($this->destDir, $subTarget))->getAbsolutePath(); |
||
659 | } |
||
660 | if ($this->forwardslash && '/' !== FileUtils::getSeparator()) { |
||
661 | $name = str_replace(FileUtils::getSeparator(), '/', $name); |
||
662 | } |
||
663 | if (!isset($addedFiles[$name])) { |
||
664 | $targets[] = $name; |
||
665 | $addedFiles[] = $name; |
||
666 | } |
||
667 | } |
||
668 | } |
||
669 | } |
||
670 | } |
||
671 | } |
||
672 | $targetFiles = $targets; |
||
673 | |||
674 | if (!$this->addsourcefile) { |
||
675 | $srcFiles = []; |
||
676 | } |
||
677 | $orig = $this->commandline->getCommandline(); |
||
678 | $result = []; // range(0,count($orig) + count($srcFiles) + count($targetFiles)); |
||
679 | |||
680 | $srcIndex = count($orig); |
||
681 | if (null !== $this->srcFilePos) { |
||
682 | $srcIndex = $this->srcFilePos->getPosition(); |
||
683 | } |
||
684 | if (null !== $this->targetFilePos) { |
||
685 | $targetIndex = $this->targetFilePos->getPosition(); |
||
686 | |||
687 | if ($srcIndex < $targetIndex || ($srcIndex === $targetIndex && $this->srcIsFirst)) { |
||
688 | // 0 --> srcIndex |
||
689 | $result[] = $orig; |
||
690 | |||
691 | // srcIndex --> targetIndex |
||
692 | $result += array_slice($orig, $srcIndex + count($srcFiles), $targetIndex - $srcIndex, true); |
||
693 | |||
694 | $result[] = $orig; |
||
695 | $result[] = $targetFiles; |
||
696 | $result = array_merge(...$result); |
||
697 | |||
698 | // targetIndex --> end |
||
699 | $result = array_merge( |
||
700 | array_slice( |
||
701 | $orig, |
||
702 | $targetIndex + count($srcFiles) + count($targetFiles), |
||
703 | count($orig) - $targetIndex, |
||
704 | true |
||
705 | ), |
||
706 | $result |
||
707 | ); |
||
708 | } else { |
||
709 | // 0 --> targetIndex |
||
710 | $result[] = $orig; |
||
711 | $result[] = $targetFiles; |
||
712 | $result = array_merge(...$result); |
||
713 | |||
714 | // targetIndex --> srcIndex |
||
715 | $result = array_merge( |
||
716 | array_slice( |
||
717 | $orig, |
||
718 | $targetIndex + count($targetFiles), |
||
719 | $srcIndex - $targetIndex, |
||
720 | true |
||
721 | ), |
||
722 | $result |
||
723 | ); |
||
724 | |||
725 | // srcIndex --> end |
||
726 | $result = array_merge( |
||
727 | array_slice( |
||
728 | $orig, |
||
729 | $srcIndex + count($srcFiles) + count($targetFiles), |
||
730 | count($orig) - $srcIndex, |
||
731 | true |
||
732 | ), |
||
733 | $result |
||
734 | ); |
||
735 | $srcIndex += count($targetFiles); |
||
736 | } |
||
737 | } else { // no targetFilePos |
||
738 | // 0 --> srcIndex |
||
739 | $result = array_merge(array_slice($orig, 0, $srcIndex, true), $result); |
||
740 | // srcIndex --> end |
||
741 | $result = array_merge( |
||
742 | array_slice($orig, $srcIndex + count($srcFiles), count($orig) - $srcIndex, true), |
||
743 | $result |
||
744 | ); |
||
745 | } |
||
746 | // fill in source file names |
||
747 | foreach ($srcFiles as $i => $file) { |
||
748 | if ($this->relative) { |
||
749 | $src = $file; |
||
750 | } else { |
||
751 | $src = (new File($basedir, $file))->getAbsolutePath(); |
||
752 | } |
||
753 | if ($this->forwardslash && '/' !== FileUtils::getSeparator()) { |
||
754 | $src = str_replace(FileUtils::getSeparator(), '/', $src); |
||
755 | } |
||
756 | if ( |
||
757 | null !== $this->srcFilePos |
||
758 | && ('' !== $this->srcFilePos->getPrefix() |
||
759 | || '' !== $this->srcFilePos->getSuffix()) |
||
760 | ) { |
||
761 | $src = $this->srcFilePos->getPrefix() . $src . $this->srcFilePos->getSuffix(); |
||
762 | } |
||
763 | $result[$srcIndex + $i] = $src; |
||
764 | } |
||
765 | |||
766 | $this->commandline = new Commandline(implode(' ', $result)); |
||
767 | $this->commandline->setEscape($this->escape); |
||
768 | $this->realCommand = (string) $this->commandline . $this->additionalCmds; |
||
769 | |||
770 | [$returncode, $output] = $this->executeCommand(); |
||
771 | |||
772 | $this->maybeSetReturnPropertyValue($returncode); |
||
773 | |||
774 | // Sets the output property |
||
775 | if ($this->outputProperty) { |
||
776 | $previousValue = $this->project->getProperty($this->outputProperty); |
||
777 | if (!empty($previousValue)) { |
||
778 | $previousValue .= "\n"; |
||
779 | } |
||
780 | $this->project->setProperty($this->outputProperty, $previousValue . implode("\n", $output)); |
||
781 | } |
||
782 | |||
783 | // Validating the 'return-code' |
||
784 | if ($this->checkreturn && (0 !== $returncode)) { |
||
785 | $this->throwBuildException("Task exited with code ({$returncode})"); |
||
786 | } |
||
787 | } |
||
788 | |||
789 | /** |
||
790 | * Throws the exception with specified information. |
||
791 | * |
||
792 | * @param string $information Exception information |
||
793 | * |
||
794 | * @throws BuildException |
||
795 | */ |
||
796 | private function throwBuildException($information): void |
||
799 | } |
||
800 | } |
||
801 |