Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
55.56% |
10 / 18 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
Config | |
55.56% |
10 / 18 |
|
25.00% |
1 / 4 |
13.62 | |
0.00% |
0 / 1 |
getBool | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getStr | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
4.68 | |||
getDate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
load | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 |
1 | <?php |
2 | /** |
3 | * @section LICENSE |
4 | * This file is part of Wikimedia Slim application library |
5 | * |
6 | * Wikimedia Slim application library is free software: you can |
7 | * redistribute it and/or modify it under the terms of the GNU General Public |
8 | * License as published by the Free Software Foundation, either version 3 of |
9 | * the License, or (at your option) any later version. |
10 | * |
11 | * Wikimedia Slim application library is distributed in the hope that it |
12 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with Wikimedia Grants Review application. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * @file |
21 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
22 | */ |
23 | |
24 | namespace Wikimedia\Slimapp; |
25 | |
26 | use InvalidArgumentException; |
27 | use const FILTER_FLAG_STRIP_HIGH; |
28 | use const FILTER_FLAG_STRIP_LOW; |
29 | use const FILTER_NULL_ON_FAILURE; |
30 | use const FILTER_SANITIZE_STRING; |
31 | use const FILTER_VALIDATE_BOOLEAN; |
32 | |
33 | /** |
34 | * Configuration registry. |
35 | * |
36 | * @author Bryan Davis <bd808@wikimedia.org> |
37 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
38 | */ |
39 | class Config { |
40 | |
41 | /** |
42 | * Get a boolean value |
43 | * @param string $name Setting name |
44 | * @param bool $default Default value if none found |
45 | * @return bool Value |
46 | */ |
47 | public static function getBool( $name, $default = false ) { |
48 | $var = getenv( $name ); |
49 | $val = filter_var( $var, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); |
50 | return $val ?? $default; |
51 | } |
52 | |
53 | /** |
54 | * Get a string value |
55 | * @param string $name Setting name |
56 | * @param string $default Default value if none found |
57 | * @return string Value |
58 | */ |
59 | public static function getStr( $name, $default = '' ) { |
60 | $var = getenv( $name ); |
61 | if ( $var !== false ) { |
62 | $var = filter_var( $var, |
63 | FILTER_SANITIZE_STRING, |
64 | FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH |
65 | ); |
66 | } |
67 | return ( $var === false ) ? $default : $var; |
68 | } |
69 | |
70 | /** |
71 | * Get a date value |
72 | * @param string $name Setting name |
73 | * @return int|bool Unix timestamp or false if not found |
74 | */ |
75 | public static function getDate( $name ) { |
76 | return strtotime( self::getStr( $name ) ); |
77 | } |
78 | |
79 | /** |
80 | * Load configuration data from file |
81 | * |
82 | * Reads ini file style configuration settings from the given file and |
83 | * loads the values into the application's environment. This is useful in |
84 | * deployments where the use of the container environment for configuration |
85 | * is discouraged. |
86 | * |
87 | * @param string $file Path to config file |
88 | */ |
89 | public static function load( $file ) { |
90 | if ( !is_readable( $file ) ) { |
91 | throw new InvalidArgumentException( "File '{$file}' is not readable." ); |
92 | } |
93 | |
94 | $settings = parse_ini_file( $file ); |
95 | |
96 | foreach ( $settings as $key => $value ) { |
97 | // Store in super globals |
98 | $_ENV[$key] = $value; |
99 | $_SERVER[$key] = $value; |
100 | |
101 | // Also store in process env vars |
102 | putenv( "{$key}={$value}" ); |
103 | } |
104 | } |
105 | |
106 | } |