|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Donii Sergii <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace sonrac\WAMP\Commands; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RunCommandInBackground |
|
12
|
|
|
* |
|
13
|
|
|
* @package sonrac\WAMP\Commands |
|
14
|
|
|
* |
|
15
|
|
|
* @see https://github.com/dmitry-ivanov/laravel-helper-functions |
|
16
|
|
|
*/ |
|
17
|
|
|
class RunCommandInBackground |
|
18
|
|
|
{ |
|
19
|
|
|
protected $command = null; |
|
20
|
|
|
protected $before = null; |
|
21
|
|
|
protected $after = null; |
|
22
|
|
|
protected $phpBinary = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* RunCommandInBackground constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $command |
|
28
|
|
|
* @param null $before |
|
|
|
|
|
|
29
|
|
|
* @param null $after |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
5 |
|
public function __construct($command, $before = null, $after = null) |
|
32
|
|
|
{ |
|
33
|
5 |
|
$this->command = $command; |
|
34
|
5 |
|
$this->before = $before; |
|
35
|
5 |
|
$this->after = $after; |
|
36
|
5 |
|
$this->phpBinary = (new PhpExecutableFinder())->find(); |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create new command |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $command |
|
43
|
|
|
* @param string|null $before |
|
44
|
|
|
* @param string|null $after |
|
45
|
|
|
* |
|
46
|
|
|
* @return \sonrac\WAMP\Commands\RunCommandInBackground |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public static function factory($command, $before = null, $after = null) |
|
49
|
|
|
{ |
|
50
|
5 |
|
return new self($command, $before, $after); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Run command in background and return process pid |
|
55
|
|
|
* |
|
56
|
|
|
* @return int|null |
|
57
|
|
|
*/ |
|
58
|
5 |
|
public function runInBackground() |
|
59
|
|
|
{ |
|
60
|
5 |
|
exec($this->composeForRunInBackground(), $out, $pid); |
|
61
|
|
|
|
|
62
|
5 |
|
return count($out) ? (int) $out[0] : null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Prepare command for run ib background |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
5 |
|
protected function composeForRunInBackground() |
|
71
|
|
|
{ |
|
72
|
5 |
|
return "({$this->composeForRun()}) > /dev/null 2>&1 & echo $!"; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Prepare command for run |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
5 |
|
protected function composeForRun() |
|
81
|
|
|
{ |
|
82
|
5 |
|
$parts = []; |
|
83
|
5 |
|
if (!empty($this->before)) { |
|
84
|
|
|
$parts[] = (string) $this->before; |
|
85
|
|
|
} |
|
86
|
5 |
|
$parts[] = 'cd '.base_path(); |
|
|
|
|
|
|
87
|
5 |
|
$parts[] = "{$this->phpBinary} {$this->getArtisan()} {$this->command}"; |
|
88
|
5 |
|
if (!empty($this->after)) { |
|
89
|
|
|
$parts[] = (string) $this->after; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
return implode(' && ', $parts); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get artisan |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
5 |
|
protected function getArtisan() |
|
101
|
|
|
{ |
|
102
|
5 |
|
return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan'; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|