Completed
Push — master ( c759a4...25acd4 )
by Michael
05:45
created

ComposerExtra::getConfig()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 11
nc 3
nop 0
1
<?php
2
3
namespace Schnittstabil\ComposerExtra;
4
5
use function Schnittstabil\Get\getValue;
6
use function Schnittstabil\Get\getValueOrFail;
7
use Schnittstabil\Get\Get;
8
9
/**
10
 * Get namespaced configuration from `composer.json`.
11
 */
12
class ComposerExtra
13
{
14
    use PresetsAwareTrait;
15
    use ComposerJsonAwareTrait;
16
17
    protected $config;
18
    protected $namespace;
19
    protected $defaultConfig;
20
    protected $presetsPath;
21
22
    /**
23
     * Merge configurations.
24
     *
25
     * @var callable
26
     */
27
    public $merge = '\Schnittstabil\ConfigMerge\config_merge';
28
29
    /**
30
     * Create a new ComposerExtra.
31
     *
32
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue`
33
     *
34
     * @param string|int|mixed[] $namespace     a `Schnittstabil\Get\getValue` path
35
     * @param mixed              $defaultConfig default configuration
36
     * @param string             $presetsPath   presets path (w/o namespace)
37
     *
38
     * @SuppressWarnings(PHPMD.StaticAccess)
39
     */
40
    public function __construct($namespace = array(), $defaultConfig = null, $presetsPath = null)
41
    {
42
        $this->namespace = Get::normalizePath($namespace);
43
        array_unshift($this->namespace, 'extra');
44
        $this->defaultConfig = $defaultConfig === null ? new \stdClass() : $defaultConfig;
45
        $this->presetsPath = $presetsPath;
46
    }
47
48
    /**
49
     * Merge two configs.
50
     *
51
     * @param mixed $target Target config
52
     * @param mixed $source Source config
53
     *
54
     * @return mixed The merged config
55
     */
56
    protected function merge($target, $source)
57
    {
58
        if ($source === null) {
59
            return $target;
60
        }
61
62
        return call_user_func($this->merge, $target, $source);
63
    }
64
65
    /**
66
     * Get the configuration.
67
     *
68
     * @return mixed the configuration
69
     */
70
    protected function getConfig()
71
    {
72
        if ($this->config !== null) {
73
            return $this->config;
74
        }
75
76
        $config = $this->merge(
77
            $this->defaultConfig,
78
            getValue($this->namespace, $this->loadComposerJson())
79
        );
80
81
        if ($this->presetsPath === null || $config === null) {
82
            return $this->config = $config;
83
        }
84
85
        $presets = $this->loadPresets(getValue($this->presetsPath, $config, []));
86
        $presetsConfig = array_reduce($presets, $this->merge, array_shift($presets));
87
88
        return $this->config = $this->merge($presetsConfig, $config);
89
    }
90
91
    /**
92
     * Get configuration value.
93
     *
94
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValue`
95
     *
96
     * @param string|int|mixed[] $path    a `Schnittstabil\Get\getValue` path
97
     * @param mixed              $default default value if $path is not valid
98
     *
99
     * @return mixed the value determined by `$path` or otherwise `$default`
100
     */
101
    public function get($path = array(), $default = null)
102
    {
103
        return getValue($path, $this->getConfig(), $default);
104
    }
105
106
    /**
107
     * Get configuration value.
108
     *
109
     * @see https://github.com/schnittstabil/get Documentation of `Schnittstabil\Get\getValueOrFail`
110
     *
111
     * @param string|int|mixed[] $path    a `Schnittstabil\Get\getValueOrFail` path
112
     * @param mixed              $message exception message
113
     *
114
     * @throws \OutOfBoundsException if `$path` is not valid
115
     *
116
     * @return mixed the value determined by `$path`
117
     */
118
    public function getOrFail($path = array(), $message = null)
119
    {
120
        return getValueOrFail($path, $this->getConfig(), $message);
121
    }
122
}
123