1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JK\Sam\Filter\Copy; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use JK\Sam\Filter\Filter; |
7
|
|
|
use SplFileInfo; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class CopyFilter extends Filter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param SplFileInfo[] $sources |
14
|
|
|
* @param SplFileInfo[] $destinations |
15
|
|
|
* @return SplFileInfo[] |
16
|
|
|
* @throws Exception |
17
|
|
|
*/ |
18
|
|
|
public function run(array $sources, array $destinations) |
19
|
|
|
{ |
20
|
|
|
$fileSystem = new Filesystem(); |
21
|
|
|
|
22
|
|
|
// must have the same sources and destinations number |
23
|
|
|
if (count($sources) !== count($destinations) && count($destinations) !== 1) { |
24
|
|
|
throw new Exception('Sources and destinations count mismatch'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (count($destinations) === 1 && !is_dir($destinations[0]) && count($sources) > 1) { |
28
|
|
|
throw new Exception('If only one destination is set for multiple source, it should be a directory'); |
29
|
|
|
} |
30
|
|
|
$copiedFiles = []; |
31
|
|
|
|
32
|
|
|
// copy generated files to its destination |
33
|
|
|
foreach ($sources as $index => $source) { |
34
|
|
|
|
35
|
|
|
if (array_key_exists($index, $destinations)) { |
36
|
|
|
$destination = $destinations[$index]; |
37
|
|
|
} else { |
38
|
|
|
$destination = $destinations[0]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (is_dir($destination)) { |
42
|
|
|
$destination = $destination.'/'.$source->getFilename(); |
43
|
|
|
} |
44
|
|
|
$this->addNotification('copying "'.$source.'" to "'.$destination.'"'); |
45
|
|
|
$fileSystem->copy($source->getRealPath(), $destination); |
|
|
|
|
46
|
|
|
$copiedFiles[] = $destination; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $copiedFiles; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
|
|
public function checkRequirements() |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return the file extensions supported by this filter. |
61
|
|
|
* |
62
|
|
|
* @return string[] |
63
|
|
|
*/ |
64
|
|
|
public function getSupportedExtensions() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'*' |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.