1 | <?php |
||
25 | class CommandExecutor |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $host; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $path; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $cwd; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $console; |
||
46 | |||
47 | /** |
||
48 | * @var PhpFinder |
||
49 | */ |
||
50 | protected $finder; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | const TIMEOUT = 2; |
||
56 | |||
57 | /** |
||
58 | * @param PhpFinder $finder |
||
59 | * @param RouterInterface $router |
||
60 | * @param RequestStack $request_stack |
||
61 | * @param string $root_dir |
||
62 | */ |
||
63 | 5 | public function __construct(PhpFinder $finder, RouterInterface $router, RequestStack $request_stack, $root_dir) |
|
64 | { |
||
65 | 5 | $this->finder = $finder; |
|
66 | 5 | $this->cwd = $root_dir.'/../'; |
|
67 | 5 | $this->console = escapeshellarg($root_dir.DIRECTORY_SEPARATOR.'console'); |
|
68 | 5 | $this->path = $router->generate('command_exec'); |
|
69 | 5 | if ($request = $request_stack->getCurrentRequest()) { |
|
70 | 4 | $this->host = $request->getHost().':'.$request->getPort(); |
|
71 | 4 | } |
|
72 | 5 | } |
|
73 | |||
74 | /** |
||
75 | * @deprecated see self::send() |
||
76 | * |
||
77 | * @throws \InvalidArgumentException |
||
78 | * |
||
79 | * @param string $command |
||
80 | */ |
||
81 | public function exec($command) |
||
88 | |||
89 | /** |
||
90 | * Execute command. |
||
91 | * |
||
92 | * If timeout <= 0 and callback is null then command will be executed in background |
||
93 | * |
||
94 | * @param string $command |
||
95 | * @param int $timeout |
||
96 | * @param callable|null $callback |
||
97 | */ |
||
98 | public function execute($command, $timeout = 300, $callback = null) |
||
106 | |||
107 | /** |
||
108 | * @param string $command |
||
109 | * @param int $timeout |
||
110 | * @param callable|null $callback |
||
111 | */ |
||
112 | public function console($command, $timeout = 300, $callback = null) |
||
116 | |||
117 | /** |
||
118 | * @throws \RuntimeException |
||
119 | * |
||
120 | * @param string $command |
||
121 | * @param int $timeout |
||
122 | * @param callable|null $callback |
||
123 | */ |
||
124 | protected function executeCommand($command, $timeout = 300, $callback = null) |
||
125 | { |
||
126 | $process = new Process($this->prepare($command), $this->cwd, null, null, $timeout); |
||
127 | $process->run($callback); |
||
128 | if (!$process->isSuccessful()) { |
||
129 | throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', $command)); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @param string $command |
||
135 | */ |
||
136 | protected function executeCommandInBackground($command) |
||
137 | { |
||
138 | $cwd = getcwd(); |
||
139 | chdir($this->cwd); |
||
140 | |||
141 | $command = $this->prepare($command); |
||
142 | if (defined('PHP_WINDOWS_VERSION_BUILD') && function_exists('popen')) { |
||
143 | pclose(popen('start /b call '.$command, 'r')); |
||
144 | } else { |
||
145 | exec($command.' &'); |
||
146 | } |
||
147 | |||
148 | chdir($cwd); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Send the command to perform in a new thread. |
||
153 | * |
||
154 | * @param string $command |
||
155 | * @param string $host |
||
156 | */ |
||
157 | 1 | public function send($command, $host = '') |
|
158 | { |
||
159 | 1 | $host = $host ?: $this->host; |
|
160 | 1 | if (!$host) { |
|
161 | 1 | throw new \InvalidArgumentException('Unknown host that will run the command'); |
|
162 | } |
||
163 | $content = 'command='.urlencode($command); |
||
164 | |||
165 | $fp = fsockopen($this->host, 80, $errno, $errstr, self::TIMEOUT); |
||
166 | $request = 'POST '.$this->path." HTTP/1.1\r\n"; |
||
167 | $request .= 'Host: '.$host."\r\n"; |
||
168 | $request .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
||
169 | $request .= 'Content-Length: '.strlen($content)."\r\n"; |
||
170 | $request .= "Connection: Close\r\n\r\n"; |
||
171 | $request .= $content; |
||
172 | fwrite($fp, $request); |
||
173 | fclose($fp); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $command |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 3 | public function prepare($command) |
|
198 | } |
||
199 |