Command::after()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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-2018, 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