Completed
Push — master ( 7df873...57a8d7 )
by Greg
02:28
created

src/Task/Base/Exec.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Task\Base;
3
4
use Robo\Common\ExecTrait;
5
use Robo\Contract\CommandInterface;
6
use Robo\Contract\PrintedInterface;
7
use Robo\Contract\SimulatedInterface;
8
use Robo\Task\BaseTask;
9
use Symfony\Component\Process\Process;
10
use Robo\Result;
11
12
/**
13
 * Executes shell script. Closes it when running in background mode.
14
 *
15
 * ``` php
16
 * <?php
17
 * $this->taskExec('compass')->arg('watch')->run();
18
 * // or use shortcut
19
 * $this->_exec('compass watch');
20
 *
21
 * $this->taskExec('compass watch')->background()->run();
22
 *
23
 * if ($this->taskExec('phpunit .')->run()->wasSuccessful()) {
24
 *  $this->say('tests passed');
25
 * }
26
 *
27
 * ?>
28
 * ```
29
 */
30
class Exec extends BaseTask implements CommandInterface, PrintedInterface, SimulatedInterface
31
{
32
    use \Robo\Common\CommandReceiver;
33
    use \Robo\Common\ExecOneCommand;
34
35
    /**
36
     * @var static[]
37
     */
38
    protected static $instances = [];
39
40
    /**
41
     * @var string|\Robo\Contract\CommandInterface
42
     */
43
    protected $command;
44
45
    /**
46
     * @param string|\Robo\Contract\CommandInterface $command
47
     */
48
    public function __construct($command)
49
    {
50
        $this->command = $this->receiveCommand($command);
51
    }
52
53
    /**
54
     *
55
     */
56
    public function __destruct()
57
    {
58
        $this->stop();
59
    }
60
61
    /**
62
     * Executes command in background mode (asynchronously)
63
     *
64
     * @return $this
65
     */
66
    public function background($arg = true)
67
    {
68
        self::$instances[] = $this;
69
        $this->background = $arg;
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected function getCommandDescription()
77
    {
78
        return $this->getCommand();
79
    }
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getCommand()
84
    {
85
        return trim($this->command . $this->arguments);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function simulate($context)
92
    {
93
        $this->printAction($context);
94
    }
95
96
    public static function stopRunningJobs()
97
    {
98
        foreach (self::$instances as $instance) {
99
            if ($instance) {
100
                unset($instance);
101
            }
102
        }
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function run()
109
    {
110
        $this->hideProgressIndicator();
111
        // TODO: Symfony 4 requires that we supply the working directory.
112
        $result_data = $this->execute(new Process($this->getCommand(), getcwd()));
0 ignored issues
show
$this->getCommand() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
113
        return new Result(
114
            $this,
115
            $result_data->getExitCode(),
116
            $result_data->getMessage(),
117
            $result_data->getData()
118
        );
119
        $this->showProgressIndicator();
120
    }
121
}
122
123
if (function_exists('pcntl_signal')) {
124
    pcntl_signal(SIGTERM, ['Robo\Task\Base\Exec', 'stopRunningJobs']);
125
}
126
127
register_shutdown_function(['Robo\Task\Base\Exec', 'stopRunningJobs']);
128