Passed
Push — master ( c1e721...8ec156 )
by 世昌
02:05
created

ConfigLoader::resolve()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 10
nc 8
nop 1
dl 0
loc 17
rs 8.4444
c 0
b 0
f 0
1
<?php
2
namespace suda\application\config;
3
4
use suda\application\exception\JSONException;
5
use suda\application\exception\YamlException;
6
7
/**
8
 * 文件配置类
9
 */
10
abstract class ConfigLoader
11
{
12
    public function loadConfig(string $path, array $extra =[]):?array
13
    {
14
        $data=null;
15
        if (!file_exists($path)) {
16
            $path = static::resolve($path);
17
        }
18
        if ($path) {
19
            $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
20
            switch ($ext) {
21
                case 'yml': case 'yaml': $data = $this->loadYaml($path, $extra); break;
22
                case 'php': $data = include $path; break;
23
                case 'ini': $data = $this->loadIni($path, $extra); break;
24
                case 'json': default: $data = $this->loadJson($path, $extra);
25
            }
26
        }
27
        return $data;
28
    }
29
30
    protected function loadJson(string $path, array $extra =[]):array
31
    {
32
        $content = file_get_contents($path);
33
        $content =$this->parseValue($content, $extra);
34
        $data = json_decode($content, true);
35
        if (json_last_error()!==JSON_ERROR_NONE) {
36
            throw new JSONException(json_last_error());
37
        }
38
        return $data;
39
    }
40
41
    protected function loadIni(string $path, array $extra =[]):array
42
    {
43
        $content = file_get_contents($path);
44
        $content =$this->parseValue($content, $extra);
45
        return \parse_ini_string($content, true) ?: [];
46
    }
47
48
    protected function loadYaml(string $path, array $extra =[]):array
49
    {
50
        if (function_exists('yaml_parse')) {
51
            $name = 'yaml_parse';
52
        } elseif (class_exists('Spyc')) {
53
            $name = 'Spyc::YAMLLoadString';
54
        } else {
55
            throw new YamlException("parse yaml config error : missing yaml extension or spyc", 1);
56
        }
57
        $content = file_get_contents($path);
58
        $content =$this->parseValue($content, $extra);
59
        return \call_user_func_array($name, [$content]);
60
    }
61
62
    abstract public function parseValue(string $content, array $extra =[]):string;
63
64
    public  static function resolve(string $path):?string
65
    {
66
        if (file_exists($path)) {
67
            return $path;
68
        }
69
        $basepath = dirname($path).'/'.pathinfo($path, PATHINFO_FILENAME);
70
        if (file_exists($conf = $basepath.'.yml')  || file_exists($conf = $basepath.'.yaml')) {
71
            if (function_exists('yaml_parse') || class_exists('Spyc')) {
72
                return $conf;
73
            }
74
        }
75
        foreach (['.json','.php','.ini'] as $ext) {
76
            if (file_exists($conf=$basepath.$ext)) {
77
                return $conf;
78
            }
79
        }
80
        return null;
81
    }
82
}
83