Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Mode | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
| is | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| isMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getModeByName | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| getCurrentMode | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2014-2023 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * Helps you to check in which mode the current TAO instance states: production, development. |
| 23 | * The purpose is to wrap the use of DEBUG_MODE to replace this system in the future... |
| 24 | * |
| 25 | * @todo Enable more than those 2 modes |
| 26 | * |
| 27 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 28 | * |
| 29 | * @package tao |
| 30 | * |
| 31 | * phpcs:disable Squiz.Classes.ValidClassName |
| 32 | */ |
| 33 | class tao_helpers_Mode |
| 34 | { |
| 35 | /** |
| 36 | * Development mode |
| 37 | */ |
| 38 | public const DEVELOPMENT = 2; |
| 39 | |
| 40 | /** |
| 41 | * Alias for development |
| 42 | */ |
| 43 | public const DEBUG = 2; |
| 44 | |
| 45 | /** |
| 46 | * Production mode |
| 47 | */ |
| 48 | public const PRODUCTION = 3; |
| 49 | |
| 50 | /** |
| 51 | * @var int the current mode |
| 52 | */ |
| 53 | private static $currentMode; |
| 54 | |
| 55 | /** |
| 56 | * Check the TAO instance current mode |
| 57 | * |
| 58 | * @example tao_helpers_Mode::is('production') |
| 59 | * |
| 60 | * @param int|string $mode |
| 61 | * |
| 62 | * @return boolean |
| 63 | */ |
| 64 | public static function is($mode) |
| 65 | { |
| 66 | if (is_int($mode) && self::get() == $mode) { |
| 67 | return true; |
| 68 | } |
| 69 | if (is_string($mode) && self::get() == self::getModeByName($mode)) { |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | public function isMode($mode): bool |
| 76 | { |
| 77 | return self::is($mode); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the current mode |
| 82 | * |
| 83 | * @example (tao_helpers_Mode::get() == tao_helpers_Mode::DEVELOPMENT) |
| 84 | * |
| 85 | * @return int matching the constants |
| 86 | */ |
| 87 | public static function get() |
| 88 | { |
| 89 | if (empty(self::$currentMode)) { |
| 90 | self::$currentMode = self::getCurrentMode(); |
| 91 | } |
| 92 | return self::$currentMode; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the mode constant by name |
| 97 | * |
| 98 | * @param string $name the mode name |
| 99 | * |
| 100 | * @return int|boolean false if not exists |
| 101 | */ |
| 102 | public static function getModeByName($name) |
| 103 | { |
| 104 | switch (strtolower($name)) { |
| 105 | case 'development': |
| 106 | case 'debug': |
| 107 | return self::DEVELOPMENT; |
| 108 | case 'production': |
| 109 | return self::PRODUCTION; |
| 110 | default: |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Reads the current value of DEBUG_MODE |
| 117 | * |
| 118 | * @throws common_Exception |
| 119 | * |
| 120 | * @return int matching the constants |
| 121 | */ |
| 122 | private static function getCurrentMode() |
| 123 | { |
| 124 | //read the mode from variable DEBUG_MODE |
| 125 | if (!defined('DEBUG_MODE')) { |
| 126 | throw new common_Exception('The DEBUG MODE constant is not defined, it should never occurs'); |
| 127 | } |
| 128 | |
| 129 | if (DEBUG_MODE == true) { |
| 130 | return self::DEVELOPMENT; |
| 131 | } |
| 132 | return self::PRODUCTION; |
| 133 | } |
| 134 | } |