Completed
Push — master ( 0f8b02...a5df1d )
by Greg
02:58
created

ConfigAwareTrait::configPostfix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Robo\Common;
4
5
use Robo\Robo;
6
use Robo\Config\Config;
7
8
trait ConfigAwareTrait
9
{
10
    /**
11
     * @var \Robo\Config\Config
12
     */
13
    protected $config;
14
15
    /**
16
     * Set the config management object.
17
     *
18
     * @param \Robo\Config\Config $config
19
     *
20
     * @return $this
21
     *
22
     * @see \Robo\Contract\ConfigAwareInterface::setConfig()
23
     */
24
    public function setConfig(Config $config)
25
    {
26
        $this->config = $config;
27
28
        return $this;
29
    }
30
31
    /**
32
     * Get the config management object.
33
     *
34
     * @return \Robo\Config\Config
35
     *
36
     * @see \Robo\Contract\ConfigAwareInterface::getConfig()
37
     */
38
    public function getConfig()
39
    {
40
        return $this->config;
41
    }
42
43
    /**
44
     * Any class that uses ConfigAwareTrait SHOULD override this method
45
     * , and define a prefix for its configuration items. This is usually
46
     * done in a base class. When used, this method should return a string
47
     * that ends with a "."; see BaseTask::configPrefix().
48
     *
49
     * @return string
50
     */
51
    protected static function configPrefix()
52
    {
53
        return '';
54
    }
55
56
    protected static function configClassIdentifier($classname)
57
    {
58
        $configIdentifier = strtr($classname, '\\', '.');
59
        $configIdentifier = preg_replace('#^(.*\.Task\.|\.)#', '', $configIdentifier);
60
61
        return $configIdentifier;
62
    }
63
64
    protected static function configPostfix()
65
    {
66
        return '';
67
    }
68
69
    /**
70
     * @param string $key
71
     *
72
     * @return string
73
     */
74
    private static function getClassKey($key)
75
    {
76
        $configPrefix = static::configPrefix();                            // task.
77
        $configClass = static::configClassIdentifier(get_called_class());  // PARTIAL_NAMESPACE.CLASSNAME
78
        $configPostFix = static::configPostfix();                          // .settings
79
80
        return sprintf('%s%s%s.%s', $configPrefix, $configClass, $configPostFix, $key);
81
    }
82
83
    /**
84
     * @param string $key
85
     * @param mixed $value
86
     * @param Config|null $config
87
     */
88
    public static function configure($key, $value, $config = null)
89
    {
90
        if (!$config) {
91
            $config = Robo::config();
92
        }
93
        $config->setDefault(static::getClassKey($key), $value);
94
    }
95
96
    /**
97
     * @param string $key
98
     * @param mixed|null $default
99
     *
100
     * @return mixed|null
101
     */
102
    protected function getConfigValue($key, $default = null)
103
    {
104
        if (!$this->getConfig()) {
105
            return $default;
106
        }
107
        return $this->getConfig()->get(static::getClassKey($key), $default);
108
    }
109
}
110