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

FileDefinitionSource   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A includeConfiguredSources() 0 9 3
A initializeObject() 0 3 1
A addFilePath() 0 15 3
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\Definition\Builder\Component\Source\DefinitionSource;
20
use CuyZ\Notiz\Core\Exception\FileNotFoundException;
21
use TYPO3\CMS\Core\SingletonInterface;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * Abstraction layer to ease registration of files containing definition.
26
 *
27
 * It enables automatic import of files:
28
 *
29
 * @see \CuyZ\Notiz\Domain\Definition\Builder\Component\Source\FileDefinitionSource::includeConfiguredSources
30
 */
31
abstract class FileDefinitionSource implements DefinitionSource, SingletonInterface
32
{
33
    /**
34
     * @var array
35
     */
36
    protected $filePaths = [];
37
38
    /**
39
     * When the class is initialized, configured files are automatically
40
     * included.
41
     */
42
    public function initializeObject()
43
    {
44
        $this->includeConfiguredSources();
45
    }
46
47
    /**
48
     * Registers a path to a file that should contain definition for the API.
49
     *
50
     * @param string $path
51
     * @return $this
52
     *
53
     * @throws FileNotFoundException
54
     */
55
    public function addFilePath($path)
56
    {
57
        if (isset($this->filePaths[$path])) {
58
            return $this;
59
        }
60
61
        $absolutePath = GeneralUtility::getFileAbsFileName($path);
62
63
        if (false === file_exists($absolutePath)) {
64
            throw FileNotFoundException::definitionSourceTypoScriptFileNotFound($path);
65
        }
66
67
        $this->filePaths[$path] = $absolutePath;
68
69
        return $this;
70
    }
71
72
    /**
73
     * This will automatically register paths to TypoScript files that were
74
     * registered inside `ext_localconf.php` files:
75
     *
76
     * ```
77
     * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][\MyVendor\MyExtension\Domain\Definition\Source\MyFileDefinitionSource::class][]
78
     *     = 'EXT:my_extension/Configuration/Foo/my_definition.foo'
79
     * ```
80
     */
81
    private function includeConfiguredSources()
82
    {
83
        // @PHP7
84
        $configuredSources = isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][static::class])
85
            ? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][static::class]
86
            : [];
87
88
        foreach ($configuredSources as $configuredSource) {
89
            $this->addFilePath((string)$configuredSource);
90
        }
91
    }
92
}
93