Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ScriptAction | |
0.00% |
0 / 82 |
|
0.00% |
0 / 10 |
1190 | |
0.00% |
0 / 1 |
| provideOptions | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| provideDescription | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| run | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| __invoke | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
| hasOption | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOption | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| provideUsage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| provideUsageOptionName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| showTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| displayUsage | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
56 | |||
| usage | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
156 | |||
| valueToString | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| secondsToDuration | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\oatbox\extension\script; |
| 23 | |
| 24 | use oat\oatbox\extension\AbstractAction; |
| 25 | use common_report_Report as Report; |
| 26 | |
| 27 | /** |
| 28 | * abstract base for extension scripts. |
| 29 | * |
| 30 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
| 31 | */ |
| 32 | abstract class ScriptAction extends AbstractAction |
| 33 | { |
| 34 | private $options; |
| 35 | private $optionsDescription; |
| 36 | |
| 37 | abstract protected function provideOptions(); |
| 38 | |
| 39 | abstract protected function provideDescription(); |
| 40 | |
| 41 | /** |
| 42 | * Run Script. |
| 43 | * |
| 44 | * Run the userland script. Implementers will use this method |
| 45 | * to implement the main logic of the script. |
| 46 | * |
| 47 | * @return \common_report_Report |
| 48 | */ |
| 49 | abstract protected function run(); |
| 50 | |
| 51 | /** |
| 52 | * Invoke |
| 53 | * |
| 54 | * This method makes the script invokable programatically. |
| 55 | * |
| 56 | * @return \common_report_Report |
| 57 | */ |
| 58 | public function __invoke($params) |
| 59 | { |
| 60 | $this->optionsDescription = $this->provideOptions(); |
| 61 | $beginScript = microtime(true); |
| 62 | |
| 63 | // Display help? |
| 64 | if ($this->displayUsage($params)) { |
| 65 | return $this->usage(); |
| 66 | } |
| 67 | |
| 68 | // Build option container. |
| 69 | try { |
| 70 | $this->options = new OptionContainer( |
| 71 | $this->optionsDescription, |
| 72 | $params |
| 73 | ); |
| 74 | } catch (\Exception $e) { |
| 75 | return new Report( |
| 76 | Report::TYPE_ERROR, |
| 77 | $e->getMessage() |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | // Run the userland script. |
| 82 | $report = $this->run(); |
| 83 | |
| 84 | $endScript = microtime(true); |
| 85 | if ($this->showTime()) { |
| 86 | $report->add( |
| 87 | new Report( |
| 88 | Report::TYPE_INFO, |
| 89 | 'Execution time: ' . self::secondsToDuration($endScript - $beginScript) |
| 90 | ) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | return $report; |
| 95 | } |
| 96 | |
| 97 | protected function hasOption($optionName) |
| 98 | { |
| 99 | return $this->options->has($optionName); |
| 100 | } |
| 101 | |
| 102 | protected function getOption($optionName) |
| 103 | { |
| 104 | return $this->options->get($optionName); |
| 105 | } |
| 106 | |
| 107 | protected function provideUsage() |
| 108 | { |
| 109 | return []; |
| 110 | } |
| 111 | |
| 112 | protected function provideUsageOptionName() |
| 113 | { |
| 114 | return 'help'; |
| 115 | } |
| 116 | |
| 117 | protected function showTime() |
| 118 | { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | private function displayUsage(array $params) |
| 123 | { |
| 124 | $usageDescription = $this->provideUsage(); |
| 125 | |
| 126 | if (!empty($usageDescription) && is_array($usageDescription)) { |
| 127 | if (!empty($usageDescription['prefix']) && in_array('-' . $usageDescription['prefix'], $params)) { |
| 128 | return true; |
| 129 | } elseif ( |
| 130 | !empty($usageDescription['longPrefix']) |
| 131 | && in_array('--' . $usageDescription['longPrefix'], $params) |
| 132 | ) { |
| 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | private function usage() |
| 141 | { |
| 142 | $report = new Report( |
| 143 | Report::TYPE_INFO, |
| 144 | $this->provideDescription() . "\n" |
| 145 | ); |
| 146 | |
| 147 | $optionsDescription = $this->optionsDescription; |
| 148 | $optionsDescription[$this->provideUsageOptionName()] = $this->provideUsage(); |
| 149 | |
| 150 | $required = new Report(Report::TYPE_INFO, 'Required Arguments:'); |
| 151 | $optional = new Report(Report::TYPE_INFO, 'Optional Arguments:'); |
| 152 | |
| 153 | foreach ($optionsDescription as $optionName => $optionParams) { |
| 154 | // Deal with prefixes. |
| 155 | $prefixes = []; |
| 156 | $optionDisplay = (!empty($optionParams['flag'])) ? '' : " ${optionName}"; |
| 157 | |
| 158 | if (!empty($optionParams['prefix'])) { |
| 159 | $prefixes[] = '-' . $optionParams['prefix'] . "${optionDisplay}"; |
| 160 | } |
| 161 | |
| 162 | if (!empty($optionParams['longPrefix'])) { |
| 163 | $prefixes[] = '--' . $optionParams['longPrefix'] . "${optionDisplay}"; |
| 164 | } |
| 165 | |
| 166 | $optionMsg = implode(', ', $prefixes); |
| 167 | if (isset($optionParams['defaultValue'])) { |
| 168 | $optionMsg .= ' (default: ' . self::valueToString($optionParams['defaultValue']) . ')'; |
| 169 | } |
| 170 | |
| 171 | $optionReport = new Report(Report::TYPE_INFO, $optionMsg); |
| 172 | |
| 173 | if (!empty($optionParams['description'])) { |
| 174 | $optionReport->add( |
| 175 | new Report(Report::TYPE_INFO, $optionParams['description']) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | $targetReport = (empty($optionParams['required'])) ? $optional : $required; |
| 180 | $targetReport->add($optionReport); |
| 181 | } |
| 182 | |
| 183 | if ($required->hasChildren()) { |
| 184 | $report->add($required); |
| 185 | } |
| 186 | |
| 187 | if ($optional->hasChildren()) { |
| 188 | $report->add($optional); |
| 189 | } |
| 190 | |
| 191 | // A little bit of formatting... |
| 192 | if ($required->hasChildren() && $optional->hasChildren()) { |
| 193 | $required->add(new Report(Report::TYPE_INFO, "")); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | return $report; |
| 198 | } |
| 199 | |
| 200 | private static function valueToString($value) |
| 201 | { |
| 202 | $string = "\"${value}\""; |
| 203 | |
| 204 | switch (gettype($value)) { |
| 205 | case 'boolean': |
| 206 | $string = ($value === true) ? 'true' : 'false'; |
| 207 | break; |
| 208 | |
| 209 | case 'integer': |
| 210 | case 'double': |
| 211 | $string = $value; |
| 212 | } |
| 213 | |
| 214 | return $string; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Seconds to Duration |
| 219 | * |
| 220 | * Format a given number of $seconds into a duration with format [hours]:[minutes]:[seconds]. |
| 221 | * |
| 222 | * @param $seconds |
| 223 | * @return string |
| 224 | */ |
| 225 | private static function secondsToDuration($seconds) |
| 226 | { |
| 227 | $seconds = intval($seconds); |
| 228 | $hours = floor($seconds / 3600); |
| 229 | $minutes = floor(($seconds / 60) % 60); |
| 230 | $seconds = $seconds % 60; |
| 231 | |
| 232 | return "${hours}h ${minutes}m {$seconds}s"; |
| 233 | } |
| 234 | } |