Completed
Push — master ( 7d55d6...15c9e2 )
by wen
03:11
created

Factory::getConfigFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
4
namespace Sco\Admin\Config;
5
6
7
class Factory
8
{
9
    protected $configs = [];
10
11
    public function __construct()
12
    {
13
14
    }
15
16
    public function makeFromUri($uri)
17
    {
18
        $name = str_replace(DIRECTORY_SEPARATOR, '.', $uri);
19
        return $this->make($name);
20
    }
21
22
    public function make($name)
23
    {
24
        if (isset($this->configs[$name])) {
25
            return $this->configs[$name];
26
        }
27
        $options = $this->getConfigOptions($name);
28
        
29
        return $this->configs[$name] = $options ? new Config($options) : null;
30
    }
31
32
    protected function getConfigOptions($name)
33
    {
34
        if (file_exists($this->getConfigFilePath($name))) {
35
            return config(config('admin.model_config_dir') . '.' . $name);
36
        }
37
        throw new \InvalidArgumentException("not found model({$name}) config file");
38
    }
39
40
    protected function getConfigFilePath($name)
41
    {
42
        $modelPath = config_path(config('admin.model_config_dir'));
43
        if (!is_dir($modelPath)) {
44
            throw new \InvalidArgumentException('not found admin model config dir');
45
        }
46
        $file = str_replace('.', DIRECTORY_SEPARATOR, $name) . '.php';
47
        return $modelPath . DIRECTORY_SEPARATOR . $file;
48
    }
49
}