fuelphp /
config
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 | * @package Fuel\Config |
||
| 4 | * @version 2.0 |
||
| 5 | * @author Fuel Development Team |
||
| 6 | * @license MIT License |
||
| 7 | * @copyright 2010 - 2015 Fuel Development Team |
||
| 8 | * @link http://fuelphp.com |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Fuel\Config; |
||
| 12 | |||
| 13 | use Fuel\Common\Arr; |
||
| 14 | use Fuel\FileSystem\Finder; |
||
| 15 | use Fuel\Common\DataContainer; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Holds configuration data |
||
| 19 | */ |
||
| 20 | class Container extends DataContainer implements ContainerInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $environment; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var Finder |
||
| 29 | */ |
||
| 30 | protected $finder; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $handlers; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $configFolder = 'config'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $defaultFormat = 'php'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $environment |
||
| 49 | * @param Finder $finder |
||
| 50 | * @param string $defaultFormat |
||
| 51 | */ |
||
| 52 | 15 | public function __construct($environment = null, $finder = null, $defaultFormat = 'php') |
|
| 53 | { |
||
| 54 | 15 | if ($environment instanceof Finder) |
|
| 55 | { |
||
| 56 | 1 | $finder = $environment; |
|
| 57 | 1 | $environment = null; |
|
| 58 | } |
||
| 59 | |||
| 60 | 15 | if ( ! $finder) |
|
| 61 | { |
||
| 62 | 14 | $finder = new Finder(); |
|
| 63 | } |
||
| 64 | |||
| 65 | 15 | if ($environment) |
|
|
0 ignored issues
–
show
|
|||
| 66 | { |
||
| 67 | 3 | $this->setEnvironment($environment); |
|
| 68 | } |
||
| 69 | |||
| 70 | 15 | $this->defaultFormat = $defaultFormat; |
|
| 71 | 15 | $this->finder = $finder; |
|
| 72 | 15 | } |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the default format |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 1 | public function getDefaultFormat() |
|
| 80 | { |
||
| 81 | 1 | return $this->defaultFormat; |
|
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets the default format |
||
| 86 | * |
||
| 87 | * @param string $format |
||
| 88 | */ |
||
| 89 | 1 | public function setDefaultFormat($format) |
|
| 90 | { |
||
| 91 | 1 | $this->defaultFormat = $format; |
|
| 92 | 1 | } |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Ensures a default config format |
||
| 96 | * |
||
| 97 | * @param string $file |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | 10 | public function ensureDefaultFormat($file) |
|
| 102 | { |
||
| 103 | 10 | if ( ! pathinfo($file, PATHINFO_EXTENSION)) |
|
| 104 | { |
||
| 105 | 6 | $file .= '.'.$this->defaultFormat; |
|
| 106 | } |
||
| 107 | |||
| 108 | 10 | return empty($this->configFolder) ? $file : $this->configFolder.DIRECTORY_SEPARATOR.$file; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the environment |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | 1 | public function getEnvironment() |
|
| 117 | { |
||
| 118 | 1 | return $this->environment; |
|
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Sets the environment |
||
| 123 | * |
||
| 124 | * @param string $environment |
||
| 125 | */ |
||
| 126 | 3 | public function setEnvironment($environment) |
|
| 127 | { |
||
| 128 | 3 | if ($environment) |
|
| 129 | { |
||
| 130 | 3 | $environment = trim($environment, DIRECTORY_SEPARATOR); |
|
| 131 | } |
||
| 132 | |||
| 133 | 3 | $this->environment = $environment; |
|
| 134 | 3 | } |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Unloads a config group |
||
| 138 | * |
||
| 139 | * @param string $group |
||
| 140 | */ |
||
| 141 | 1 | public function unload($group) |
|
| 142 | { |
||
| 143 | 1 | $this->delete($group); |
|
| 144 | 1 | } |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Reloads a group |
||
| 148 | * |
||
| 149 | * @param string $name |
||
| 150 | * @param string|boolean $group |
||
| 151 | * |
||
| 152 | * @return array|null |
||
| 153 | */ |
||
| 154 | 1 | public function reload($name, $group = true) |
|
| 155 | { |
||
| 156 | 1 | if ($group === true) |
|
| 157 | { |
||
| 158 | 1 | $group = pathinfo($name, PATHINFO_FILENAME); |
|
| 159 | } |
||
| 160 | |||
| 161 | 1 | $this->delete($group); |
|
| 162 | |||
| 163 | 1 | return $this->load($name, $group); |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Loads a config file |
||
| 168 | * |
||
| 169 | * @param string $name |
||
| 170 | * @param null|string|boolean $group |
||
| 171 | * |
||
| 172 | * @return array|null |
||
| 173 | */ |
||
| 174 | 8 | public function load($name, $group = null) |
|
| 175 | { |
||
| 176 | 8 | if ($group === true) |
|
| 177 | { |
||
| 178 | 6 | $group = pathinfo($name, PATHINFO_FILENAME); |
|
| 179 | } |
||
| 180 | |||
| 181 | 8 | if ($group and $cached = $this->get($group)) |
|
| 182 | { |
||
| 183 | 1 | return $cached; |
|
| 184 | } |
||
| 185 | |||
| 186 | 8 | $name = $this->ensureDefaultFormat($name); |
|
| 187 | 8 | $paths = $this->finder->findAllFiles($name); |
|
| 188 | |||
| 189 | 8 | if (empty($paths)) |
|
| 190 | { |
||
| 191 | 1 | return false; |
|
| 192 | } |
||
| 193 | |||
| 194 | 7 | $config = array(); |
|
| 195 | |||
| 196 | 7 | foreach ($paths as $path) |
|
| 197 | { |
||
| 198 | 7 | $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 199 | 7 | $handler = $this->getHandler($extension); |
|
| 200 | 7 | $config = Arr::merge($config, $handler->load($path)); |
|
| 201 | } |
||
| 202 | |||
| 203 | 7 | if ($group) |
|
| 204 | { |
||
| 205 | 7 | $this->set($group, $config); |
|
| 206 | } |
||
| 207 | elseif ($group === null) |
||
| 208 | { |
||
| 209 | $this->merge($config); |
||
| 210 | } |
||
| 211 | |||
| 212 | 7 | return $config; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Stores a config file |
||
| 217 | * |
||
| 218 | * @param string $group |
||
| 219 | * @param string|null $destination |
||
| 220 | * |
||
| 221 | * @throws \RuntimeException |
||
| 222 | */ |
||
| 223 | 6 | public function save($group, $destination = null) |
|
| 224 | { |
||
| 225 | 6 | if ($destination === null) |
|
| 226 | { |
||
| 227 | 2 | $destination = $group; |
|
| 228 | } |
||
| 229 | |||
| 230 | 6 | if ( ! $this->has($group)) |
|
| 231 | { |
||
| 232 | 1 | throw new \RuntimeException('Unable to save non-existig config group: '.$group); |
|
| 233 | } |
||
| 234 | |||
| 235 | 5 | $destination = $this->ensureDefaultFormat($destination); |
|
| 236 | 5 | $format = pathinfo($destination, PATHINFO_EXTENSION); |
|
| 237 | 5 | $handler = $this->getHandler($format); |
|
| 238 | 5 | $data = $this->get($group); |
|
| 239 | 5 | $output = $handler->format($data); |
|
| 240 | 4 | $path = $this->findDestination($destination); |
|
| 241 | |||
| 242 | 4 | if ( ! $path) |
|
| 243 | { |
||
| 244 | 1 | throw new \RuntimeException(sprintf('Could not save group "%" as "%s".', $group, $destination)); |
|
| 245 | } |
||
| 246 | |||
| 247 | 3 | return file_put_contents($path, $output); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Finds a config file |
||
| 252 | * |
||
| 253 | * @param string $destination |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 5 | public function findDestination($destination) |
|
| 258 | { |
||
| 259 | 5 | if (is_file($destination)) |
|
| 260 | { |
||
| 261 | 1 | return $destination; |
|
| 262 | } |
||
| 263 | |||
| 264 | 5 | if ($location = $this->finder->findFileReversed($destination)) |
|
| 265 | { |
||
| 266 | 1 | return $location; |
|
| 267 | } |
||
| 268 | |||
| 269 | 5 | $paths = $this->finder->getPaths(); |
|
| 270 | |||
| 271 | 5 | if (empty($paths)) |
|
| 272 | { |
||
| 273 | 2 | return false; |
|
| 274 | } |
||
| 275 | |||
| 276 | 3 | $last = end($paths); |
|
| 277 | |||
| 278 | 3 | return $last.ltrim($destination, DIRECTORY_SEPARATOR); |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Retrieves the handler for a file type |
||
| 283 | * |
||
| 284 | * @param string $extension |
||
| 285 | * |
||
| 286 | * @return Handler |
||
| 287 | * |
||
| 288 | * @throws \RuntimeException |
||
| 289 | */ |
||
| 290 | 11 | public function getHandler($extension) |
|
| 291 | { |
||
| 292 | 11 | if (isset($this->handlers[$extension])) |
|
| 293 | { |
||
| 294 | 6 | return $this->handlers[$extension]; |
|
| 295 | } |
||
| 296 | |||
| 297 | 10 | $class = 'Fuel\Config\Handler\\'.ucfirst($extension); |
|
| 298 | |||
| 299 | 10 | if ( ! class_exists($class, true)) |
|
| 300 | { |
||
| 301 | 1 | throw new \RuntimeException('Could not find config handler for extension: '.$extension); |
|
| 302 | } |
||
| 303 | |||
| 304 | 9 | $handler = new $class; |
|
| 305 | 9 | $this->handlers[$extension] = $handler; |
|
| 306 | |||
| 307 | 9 | return $handler; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Sets a handler for an extension |
||
| 312 | * |
||
| 313 | * @param string $extension |
||
| 314 | * @param Handler $handler |
||
| 315 | */ |
||
| 316 | 1 | public function setHandler($extension, Handler $loader) |
|
| 317 | { |
||
| 318 | 1 | $this->handlers[$extension] = $loader; |
|
| 319 | 1 | } |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Sets the config folder |
||
| 323 | * |
||
| 324 | * @param string $folder |
||
| 325 | */ |
||
| 326 | 6 | public function setConfigFolder($folder) |
|
| 327 | { |
||
| 328 | 6 | $this->configFolder = rtrim($folder, DIRECTORY_SEPARATOR); |
|
| 329 | 6 | } |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Adds a path |
||
| 333 | * |
||
| 334 | * @param string $path |
||
| 335 | * |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | 8 | View Code Duplication | public function addPath($path) |
| 339 | { |
||
| 340 | 8 | $path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
|
| 341 | 8 | $this->finder->addPath($path); |
|
| 342 | |||
| 343 | 8 | if ($this->environment) |
|
| 344 | { |
||
| 345 | 2 | $this->finder->addPath($path.$this->environment); |
|
| 346 | } |
||
| 347 | |||
| 348 | 8 | return $this; |
|
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Adds paths to look in |
||
| 353 | * |
||
| 354 | * @param array $paths |
||
| 355 | */ |
||
| 356 | 1 | public function addPaths(array $paths) |
|
| 357 | { |
||
| 358 | 1 | array_map(array($this, 'addPath'), $paths); |
|
| 359 | 1 | } |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Removes a path |
||
| 363 | * |
||
| 364 | * @param string $path |
||
| 365 | */ |
||
| 366 | 1 | View Code Duplication | public function removePath($path) |
| 367 | { |
||
| 368 | 1 | $path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
|
| 369 | 1 | $this->finder->removePath($path); |
|
| 370 | |||
| 371 | 1 | if ($this->environment) |
|
| 372 | { |
||
| 373 | 1 | $this->finder->removePath($path.$this->environment); |
|
| 374 | } |
||
| 375 | 1 | } |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Removes paths |
||
| 379 | * |
||
| 380 | * @param array $paths |
||
| 381 | */ |
||
| 382 | 1 | public function removePaths(array $paths) |
|
| 383 | { |
||
| 384 | 1 | array_map(array($this, 'removePath'), $paths); |
|
| 385 | 1 | } |
|
| 386 | } |
||
| 387 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: