Completed
Push — master ( e0ed7b...0e5eef )
by Jakub
03:22
created

Classes/Service/UpdateService.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace DCNGmbH\MooxCore\Service;
3
4
use TYPO3\CMS\Core\Utility\GeneralUtility;
5
6
/**
7
 * UpdateService for moox_core
8
 *
9
 * @package MooxCore
10
 */
11
class UpdateService {
12
13
    /**
14
     * @var string
15
     */
16
    protected $sourceConfigurationLines = array(
17
        '$GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'contentRenderingTemplates\'] = array(\'mooxcore/Configuration/TypoScript/\');',
18
        '$GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'activateContentAdapter\'] = 0;'
19
    );
20
21
    /**
22
     * @var string
23
     */
24
    protected $targetConfigurationFile = 'typo3conf/AdditionalConfiguration.php';
25
26
    /**
27
     * Constructor
28
     */
29
    public function __construct() {
30
        $this->targetConfigurationFile = GeneralUtility::getFileAbsFileName($this->targetConfigurationFile);
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    protected function getCurrentConfigurationLines() {
37
        if (FALSE === file_exists($this->targetConfigurationFile)) {
38
            // We return not a completely empty array but an array containing the
39
            // expected opening PHP tag; to make sure it ends up in the output.
40
            return array('<?php');
41
        }
42
        $lines = explode(PHP_EOL, trim(file_get_contents($this->targetConfigurationFile)));
43
        if (0 === count($lines) || '<?php' !== $lines[0]) {
44
            array_unshift($lines, '<?php');
45
        }
46
        return $lines;
47
    }
48
49
    /**
50
     * Returns TRUE if either of the expected configuration lines
51
     * do not currently exist. If both exist, returns FALSE
52
     * meaning "no need to run the script"
53
     *
54
     * NOTE: Is required by the extension manager, do not remove or make protected
55
     * @api
56
     * @return boolean
57
     */
58
    public function access() {
59
        $currentConfiguration = $this->getCurrentConfigurationLines();
60
        foreach ($this->sourceConfigurationLines as $expectedConfigurationLine) {
0 ignored issues
show
The expression $this->sourceConfigurationLines of type string is not traversable.
Loading history...
61
            if (FALSE === in_array($expectedConfigurationLine, $currentConfiguration)) {
62
                return TRUE;
63
            }
64
        }
65
        return FALSE;
66
    }
67
68
    /**
69
     * NOTE: Is required by the extension manager, do not remove or make protected
70
     * @api
71
     * @return string
72
     */
73
    public function main() {
74
        $this->installAdditionalConfiguration();
75
        return 'Additional configuration lines added to AdditionalConfiguration.php';
76
    }
77
78
    /**
79
     * Install expected lines missing from AdditionalConfiguration file
80
     *
81
     * @return void
82
     */
83
    protected function installAdditionalConfiguration() {
84
        $currentConfigurationLines = $this->getCurrentConfigurationLines();
85
        // remove trailing empty spaces and closing PHP tag to ensure predictable appending:
86
        for ($i = count($currentConfigurationLines) - 1; $i--; $i >= 0) {
87
            $line = trim($currentConfigurationLines[$i]);
88
            if (TRUE === empty($line) || '?>' === $line) {
89
                unset($currentConfigurationLines[$i]);
90
            }
91
        }
92
        // add expected lines if they are not found:
93
        foreach ($this->sourceConfigurationLines as $expectedConfigurationLine) {
0 ignored issues
show
The expression $this->sourceConfigurationLines of type string is not traversable.
Loading history...
94
            if (FALSE === in_array($expectedConfigurationLine, $currentConfigurationLines)) {
95
                $currentConfigurationLines[] = $expectedConfigurationLine;
96
            }
97
        }
98
        $this->writeAdditionalConfigurationFile($currentConfigurationLines);
99
    }
100
101
    /**
102
     * Wrapping method to write array to file
103
     *
104
     * @param array $lines
105
     * @return void
106
     */
107
    protected function writeAdditionalConfigurationFile(array $lines) {
108
        $content = implode(PHP_EOL, $lines) . PHP_EOL;
109
        file_put_contents($this->targetConfigurationFile, $content);
110
    }
111
112
}