Completed
Pull Request — master (#12)
by
unknown
01:57
created

getTypoScriptConfigurationArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Aoe\Asdis\System\Configuration;
3
4
use Aoe\Asdis\System\Configuration\Exception\InvalidTypoScriptSetting;
5
use Aoe\Asdis\System\Configuration\Exception\TypoScriptSettingNotExists;
6
use TYPO3\CMS\Core\SingletonInterface;
7
8
/**
9
 * TypoScript configuration provider.
10
 */
11
class TypoScriptConfiguration implements SingletonInterface 
12
{
13
    /**
14
     * @var array
15
     */
16
    private $configuration;
17
18
    /**
19
     * @var array
20
     */
21
    private $configurationCache = [];
22
23
    /**
24
     * @param string $key The setting key. E.g. "logger.severity"
25
     * @param string $validateType The data type to be validated against (E.g. "string"). Empty string to skip validation.
26
     * @param boolean $hasSubkeys Tells whether the requested key is assumed to has subkeys.
27
     * @return mixed
28
     * @throws \Aoe\Asdis\System\Configuration\Exception\InvalidTypoScriptSetting
29
     * @throws \Aoe\Asdis\System\Configuration\Exception\TypoScriptSettingNotExists
30
     */
31 2
    public function getSetting($key, $validateType = '', $hasSubkeys = false)
32
    {
33 2
        if (isset($this->configurationCache[$key])) {
34
            return $this->configurationCache[$key];
35
        }
36 2
        $parts = explode('.', $key);
37 2
        if (false === is_array($parts) || sizeof($parts) < 1) {
38
            throw new TypoScriptSettingNotExists($key, 1372050700894);
39
        }
40 2
        $conf = $this->getTypoScriptConfigurationArray();
41 2
        $lastPartIndex = sizeof($parts) - 1;
42 2
        foreach($parts as $index => $part) {
43 2
            $subkey = $part;
44 2
            if ($lastPartIndex !== $index || $hasSubkeys) {
45 1
                $subkey .= '.';
46
            }
47 2
            if (false === isset($conf[$subkey])) {
48 1
                throw new TypoScriptSettingNotExists($key, 1372063884313);
49
            }
50 1
            $conf = $conf[$subkey];
51 1
            if ($lastPartIndex === $index) {
52 1
                break;
53
            }
54
        }
55 1
        if (strlen($validateType) > 0 && strcmp($validateType, gettype($conf)) !== 0) {
56 1
            throw new InvalidTypoScriptSetting($key, gettype($conf), 1372064668444);
57
        }
58
        $this->configurationCache[$key] = $conf;
59
        return $conf;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    protected function getTypoScriptConfigurationArray()
66
    {
67
        return $GLOBALS['TSFE']->tmpl->setup['config.']['tx_asdis.'];
68
    }
69
}