9e7156561606af833e5241dc9682a2fbd905b5d1
[pharext/pharext] / src / pharext / Task / Make.php
1 <?php
2
3 namespace pharext\Task;
4
5 use pharext\ExecCmd;
6 use pharext\Exception;
7 use pharext\Task;
8
9 /**
10 * Run make in the source dir
11 */
12 class Make implements Task
13 {
14 /**
15 * @var string
16 */
17 private $cwd;
18
19 /**
20 * @var array
21 */
22 private $args;
23
24 /**
25 * @var string
26 */
27 private $sudo;
28
29 /**
30 *
31 * @param string $cwd working directory
32 * @param array $args make's arguments
33 * @param string $sudo sudo command
34 */
35 public function __construct($cwd, array $args = null, $sudo = null) {
36 $this->cwd = $cwd;
37 $this->sudo = $sudo;
38 $this->args = $args;
39 }
40
41 /**
42 *
43 * @param bool $verbose
44 * @throws \pharext\Exception
45 */
46 public function run($verbose = false) {
47 $pwd = getcwd();
48 if (!chdir($this->cwd)) {
49 throw new Exception;
50 }
51 try {
52 $cmd = new ExecCmd("make", $verbose);
53 if (isset($this->sudo)) {
54 $cmd->setSu($this->sudo);
55 }
56 $args = $this->args;
57 if (!$verbose) {
58 $args = array_merge((array) $args, ["-s"]);
59 }
60 $cmd->run($args);
61 } finally {
62 chdir($pwd);
63 }
64 }
65 }