|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Vitaliy IIIFX Khomenko (c) 2021 |
|
4
|
|
|
* @license MIT |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/iiifx-production/yii2-autocomplete-helper |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace iiifx\Yii2\Autocomplete; |
|
10
|
|
|
|
|
11
|
|
|
use Closure; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\BaseObject; |
|
15
|
|
|
use yii\helpers\FileHelper; |
|
16
|
|
|
|
|
17
|
|
|
class Config extends BaseObject |
|
18
|
|
|
{ |
|
19
|
|
|
public array $files = []; |
|
20
|
|
|
protected ?array $_config = null; |
|
21
|
|
|
protected ?array $_components = null; |
|
22
|
|
|
|
|
23
|
|
|
public function getComponents(): array |
|
24
|
|
|
{ |
|
25
|
|
|
if (null === $this->_components) { |
|
26
|
|
|
$this->_components = []; |
|
27
|
|
|
|
|
28
|
|
|
if ($config = $this->readConfig()) { |
|
29
|
|
|
foreach ($this->files as $current) { |
|
30
|
|
|
if (isset($config[$current]['components'])) { |
|
31
|
|
|
/** @var mixed[] $components */ |
|
32
|
|
|
$components = $config[$current]['components']; |
|
33
|
|
|
|
|
34
|
|
|
if (is_array($components)) { |
|
35
|
|
|
foreach ($components as $name => $component) { |
|
36
|
|
|
if (($class = $this->findClass($component)) !== false) { |
|
37
|
|
|
$this->_components[$name][$class] = $class; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $this->_components; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function readConfig(): array |
|
50
|
|
|
{ |
|
51
|
|
|
if (null === $this->_config) { |
|
52
|
|
|
$this->_config = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->files as $file) { |
|
55
|
|
|
$path = Yii::getAlias($file); |
|
56
|
|
|
$path = FileHelper::normalizePath($path); |
|
57
|
|
|
|
|
58
|
|
|
if (is_file($path)) { |
|
59
|
|
|
try { |
|
60
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
61
|
|
|
$this->_config[$file] = require $path; |
|
62
|
|
|
} catch (Throwable) { |
|
63
|
|
|
# Ignore |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->_config; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param mixed $section |
|
74
|
|
|
* |
|
75
|
|
|
* @return string|false |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function findClass(mixed $section): bool|string |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
if ($section instanceof Closure) { |
|
81
|
|
|
return get_class($section()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (is_object($section)) { |
|
85
|
|
|
return get_class($section); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (is_string($section)) { |
|
89
|
|
|
return $section; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (is_array($section) && isset($section['class'])) { |
|
93
|
|
|
return $section['class']; |
|
94
|
|
|
} |
|
95
|
|
|
} catch (Throwable) { |
|
96
|
|
|
# Ignore |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|