Completed
Push — master ( 742ff7...9d59a2 )
by Nikola
09:00
created

CachingConfigLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phoundation\Config;
6
7
final class CachingConfigLoader implements ConfigLoader
8
{
9
    /** @var ConfigLoader */
10
    private $configLoader;
11
12
    /** @var string */
13
    private $cachedConfigFile;
14
    
15
    public function __construct(ConfigLoader $configLoader, string $cachedConfigFile)
16
    {
17
        $this->configLoader = $configLoader;
18
        $this->cachedConfigFile = $cachedConfigFile;
19
    }
20
21
    public function load(): array
22
    {
23
        if (is_file($this->cachedConfigFile)) {
24
            return require $this->cachedConfigFile;
25
        }
26
27
        $config = $this->configLoader->load();
28
29
        file_put_contents($this->cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
30
31
        return $config;
32
    }
33
}
34