Passed
Pull Request — master (#151)
by Romain
03:52
created

TypoScriptDefinitionSource::getDefinitionArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 18
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
namespace CuyZ\Notiz\Domain\Definition\Builder\Component\Source;
18
19
use CuyZ\Notiz\Core\Support\NotizConstants;
20
use Romm\ConfigurationObject\ConfigurationObjectInstance;
21
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Service\TypoScriptService;
24
25
/**
26
 * This definition source component is used to fetch a definition array from
27
 * TypoScript files.
28
 *
29
 * Simple TypoScript file registration
30
 * -----------------------------------
31
 *
32
 * To add a TypoScript source file you can add the following line in the
33
 * `ext_localconf.php` file of your extension:
34
 *
35
 * ```
36
 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][\CuyZ\Notiz\Domain\Definition\Builder\Component\Source\TypoScriptDefinitionSource::class][]
37
 *     = 'EXT:notiz_example/Configuration/TypoScript/Shop.typoscript';
38
 * ```
39
 *
40
 * Advanced TypoScript file registration
41
 * -------------------------------------
42
 *
43
 * In some cases you may need more complex logic to register files; you can then
44
 * use a definition component service (see example below).
45
 *
46
 * To know how to register a new component:
47
 *
48
 * @see \CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder
49
 *
50
 * ```
51
 * class MyCustomComponents
52
 * {
53
 *     public function register(\CuyZ\Notiz\Core\Definition\Builder\Component\DefinitionComponents $components)
54
 *     {
55
 *         if ($this->someCustomCondition()) {
56
 *             $typoScriptSource = $components->getSource(\CuyZ\Notiz\Core\Definition\Builder\Component\Source\DefinitionSource::SOURCE_TYPOSCRIPT);
57
 *             $typoScriptSource->addFilePath('EXT:my_extension/Definition/TypoScript/NotiZ/setup.typoscript');
58
 *         }
59
 *     }
60
 * }
61
 * ```
62
 */
63
class TypoScriptDefinitionSource extends FileDefinitionSource
64
{
65
    /**
66
     * @var ConfigurationObjectInstance
67
     */
68
    protected $objectDefinition;
69
70
    /**
71
     * @var TypoScriptService|\TYPO3\CMS\Core\TypoScript\TypoScriptService
72
     */
73
    protected $typoScriptService;
74
75
    /**
76
     * @param TypoScriptService $typoScriptService
77
     */
78
    public function __construct(TypoScriptService $typoScriptService)
79
    {
80
        $this->typoScriptService = $typoScriptService;
81
    }
82
83
    /**
84
     * Loops on each registered TypoScript definition file, transforms every
85
     * content in a plain definition array.
86
     *
87
     * The arrays are merged to have a single array tree which is then returned.
88
     *
89
     * @return array
90
     */
91
    public function getDefinitionArray()
92
    {
93
        $content = '';
94
95
        foreach ($this->filePaths as $path) {
96
            $content .= GeneralUtility::getUrl($path) . LF;
0 ignored issues
show
Bug introduced by
Are you sure TYPO3\CMS\Core\Utility\G...lUtility::getUrl($path) of type mixed|false can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            $content .= /** @scrutinizer ignore-type */ GeneralUtility::getUrl($path) . LF;
Loading history...
97
        }
98
99
        /** @var TypoScriptParser $typoScriptParser */
100
        $typoScriptParser = GeneralUtility::makeInstance(TypoScriptParser::class);
101
        $content = TypoScriptParser::checkIncludeLines($content);
102
        $typoScriptParser->parse($content);
103
104
        $definition = $this->typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptParser->setup);
105
106
        return isset($definition[NotizConstants::DEFINITION_ROOT_PATH])
107
            ? $definition[NotizConstants::DEFINITION_ROOT_PATH]
108
            : [];
109
    }
110
}
111