Completed
Push — master ( d46a06...d093e3 )
by
unknown
14:46
created

EnvironmentService::isEnvironmentInFrontendMode()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Extbase\Service;
19
20
use TYPO3\CMS\Core\SingletonInterface;
21
22
/**
23
 * Service for determining environment params
24
 * @internal only to be used within Extbase, not part of TYPO3 Core API.
25
 */
26
class EnvironmentService implements SingletonInterface
27
{
28
    /**
29
     * @var bool|null
30
     */
31
    protected $isFrontendMode;
32
33
    /**
34
     * Detects if TYPO3_MODE is defined and its value is "FE"
35
     *
36
     * @return bool
37
     */
38
    public function isEnvironmentInFrontendMode(): bool
39
    {
40
        $this->initialize();
41
        if ($this->isFrontendMode !== null) {
42
            return $this->isFrontendMode;
43
        }
44
        return (defined('TYPO3_MODE') && TYPO3_MODE === 'FE') ?: false;
0 ignored issues
show
introduced by
The condition TYPO3\CMS\Extbase\Service\TYPO3_MODE === 'FE' is always false.
Loading history...
45
    }
46
47
    /**
48
     * Detects if TYPO3_MODE is defined and its value is "BE"
49
     *
50
     * @return bool
51
     */
52
    public function isEnvironmentInBackendMode(): bool
53
    {
54
        return !$this->isEnvironmentInFrontendMode();
55
    }
56
57
    protected function initialize(): void
58
    {
59
        if ($this->isFrontendMode !== null) {
60
            return;
61
        }
62
        if (defined('TYPO3_MODE')) {
63
            $this->isFrontendMode = TYPO3_MODE === 'FE';
64
        }
65
    }
66
67
    /**
68
     * A helper method for tests to simulate TYPO3_MODE behavior, should only be used within TYPO3 Core
69
     *
70
     * @param bool $isFrontendMode
71
     * @internal only used for testing purposes and can be removed at any time.
72
     */
73
    public function setFrontendMode(bool $isFrontendMode): void
74
    {
75
        $this->isFrontendMode = $isFrontendMode;
76
    }
77
}
78