Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Verbose | |
0.00% |
0 / 16 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| setOutputColorVisibility | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| getMinimumLogLevel | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| load | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| hasParameter | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| 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; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\cliArgument\argument\implementation\verbose; |
| 23 | |
| 24 | use oat\oatbox\action\Action; |
| 25 | use oat\oatbox\log\ColoredVerboseLogger; |
| 26 | use oat\oatbox\log\LoggerService; |
| 27 | use oat\oatbox\log\VerboseLogger; |
| 28 | use oat\oatbox\PhpSerializable; |
| 29 | use oat\oatbox\PhpSerializeStateless; |
| 30 | use oat\tao\model\cliArgument\argument\Argument; |
| 31 | use Psr\Log\LoggerAwareInterface; |
| 32 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 33 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 34 | |
| 35 | abstract class Verbose implements Argument, ServiceLocatorAwareInterface, PhpSerializable |
| 36 | { |
| 37 | use ServiceLocatorAwareTrait; |
| 38 | use PhpSerializeStateless; |
| 39 | |
| 40 | /** |
| 41 | * @var bool |
| 42 | */ |
| 43 | protected $hideColors = false; |
| 44 | |
| 45 | /** |
| 46 | * Sets the output color's visibility. |
| 47 | * |
| 48 | * @param array $params |
| 49 | */ |
| 50 | protected function setOutputColorVisibility(array $params) |
| 51 | { |
| 52 | if ($this->hasParameter($params, '-nc') || $this->hasParameter($params, '--no-color')) { |
| 53 | $this->hideColors = true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return the Psr3 logger level minimum to send log to logger |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | abstract protected function getMinimumLogLevel(); |
| 63 | |
| 64 | /** |
| 65 | * Propagate the argument process to Action |
| 66 | * To load a verbose logger, a check is done Action interfaces to find LoggerInterface |
| 67 | * The verbose logger is loaded with the minimum level requires |
| 68 | * |
| 69 | * @param Action $action |
| 70 | */ |
| 71 | public function load(Action $action) |
| 72 | { |
| 73 | if ($action instanceof LoggerAwareInterface) { |
| 74 | $action->setLogger( |
| 75 | $this->getServiceLocator()->get(LoggerService::SERVICE_ID) |
| 76 | ->addLogger( |
| 77 | $this->hideColors |
| 78 | ? new VerboseLogger($this->getMinimumLogLevel()) |
| 79 | : new ColoredVerboseLogger($this->getMinimumLogLevel()) |
| 80 | ) |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Find a parameter $name into $params arguments |
| 87 | * If $value is defined, check if following parameter equals to given $value |
| 88 | * |
| 89 | * @param array $params |
| 90 | * @param $name |
| 91 | * @param null $value |
| 92 | * @return bool |
| 93 | */ |
| 94 | protected function hasParameter(array $params, $name, $value = null) |
| 95 | { |
| 96 | $found = in_array($name, $params); |
| 97 | if (is_null($value) || !$found) { |
| 98 | return $found; |
| 99 | } |
| 100 | $paramValueIndex = array_search($name, $params) + 1; |
| 101 | return isset($params[$paramValueIndex]) && ($params[$paramValueIndex] == $value); |
| 102 | } |
| 103 | } |