Passed
Push — master ( 5d2787...c547da )
by 世昌
03:12
created

PathResolver::resolveExtensions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\framework\server\config;
3
4
/**
5
 * 路径解析器
6
 * 支持 yaml,yml,json,php,ini 路径做配置
7
 */
8
class PathResolver
9
{
10
    public static function resolve(string $path):?string
11
    {
12
        if (file_exists($path)) {
13
            return $path;
14
        }
15
        $basepath = dirname($path).'/'.pathinfo($path, PATHINFO_FILENAME);
16
        
17
        return static::resolveYaml($basepath) ?? static::resolveExtensions($basepath, ['json','php','ini']);
18
    }
19
20
    protected static function resolveYaml(string $basepath):?string
21
    {
22
        if (file_exists($conf = $basepath.'.yml') || file_exists($conf = $basepath.'.yaml')) {
23
            if (function_exists('yaml_parse') || class_exists('Spyc')) {
24
                return $conf;
25
            }
26
        }
27
        return null;
28
    }
29
30
    protected static function resolveExtensions(string $basepath, array $extensions):?string
31
    {
32
        foreach ($extensions as $ext) {
33
            if (file_exists($conf = $basepath.'.'.$ext)) {
34
                return $conf;
35
            }
36
        }
37
        return null;
38
    }
39
}
40