arrilot /
dotenv-php
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Arrilot\DotEnv; |
||
| 4 | |||
| 5 | use Arrilot\DotEnv\Exceptions\MissingVariableException; |
||
| 6 | |||
| 7 | class DotEnv |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Key-value storage. |
||
| 11 | * |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected static $variables = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Required variables. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $required = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Were variables loaded? |
||
| 25 | * |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected static $isLoaded = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Load .env.php file or array. |
||
| 32 | * |
||
| 33 | * @param string|array $source |
||
| 34 | * |
||
| 35 | * @return void |
||
| 36 | */ |
||
| 37 | public static function load($source) |
||
| 38 | { |
||
| 39 | self::$variables = is_array($source) ? $source : require $source; |
||
| 40 | self::$isLoaded = true; |
||
| 41 | |||
| 42 | self::checkRequiredVariables(); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Copy all variables to putenv(). |
||
| 47 | * |
||
| 48 | * @param string $prefix |
||
| 49 | */ |
||
| 50 | public static function copyVarsToPutenv($prefix = 'PHP_') |
||
| 51 | { |
||
| 52 | foreach (self::all() as $key => $value) { |
||
| 53 | if (is_object($value) || is_array($value)) { |
||
| 54 | $value = serialize($value); |
||
| 55 | } |
||
| 56 | |||
| 57 | putenv("{$prefix}{$key}={$value}"); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Copy all variables to $_ENV. |
||
| 63 | */ |
||
| 64 | public static function copyVarsToEnv() |
||
|
0 ignored issues
–
show
|
|||
| 65 | { |
||
| 66 | foreach (self::all() as $key => $value) { |
||
| 67 | $_ENV[$key] = $value; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Copy all variables to $_SERVER. |
||
| 73 | */ |
||
| 74 | public static function copyVarsToServer() |
||
|
0 ignored issues
–
show
copyVarsToServer uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 75 | { |
||
| 76 | foreach (self::all() as $key => $value) { |
||
| 77 | $_SERVER[$key] = $value; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get env variables. |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public static function all() |
||
| 87 | { |
||
| 88 | return self::$variables; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get env variable. |
||
| 93 | * |
||
| 94 | * @param string $key |
||
| 95 | * @param mixed $default |
||
| 96 | * |
||
| 97 | * @return mixed |
||
| 98 | */ |
||
| 99 | public static function get($key, $default = null) |
||
| 100 | { |
||
| 101 | return isset(self::$variables[$key]) ? self::$variables[$key] : $default; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Set env variable. |
||
| 106 | * |
||
| 107 | * @param string|array $keys |
||
| 108 | * @param mixed $value |
||
| 109 | * |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public static function set($keys, $value = null) |
||
| 113 | { |
||
| 114 | if (is_array($keys)) { |
||
| 115 | self::$variables = array_merge(self::$variables, $keys); |
||
| 116 | } else { |
||
| 117 | self::$variables[$keys] = $value; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set required variables. |
||
| 123 | * |
||
| 124 | * @param array $variables |
||
| 125 | */ |
||
| 126 | public static function setRequired(array $variables) |
||
| 127 | { |
||
| 128 | self::$required = $variables; |
||
| 129 | |||
| 130 | if (self::$isLoaded) { |
||
| 131 | self::checkRequiredVariables(); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Delete all variables. |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | public static function flush() |
||
| 141 | { |
||
| 142 | self::$variables = []; |
||
| 143 | self::$isLoaded = false; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Throw exception if any of required variables was not loaded. |
||
| 148 | * |
||
| 149 | * @throws MissingVariableException |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | protected static function checkRequiredVariables() |
||
| 154 | { |
||
| 155 | foreach (self::$required as $key) { |
||
| 156 | if (!isset(self::$variables[$key])) { |
||
| 157 | throw new MissingVariableException(".env variable '{$key}' is missing"); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: