Passed
Push — master ( 9ca335...911c13 )
by Romain
03:37
created

FileDefinitionSource::filePaths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
rs 10
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\Definition\Builder\Component\Source\DefinitionSource;
20
use CuyZ\Notiz\Core\Exception\FileNotFoundException;
21
use Generator;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Abstraction layer to ease registration of files containing definition.
27
 *
28
 * It enables automatic import of files:
29
 *
30
 * @see \CuyZ\Notiz\Domain\Definition\Builder\Component\Source\FileDefinitionSource::includeConfiguredSources
31
 */
32
abstract class FileDefinitionSource implements DefinitionSource, SingletonInterface
33
{
34
    /**
35
     * @var array
36
     */
37
    private $filePaths = [];
38
39
    /**
40
     * When the class is initialized, configured files are automatically
41
     * included.
42
     */
43
    public function initializeObject()
44
    {
45
        $this->includeConfiguredSources();
46
    }
47
48
    /**
49
     * Registers a path to a file that should contain definition for the API.
50
     *
51
     * The highest the given priority is, the sooner the file will be handled.
52
     * Files with the lowest priority have more chance to override definition
53
     * values.
54
     *
55
     * @param string $path
56
     * @param int $priority
57
     * @return $this
58
     *
59
     * @throws FileNotFoundException
60
     */
61
    public function addFilePath($path, $priority = 0)
62
    {
63
        if (!isset($this->filePaths[$priority])) {
64
            $this->filePaths[$priority] = [];
65
            krsort($this->filePaths);
66
        }
67
68
        if (isset($this->filePaths[$priority][$path])) {
69
            return $this;
70
        }
71
72
        $absolutePath = GeneralUtility::getFileAbsFileName($path);
73
74
        if (false === file_exists($absolutePath)) {
75
            throw FileNotFoundException::definitionSourceFileNotFound($path);
76
        }
77
78
        $this->filePaths[$priority][$path] = $absolutePath;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return Generator
85
     */
86
    final protected function filePaths()
87
    {
88
        foreach ($this->filePaths as $priority => $paths) {
89
            foreach ($paths as $path) {
90
                yield $priority => $path;
91
            }
92
        }
93
    }
94
95
    /**
96
     * This will automatically register paths to TypoScript files that were
97
     * registered inside `ext_localconf.php` files:
98
     *
99
     * ```
100
     * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][\MyVendor\MyExtension\Domain\Definition\Source\MyFileDefinitionSource::class][]
101
     *     = 'EXT:my_extension/Configuration/Foo/my_definition.foo'
102
     * ```
103
     */
104
    private function includeConfiguredSources()
105
    {
106
        // @PHP7
107
        $configuredSources = isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][static::class])
108
            ? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['NotiZ']['Definition']['Source'][static::class]
109
            : [];
110
111
        foreach ($configuredSources as $configuredSource) {
112
            $this->addFilePath((string)$configuredSource);
113
        }
114
    }
115
}
116