1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Robo\Task\File; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use Robo\Common\ResourceExistenceChecker; |
7
|
|
|
use Robo\Result; |
8
|
|
|
use Robo\Task\BaseTask; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Merges files into one. Used for preparing assets. |
12
|
|
|
* |
13
|
|
|
* ``` php |
14
|
|
|
* <?php |
15
|
|
|
* $this->taskConcat([ |
16
|
|
|
* 'web/assets/screen.css', |
17
|
|
|
* 'web/assets/print.css', |
18
|
|
|
* 'web/assets/theme.css' |
19
|
|
|
* ]) |
20
|
|
|
* ->to('web/assets/style.css') |
21
|
|
|
* ->run() |
22
|
|
|
* ?> |
23
|
|
|
* ``` |
24
|
|
|
*/ |
25
|
|
|
class Concat extends BaseTask |
26
|
|
|
{ |
27
|
|
|
use ResourceExistenceChecker; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array|\Iterator |
31
|
|
|
*/ |
32
|
|
|
protected $files; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $dst; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param array|\Iterator $files |
43
|
|
|
*/ |
44
|
|
|
public function __construct($files) |
45
|
|
|
{ |
46
|
|
|
$this->files = $files; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* set the destination file |
51
|
|
|
* |
52
|
|
|
* @param string $dst |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function to($dst) |
57
|
|
|
{ |
58
|
|
|
$this->dst = $dst; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function run() |
67
|
|
|
{ |
68
|
|
|
if (is_null($this->dst) || "" === $this->dst) { |
69
|
|
|
return Result::error($this, 'You must specify a destination file with to() method.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!$this->checkResources($this->files, 'file')) { |
73
|
|
|
return Result::error($this, 'Source files are missing!'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
if (file_exists($this->dst) && !is_writable($this->dst)) { |
|
|
|
|
77
|
|
|
return Result::error($this, 'Destination already exists and cannot be overwritten.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$dump = ''; |
81
|
|
|
|
82
|
|
|
foreach ($this->files as $path) { |
83
|
|
|
foreach (glob($path) as $file) { |
84
|
|
|
$dump .= file_get_contents($file) . "\n"; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->printTaskInfo('Writing {destination}', ['destination' => $this->dst]); |
89
|
|
|
|
90
|
|
|
$dst = $this->dst . '.part'; |
91
|
|
|
$write_result = file_put_contents($dst, $dump); |
92
|
|
|
|
93
|
|
|
if (false === $write_result) { |
94
|
|
|
@unlink($dst); |
95
|
|
|
return Result::error($this, 'File write failed.'); |
96
|
|
|
} |
97
|
|
|
// Cannot be cross-volume; should always succeed. |
98
|
|
|
@rename($dst, $this->dst); |
99
|
|
|
|
100
|
|
|
return Result::success($this); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.