1 | <?php |
||
35 | class OccRunner { |
||
36 | /** |
||
37 | * @var Locator $locator |
||
38 | */ |
||
39 | protected $locator; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $canUseProcess; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @param Locator $locator |
||
49 | * @param bool $canUseProcess |
||
50 | */ |
||
51 | 2 | public function __construct(Locator $locator, $canUseProcess){ |
|
55 | |||
56 | /** |
||
57 | * @param bool $canUseProcess |
||
58 | */ |
||
59 | public function setCanUseProcess($canUseProcess){ |
||
62 | |||
63 | /** |
||
64 | * @param $command |
||
65 | * @param array $args |
||
66 | * @param bool $asJson |
||
67 | * @return string |
||
68 | */ |
||
69 | 2 | public function run($command, $args = [], $asJson = false){ |
|
70 | 2 | if ($this->canUseProcess){ |
|
71 | 1 | $extra = $asJson ? '--output=json' : ''; |
|
72 | 1 | $cmdLine = trim($command . ' ' . $extra); |
|
73 | 1 | foreach ($args as $optionTitle => $optionValue){ |
|
74 | if (strpos($optionTitle, '--') === 0){ |
||
75 | $line = trim("$optionTitle=$optionValue"); |
||
76 | } else { |
||
77 | $line = $optionValue; |
||
78 | } |
||
79 | $escapedLine = ProcessUtils::escapeArgument($line); |
||
80 | $cmdLine .= " $escapedLine"; |
||
81 | 1 | } |
|
82 | 1 | return $this->runAsProcess($cmdLine); |
|
83 | } else { |
||
84 | 1 | if ($asJson){ |
|
85 | 1 | $args['--output'] = 'json'; |
|
86 | 1 | } |
|
87 | 1 | $response = $this->runAsRequest($command, $args); |
|
88 | 1 | $decodedResponse = json_decode($response, true); |
|
89 | 1 | return $decodedResponse['response']; |
|
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param $command |
||
95 | * @param array $args |
||
96 | * @return mixed |
||
97 | */ |
||
98 | 5 | public function runJson($command, $args = []){ |
|
109 | |||
110 | /** |
||
111 | * @param $command |
||
112 | * @param $args |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function runAsRequest($command, $args){ |
||
116 | $application = $this->getApplication(); |
||
117 | $client = new Client(); |
||
118 | $endpointBase = $application->getEndpoint(); |
||
119 | $params = [ |
||
120 | 'timeout' => 0, |
||
121 | 'json' => [ |
||
122 | 'token' => $application->getAuthToken(), |
||
123 | 'params'=> $args |
||
124 | ] |
||
125 | ]; |
||
126 | |||
127 | // Skip SSL validation for localhost only as localhost never has a valid cert |
||
128 | if (preg_match('/^https:\/\/localhost\/.*/i', $endpointBase)){ |
||
129 | $params['verify'] = false; |
||
130 | } |
||
131 | |||
132 | $request = $client->createRequest( |
||
133 | 'POST', |
||
134 | $endpointBase . $command, |
||
135 | $params |
||
136 | ); |
||
137 | |||
138 | $response = $client->send($request); |
||
139 | $responseBody = $response->getBody()->getContents(); |
||
140 | return $responseBody; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return mixed |
||
145 | */ |
||
146 | protected function getApplication(){ |
||
151 | |||
152 | /** |
||
153 | * @param $cmdLine |
||
154 | * @return string |
||
155 | */ |
||
156 | protected function runAsProcess($cmdLine){ |
||
168 | } |
||
169 |