slickframework /
configuration
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of Configuration |
||
| 5 | * |
||
| 6 | * For the full copyright and license information, please view the LICENSE |
||
| 7 | * file that was distributed with this source code. |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace Slick\Configuration\Driver; |
||
| 11 | |||
| 12 | use Slick\Configuration\ConfigurationInterface; |
||
| 13 | use Slick\Configuration\Exception\ParserErrorException; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Ini configuration driver |
||
| 17 | * |
||
| 18 | * @package Slick\Configuration\Driver |
||
| 19 | */ |
||
| 20 | class Ini implements ConfigurationInterface |
||
| 21 | { |
||
| 22 | |||
| 23 | use CommonDriverMethods; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | 6 | */ |
|
| 28 | private string $lastError = ''; |
||
| 29 | 6 | ||
| 30 | 6 | /** |
|
| 31 | 3 | * Creates an ini configuration driver |
|
| 32 | 3 | * |
|
| 33 | 3 | * @param string $filePath |
|
| 34 | */ |
||
| 35 | 3 | public function __construct(string $filePath) |
|
| 36 | 3 | { |
|
| 37 | $this->checkFile($filePath); |
||
| 38 | |||
| 39 | $this->loadSettings($filePath); |
||
| 40 | |||
| 41 | if (!is_array($this->data)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 42 | throw new ParserErrorException( |
||
| 43 | "Parse error: $this->lastError" |
||
| 44 | ); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | private function loadSettings(string $filePath): void |
||
| 49 | { |
||
| 50 | set_error_handler(function (int $errorNumber, string $message): bool { |
||
| 51 | $this->lastError = "$errorNumber: $message"; |
||
| 52 | return true; |
||
| 53 | }); |
||
| 54 | $parsedFile = parse_ini_file($filePath, true, INI_SCANNER_TYPED); |
||
| 55 | $this->data = is_array($parsedFile) ? $parsedFile : 0; |
||
|
0 ignored issues
–
show
|
|||
| 56 | restore_error_handler(); |
||
| 57 | } |
||
| 58 | } |
||
| 59 |