TypoScriptDefinitionSource   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 4

2 Methods

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

97
            $content .= /** @scrutinizer ignore-type */ GeneralUtility::getUrl($path) . LF;
Loading history...
98
        }
99
100
        /** @var TypoScriptParser $typoScriptParser */
101
        $typoScriptParser = GeneralUtility::makeInstance(TypoScriptParser::class);
102
        $content = TypoScriptParser::checkIncludeLines($content);
103
        $typoScriptParser->parse($content);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type array; however, parameter $string of TYPO3\CMS\Core\TypoScrip...poScriptParser::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

103
        $typoScriptParser->parse(/** @scrutinizer ignore-type */ $content);
Loading history...
104
105
        $definition = $this->typoScriptService->convertTypoScriptArrayToPlainArray($typoScriptParser->setup);
106
107
        return isset($definition[NotizConstants::DEFINITION_ROOT_PATH])
108
            ? $definition[NotizConstants::DEFINITION_ROOT_PATH]
109
            : [];
110
    }
111
}
112