|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2017/6/17 |
| 6 | + * Time: 上午11:41 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace MyLib\FileParse; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class IniParser |
| 13 | + * @package MyLib\FileParse |
| 14 | + */ |
| 15 | +class IniParser extends BaseParser |
| 16 | +{ |
| 17 | + /** |
| 18 | + * parse INI |
| 19 | + * @param string $string Waiting for the parse data |
| 20 | + * @param bool $enhancement 启用增强功能,支持通过关键字 继承、导入、参考 |
| 21 | + * @param callable $pathHandler When the second param is true, this param is valid. |
| 22 | + * @param string $fileDir When the second param is true, this param is valid. |
| 23 | + * @return array |
| 24 | + * @throws \InvalidArgumentException |
| 25 | + * @throws \UnexpectedValueException |
| 26 | + */ |
| 27 | + protected static function doParse($string, $enhancement = false, callable $pathHandler = null, $fileDir = '') |
| 28 | + { |
| 29 | + if (!$string) { |
| 30 | + return []; |
| 31 | + } |
| 32 | + |
| 33 | + if (!\is_string($string)) { |
| 34 | + throw new \InvalidArgumentException('parameter type error! must is string.'); |
| 35 | + } |
| 36 | + |
| 37 | + /** @var array $array */ |
| 38 | + $array = parse_ini_string(trim($string), true); |
| 39 | + |
| 40 | + /* |
| 41 | + * Parse special keywords |
| 42 | + * |
| 43 | + * extend = ../parent.yml |
| 44 | + * db = import#../db.yml |
| 45 | + * [cache] |
| 46 | + * debug = reference#debug |
| 47 | + */ |
| 48 | + if ($enhancement === true) { |
| 49 | + if (isset($array[self::EXTEND_KEY]) && ($extendFile = $array[self::EXTEND_KEY])) { |
| 50 | + // if needed custom handle $importFile path. e.g: Maybe it uses custom alias path |
| 51 | + if ($pathHandler && \is_callable($pathHandler)) { |
| 52 | + $extendFile = $pathHandler($extendFile); |
| 53 | + } |
| 54 | + |
| 55 | + // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile |
| 56 | + if ($fileDir && !file_exists($extendFile) && $extendFile[0] !== '/') { |
| 57 | + $extendFile = $fileDir . '/' . trim($extendFile, './'); |
| 58 | + } |
| 59 | + |
| 60 | + // $importFile is file |
| 61 | + if (is_file($extendFile)) { |
| 62 | + $data = file_get_contents($extendFile); |
| 63 | + $array = array_merge(parse_ini_string(trim($data), true), $array); |
| 64 | + } else { |
| 65 | + throw new \UnexpectedValueException("needed extended file [$extendFile] don't exists!"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + foreach ($array as $key => $item) { |
| 70 | + if (!\is_string($item)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (0 === strpos($item, self::IMPORT_KEY . '#')) { |
| 75 | + $importFile = trim(substr($item, 6)); |
| 76 | + |
| 77 | + // if needed custom handle $importFile path. e.g: Maybe it uses custom alias path |
| 78 | + if ($pathHandler && \is_callable($pathHandler)) { |
| 79 | + $importFile = $pathHandler($importFile); |
| 80 | + } |
| 81 | + |
| 82 | + // if $importFile is not exists AND $importFile is not a absolute path AND have $parentFile |
| 83 | + if ($fileDir && !file_exists($importFile) && $importFile[0] !== '/') { |
| 84 | + $importFile = $fileDir . '/' . trim($importFile, './'); |
| 85 | + } |
| 86 | + |
| 87 | + // $importFile is file |
| 88 | + if (is_file($importFile)) { |
| 89 | + $data = file_get_contents($importFile); |
| 90 | + $array[$key] = parse_ini_string(trim($data), true); |
| 91 | + } else { |
| 92 | + throw new \UnexpectedValueException("needed imported file [$importFile] don't exists!"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + return $array; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments