Completed
Pull Request — master (#355)
by Victor
02:51
created

OccRunner::runAsProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Utils;
23
24
use GuzzleHttp\Client;
25
use Owncloud\Updater\Console\Application;
26
use Symfony\Component\Process\Process;
27
use Symfony\Component\Process\ProcessUtils;
28
use Symfony\Component\Process\Exception\ProcessFailedException;
29
use Owncloud\Updater\Utils\Locator;
30
31
class OccRunner {
32
	/**
33
	 * @var Locator $locator
34
	 */
35
	protected $locator;
36
37
	/**
38
	 * @var bool
39
	 */
40
	protected $canUseProcess;
41
42
	/**
43
	 *
44
	 * @param Locator $locator
45
	 */
46
	public function __construct(Locator $locator){
47
		$this->locator = $locator;
48
49
		$disabled = explode(',', ini_get('disable_functions'));
50
		$this->canUseProcess = function_exists('proc_open') && !in_array('proc_open', $disabled);
51
		// TODO: remove
52
		$this->canUseProcess = false;
53
	}
54
55
	public function run($command, $args = [], $extra = ''){
56
		if ($this->canUseProcess){
57
			$cmdLine = trim($command . ' ' . $extra);
58
			foreach ($args as $optionTitle => $optionValue){
59
				if (strpos($optionTitle, '--') === 0){
60
					$line = trim("$optionTitle $optionValue");
61
				} else {
62
					$line = $optionValue;
63
				}
64
				$escapedLine = ProcessUtils::escapeArgument($line);
65
				$cmdLine .= " $escapedLine";
66
			}
67
			return $this->runAsProcess($cmdLine);
68
		} else {
69
			return $this->runAsRequest($command, $args);
70
		}
71
	}
72
73 3
	public function runJson($command, $args = []){
74 3
		$plain = $this->run($command, $args, '--output=json');
75 3
		$decoded = json_decode($plain, true);
76 3
		if (!is_array($decoded)){
77
			throw new \UnexpectedValueException('Could not parse a response for ' . $command . '. Please check if the current shell user can run occ command. Raw output: ' . PHP_EOL . $plain);
78
		}
79 3
		return $decoded;
80
	}
81
82
	private function runAsRequest($command, $args){
83
		$container = Application::$container;
84
		$application = $container['application'];
85
		$client = new Client();
86
		$request = $client->createRequest(
87
			'POST',
88
			$application->getEndpoint() . $command,
89
			[
90
				'timeout' => 0
91
			]
92
		);
93
94
		$query = $request->getQuery();
95
		foreach ($args as $optionTitle => $optionValue){
96
			$query[$optionTitle] = $optionValue;
97
		}
98
		$response = $client->send($request);
99
		return $response->getBody()->getContents();
100
	}
101
102
	private function runAsProcess($cmdLine){
103
		$occPath = $this->locator->getPathToOccFile();
104
		$cmd = "php $occPath --no-warnings $cmdLine";
105
		$process = new Process($cmd);
106
		$process->setTimeout(null);
107
		$process->run();
108
109
		if (!$process->isSuccessful()){
110
			throw new ProcessFailedException($process);
111
		}
112
		return $process->getOutput();
113
	}
114
}
115