1 | <?php |
||
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 | * @param bool $canUseProcess |
||
46 | */ |
||
47 | 2 | public function __construct(Locator $locator, $canUseProcess){ |
|
51 | |||
52 | 2 | public function run($command, $args = [], $asJson = false){ |
|
53 | 2 | if ($this->canUseProcess){ |
|
54 | 1 | $extra = $asJson ? '--output=json' : ''; |
|
55 | 1 | $cmdLine = trim($command . ' ' . $extra); |
|
56 | 1 | foreach ($args as $optionTitle => $optionValue){ |
|
57 | if (strpos($optionTitle, '--') === 0){ |
||
58 | $line = trim("$optionTitle $optionValue"); |
||
59 | } else { |
||
60 | $line = $optionValue; |
||
61 | } |
||
62 | $escapedLine = ProcessUtils::escapeArgument($line); |
||
63 | $cmdLine .= " $escapedLine"; |
||
64 | } |
||
65 | 1 | return $this->runAsProcess($cmdLine); |
|
66 | } else { |
||
67 | 1 | if ($asJson){ |
|
68 | 1 | $args['--output'] = 'json'; |
|
69 | } |
||
70 | 1 | $response = $this->runAsRequest($command, $args); |
|
71 | 1 | $decodedResponse = json_decode($response, true); |
|
72 | 1 | return $decodedResponse['response']; |
|
73 | } |
||
74 | } |
||
75 | |||
76 | 5 | public function runJson($command, $args = []){ |
|
87 | |||
88 | protected function runAsRequest($command, $args){ |
||
89 | $application = $this->getApplication(); |
||
90 | $client = new Client(); |
||
91 | $request = $client->createRequest( |
||
92 | 'POST', |
||
93 | $application->getEndpoint() . $command, |
||
94 | [ |
||
95 | 'timeout' => 0, |
||
96 | 'json' => [ |
||
97 | 'token' => $application->getAuthToken(), |
||
98 | 'params'=> $args |
||
99 | ] |
||
100 | ] |
||
101 | ); |
||
102 | |||
103 | $response = $client->send($request); |
||
104 | $responseBody = $response->getBody()->getContents(); |
||
105 | return $responseBody; |
||
106 | } |
||
107 | |||
108 | protected function getApplication(){ |
||
113 | |||
114 | protected function runAsProcess($cmdLine){ |
||
126 | } |
||
127 |