PathResolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 39
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 8 3
A resolveExtensions() 0 9 4
A resolveYaml() 0 8 6
1
<?php
2
namespace suda\framework\config;
3
4
/**
5
 * 路径解析器
6
 * 支持 yaml,yml,json,php,ini 路径做配置
7
 */
8
class PathResolver
9
{
10
    /**
11
     * @param string $path
12
     * @return string|null
13
     */
14
    public static function resolve(string $path):?string
15
    {
16
        if (file_exists($path) && is_file($path)) {
17
            return $path;
18
        }
19
        $basepath = dirname($path).'/'.pathinfo($path, PATHINFO_FILENAME);
20
21
        return static::resolveYaml($basepath) ?? static::resolveExtensions($basepath, ['json','php','ini']);
22
    }
23
24
    /**
25
     * @param string $basepath
26
     * @return string|null
27
     */
28
    protected static function resolveYaml(string $basepath):?string
29
    {
30
        if (file_exists($conf = $basepath.'.yml') || file_exists($conf = $basepath.'.yaml')) {
31
            if (function_exists('yaml_parse') || class_exists('Spyc') || class_exists('Symfony\Component\Yaml\Yaml')) {
32
                return $conf;
33
            }
34
        }
35
        return null;
36
    }
37
38
    protected static function resolveExtensions(string $basepath, array $extensions):?string
39
    {
40
        foreach ($extensions as $ext) {
41
            $conf = $basepath.'.'.$ext;
42
            if (file_exists($conf) && is_file($conf)) {
43
                return $conf;
44
            }
45
        }
46
        return null;
47
    }
48
}
49