1 | <?php |
||
9 | class ResourceLoader |
||
10 | { |
||
11 | /** |
||
12 | * Yaml service. |
||
13 | * |
||
14 | * @var Yaml |
||
15 | */ |
||
16 | protected $yaml; |
||
17 | |||
18 | /** |
||
19 | * @var \Illuminate\Support\Collection |
||
20 | */ |
||
21 | private $resources; |
||
22 | |||
23 | /** |
||
24 | * ResourceLoader constructor. |
||
25 | * |
||
26 | * @param Yaml $yaml |
||
27 | */ |
||
28 | public function __construct(Yaml $yaml) |
||
29 | { |
||
30 | $this->yaml = $yaml; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Get enabled resources. |
||
35 | * |
||
36 | * @param $resources |
||
37 | * @return \Illuminate\Support\Collection |
||
38 | * @throws \Exception |
||
39 | */ |
||
40 | private function getEnabledResources($resources) |
||
41 | { |
||
42 | if ( |
||
43 | is_array($filters = config($configKey = 'health.resources.enabled')) |
||
44 | ) { |
||
45 | return collect($resources)->filter(function ($resource, $name) use ( |
||
46 | $filters |
||
47 | ) { |
||
48 | foreach ($filters as $filter) { |
||
49 | if (preg_match("|^$filter$|", $name)) { |
||
50 | return true; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return false; |
||
55 | }); |
||
56 | } |
||
57 | |||
58 | throw new DomainException("Invalid value for config('$configKey'')"); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Resources getter. |
||
63 | * |
||
64 | * @return \Illuminate\Support\Collection |
||
65 | * @throws \Exception |
||
66 | */ |
||
67 | public function getResources() |
||
68 | { |
||
69 | $this->load(); |
||
70 | |||
71 | return $this->resources; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Load all resources. |
||
76 | * |
||
77 | * @return \Illuminate\Support\Collection |
||
78 | * @throws \Exception |
||
79 | */ |
||
80 | public function load() |
||
81 | { |
||
82 | if (! empty($this->resources)) { |
||
83 | return $this->resources; |
||
84 | } |
||
85 | |||
86 | return $this->resources = $this->sanitizeResources( |
||
87 | $this->getEnabledResources($this->loadResourceFiles()) |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Load resources in files. |
||
93 | * |
||
94 | * @return \Illuminate\Support\Collection |
||
95 | */ |
||
96 | private function loadResourceFiles() |
||
97 | { |
||
98 | return $this->replaceExecutableCode( |
||
99 | $this->loadResourcesFiles() |
||
100 | )->mapWithKeys(function ($value, $key) { |
||
101 | return [$this->removeExtension($key) => $value]; |
||
102 | }); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Replace executable code. |
||
107 | * |
||
108 | * @return \Illuminate\Support\Collection |
||
109 | */ |
||
110 | private function replaceExecutableCode($files) |
||
111 | { |
||
112 | return $files->map(function ($item) { |
||
113 | return collect($item) |
||
114 | ->map(function ($value) { |
||
115 | return (new Resolver())->recursivelyFindAndReplaceExecutableCode( |
||
116 | $value |
||
117 | ); |
||
118 | }) |
||
119 | ->all(); |
||
120 | }); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Load Resources. |
||
125 | * |
||
126 | * @return \Illuminate\Support\Collection |
||
127 | * @throws \Exception |
||
128 | */ |
||
129 | public function loadResources() |
||
133 | |||
134 | /** |
||
135 | * Load resource files. |
||
136 | * |
||
137 | * @return \Illuminate\Support\Collection |
||
138 | */ |
||
139 | private function loadResourcesFiles() |
||
152 | |||
153 | /** |
||
154 | * Remove extension from file name. |
||
155 | * |
||
156 | * @param $key |
||
157 | * @return string |
||
158 | */ |
||
159 | private function removeExtension($key) |
||
163 | |||
164 | /** |
||
165 | * Sanitize resource key. |
||
166 | * |
||
167 | * @param $key |
||
168 | * @return string |
||
169 | */ |
||
170 | private function sanitizeKey($key) |
||
174 | |||
175 | /** |
||
176 | * Sanitize resources. |
||
177 | * |
||
178 | * @param $resources |
||
179 | * @return \Illuminate\Support\Collection |
||
180 | */ |
||
181 | private function sanitizeResources($resources) |
||
189 | } |
||
190 |