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 ConfigManager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Illuminate\Foundation\Application |
12
|
|
|
*/ |
13
|
|
|
protected $app; |
14
|
|
|
|
15
|
|
|
protected $configs = []; |
16
|
|
|
|
17
|
|
|
public function __construct($app) |
18
|
|
|
{ |
19
|
|
|
$this->app = $app; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function makeFromUri($uri) |
23
|
|
|
{ |
24
|
|
|
$name = $this->getNestedConfigName($uri); |
25
|
|
|
return $this->make($name); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function getNestedConfigName($uri) |
29
|
|
|
{ |
30
|
|
|
return str_replace('/', '.', $uri); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function make($name) |
34
|
|
|
{ |
35
|
|
|
if (isset($this->configs[$name])) { |
36
|
|
|
return $this->configs[$name]; |
37
|
|
|
} |
38
|
|
|
$options = $this->getConfigOptions($name); |
39
|
|
|
|
40
|
|
|
return $this->configs[$name] = $options ? new ModelConfig($options) : null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getConfigOptions($name) |
44
|
|
|
{ |
45
|
|
|
if (file_exists($this->getConfigFilePath($name))) { |
46
|
|
|
return config(config('admin.model_config_dir') . '.' . $name); |
47
|
|
|
} |
48
|
|
|
throw new NotFoundHttpException("not found model({$name}) config file"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getConfigFilePath($name) |
52
|
|
|
{ |
53
|
|
|
$modelPath = config_path(config('admin.model_config_dir')); |
54
|
|
|
if (!is_dir($modelPath)) { |
55
|
|
|
throw new InvalidArgumentException('admin config(model_config_dir) is not a directory'); |
56
|
|
|
} |
57
|
|
|
$file = str_replace('.', DIRECTORY_SEPARATOR, $name) . '.php'; |
58
|
|
|
return $modelPath . DIRECTORY_SEPARATOR . $file; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|