ProcessLauncher::installSignalHandlers()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 7
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
crap 3.576
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\Process;
13
14
use RuntimeException;
15
use Symfony\Component\Process\ProcessUtils;
16
17
/**
18
 * Launches an interactive process in the foreground.
19
 *
20
 * This class is used to execute "man" and "less".
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class ProcessLauncher
27
{
28
    /**
29
     * @var bool
30
     */
31
    private $running = false;
32
33
    /**
34
     * @var float
35
     */
36
    private $checkInterval = 0.1;
37
38
    /**
39
     * Returns whether the launcher is supported on the current system.
40
     *
41
     * @return bool Whether the launcher is supported on the current system.
42
     */
43
    public function isSupported()
44
    {
45
        return function_exists('proc_open');
46
    }
47
48
    /**
49
     * Returns whether the launcher is currently running.
50
     *
51
     * @return bool Whether the launcher is running.
52
     */
53
    public function isRunning()
54
    {
55
        return $this->running;
56
    }
57
58
    /**
59
     * Returns the interval used to check whether the process is still alive.
60
     *
61
     * By default, the interval is 1 second.
62
     *
63
     * @param float $checkInterval The check interval.
64
     */
65 2
    public function setCheckInterval($checkInterval)
66
    {
67 2
        $this->checkInterval = $checkInterval;
68 2
    }
69
70
    /**
71
     * Launches a process in the foreground.
72
     *
73
     * @param string   $command   The command to execute.
74
     * @param string[] $arguments Arguments to be quoted and inserted into the
75
     *                            command. Each key "key" in the array should
76
     *                            correspond to a placeholder "%key%" in the
77
     *                            command.
78
     * @param bool     $killable  Whether the process can be killed by the user.
79
     *
80
     * @return int The exit status of the process.
81
     */
82 2
    public function launchProcess($command, array $arguments = array(), $killable = true)
83
    {
84 2
        $this->installSignalHandlers($killable);
85
86 2
        $exitCode = $this->run($command, $arguments);
87
88 2
        $this->restoreSignalHandlers($killable);
89
90 2
        return $exitCode;
91
    }
92
93 2 View Code Duplication
    private function installSignalHandlers($terminable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94
    {
95 2
        if (function_exists('pcntl_signal') && !$terminable) {
96
            pcntl_signal(SIGTERM, SIG_IGN);
97
            pcntl_signal(SIGINT, SIG_IGN);
98
        }
99 2
    }
100
101 2 View Code Duplication
    private function restoreSignalHandlers($terminable = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
102
    {
103 2
        if (function_exists('pcntl_signal') && !$terminable) {
104
            pcntl_signal(SIGTERM, SIG_DFL);
105
            pcntl_signal(SIGINT, SIG_DFL);
106
        }
107 2
    }
108
109 2
    private function run($command, array $arguments)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
110
    {
111 2
        if (!function_exists('proc_open')) {
112
            throw new RuntimeException('The "proc_open" function is not available.');
113
        }
114
115 2
        $replacements = array();
116
117 2
        foreach ($arguments as $name => $value) {
118 2
            $replacements['%'.$name.'%'] = ProcessUtils::escapeArgument($value);
119
        }
120
121
        // Insert quoted arguments
122 2
        $command = strtr($command, $replacements);
123
124
        $dspec = array(
125 2
            0 => STDIN,
126 2
            1 => STDOUT,
127 2
            2 => STDERR,
128
        );
129
130 2
        $this->running = true;
131 2
        $proc = proc_open($command, $dspec, $pipes, null, null);
132
133 2
        if (is_resource($proc)) {
134 2
            while (true) {
135 2
                $status = proc_get_status($proc);
136
137 2
                if (!$status['running']) {
138 2
                    break;
139
                }
140
141 2
                sleep($this->checkInterval);
142
            }
143
144 2
            proc_close($proc);
145
        }
146
147 2
        $this->running = false;
148
149 2
        return isset($status['exitcode']) ? $status['exitcode'] : 1;
150
    }
151
}
152