FileDefinitionSource::initializeObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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