Completed
Push — master ( 60ef51...780387 )
by Nikola
02:46
created

CachingConfigLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Phoundation package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Phoundation\Config\Loader;
14
15
use Phoundation\Config\Config;
16
17
/**
18
 * @author Nikola Posa <[email protected]>
19
 */
20
final class CachingConfigLoader implements ConfigLoaderInterface
21
{
22
    /**
23
     * @var ConfigLoaderInterface
24
     */
25
    private $configLoader;
26
27
    /**
28
     * @var string
29
     */
30
    private $cachedConfigFile;
31
    
32
    public function __construct(ConfigLoaderInterface $configLoader, string $cachedConfigFile)
33
    {
34
        $this->configLoader = $configLoader;
35
        $this->cachedConfigFile = $cachedConfigFile;
36
    }
37
38
    public function __invoke() : Config
39
    {
40
        if (is_file($this->cachedConfigFile)) {
41
            return Config::fromArray(include $this->cachedConfigFile);
42
        }
43
44
        $configLoader = $this->configLoader;
45
        /* @var $config Config */
46
        $config = $configLoader();
47
48
        file_put_contents($this->cachedConfigFile, '<?php return ' . var_export($config->toArray(), true) . ';');
49
50
        return $config;
51
    }
52
}
53