Completed
Push — master ( a1acae...da50b2 )
by Andrii
15:34
created

Command::before()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development.
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\components;
12
13
use Yii;
14
15
/**
16
 * Command goal.
17
 * Holds and executes commands.
18
 */
19
class Command extends \hidev\base\Component
20
{
21
    public $path;
22
    public $sudo;
23
    public $comment;
24
    public $commands;
25
26
    protected $_cwd;
27
28
    public function setCommand($value)
29
    {
30
        $this->commands = $value;
31
    }
32
33
    public function getCommands()
34
    {
35
        if (!is_array($this->commands)) {
36
            $this->commands = explode("\n", $this->commands);
37
        }
38
39
        return $this->commands;
40
    }
41
42
    public function save()
43
    {
44
        $this->before();
45
        foreach ($this->getCommands() as $command) {
46
            $command = trim($command);
47
            if ($command) {
48
                passthru(($this->sudo ? 'sudo ' : '') . $command);
49
            }
50
        }
51
        $this->after();
52
    }
53
54
    public function before()
55
    {
56
        $this->_cwd = getcwd();
57
        if ($this->path) {
58
            chdir(Yii::getAlias($this->path));
59
        }
60
    }
61
62
    public function after()
63
    {
64
        chdir($this->_cwd);
65
    }
66
}
67