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

Factory::getNestedConfigName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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