Completed
Push — master ( 5ea837...9dec3c )
by Paul
9s
created

ConfigManager::getMergedConfig()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.2
cc 4
eloc 12
nc 3
nop 0
1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Config;
12
13
use Symfony\Component\Filesystem\Filesystem;
14
use Zend\Stdlib\ArrayUtils;
15
16
/**
17
 * ConfigManager extends ConfigLoader capabilities with lazy-loading and a caching mechanism,.
18
 *
19
 * @author Vítor Brandão <[email protected]>
20
 */
21
class ConfigManager extends ConfigLoader
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $cachePath;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $cacheEnabled;
32
33
    /**
34
     * @var array
35
     */
36
    protected $configs = array();
37
38
    /**
39
     * @var null|array
40
     */
41
    protected $mergedConfig;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $skipConfig = false;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param string       $cachePath
52
     * @param bool         $cacheEnabled
53
     * @param string|array $paths        A path or an array of paths where to look for resources
54
     */
55
    public function __construct($cachePath, $cacheEnabled, $paths = array())
56
    {
57
        $this->cachePath    = $cachePath;
58
        $this->cacheEnabled = (bool) $cacheEnabled;
59
60
        if ((true === $this->cacheEnabled) && file_exists($this->cachePath)) {
61
            $this->skipConfig   = true;
62
            $this->mergedConfig = require $this->cachePath;
63
        }
64
65
        parent::__construct($paths);
66
    }
67
68
    /**
69
     * @param mixed  $resource The resource
70
     * @param string $type     The resource type
71
     *
72
     * @return $this
73
     */
74
    public function addConfig($resource, $type)
75
    {
76
        if (!$this->skipConfig) {
77
            $this->configs[] = array(
78
                'resource'  => $resource,
79
                'type'      => $type,
80
            );
81
        }
82
83
        return $this;
84
    }
85
86
    public function getMergedConfig()
87
    {
88
        if (null === $this->mergedConfig) {
89
            $this->mergedConfig = array();
90
            foreach ($this->configs as $config) {
91
                $this->mergedConfig = ArrayUtils::merge($this->mergedConfig,
92
                    $this->load($config['resource'], $config['type']));
93
            }
94
95
            if ($this->cacheEnabled) {
96
                $mode       = 0666 & ~umask();
97
                $content    = "<?php\nreturn " . var_export($this->mergedConfig, 1) . ';';
98
                $filesystem = new Filesystem();
99
                $filesystem->dumpFile($this->cachePath, $content, $mode);
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::dumpFile() has too many arguments starting with $mode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
            }
101
        }
102
103
        return $this->mergedConfig;
104
    }
105
}
106