Passed
Push — master ( a7d46b...0a8eff )
by
unknown
14:11
created

EnvironmentService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 14
dl 0
loc 57
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setFrontendMode() 0 3 1
A isEnvironmentInBackendMode() 0 3 1
A isEnvironmentInFrontendMode() 0 9 3
A __construct() 0 3 1
A initialize() 0 8 3
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 Psr\Http\Message\ServerRequestInterface;
21
use TYPO3\CMS\Core\Http\ApplicationType;
22
use TYPO3\CMS\Core\SingletonInterface;
23
24
/**
25
 * Service for determining environment params
26
 * @internal only to be used within Extbase, not part of TYPO3 Core API.
27
 * @deprecated since v11.2, will be removed in v12.0.
28
 */
29
class EnvironmentService implements SingletonInterface
30
{
31
    /**
32
     * @var bool|null
33
     */
34
    protected $isFrontendMode;
35
36
    public function __construct()
37
    {
38
        trigger_error(__CLASS__ . ' will be removed in TYPO3 v12, use the PSR-7 Request and the ApplicationType instead.', E_USER_DEPRECATED);
39
    }
40
41
    /**
42
     * Detects if frontend application has been called.
43
     *
44
     * @return bool
45
     */
46
    public function isEnvironmentInFrontendMode(): bool
47
    {
48
        $this->initialize();
49
        if ($this->isFrontendMode !== null) {
50
            return $this->isFrontendMode;
51
        }
52
        // Frontend mode stays false if backend or cli without request object
53
        return ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
54
            && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend();
55
    }
56
57
    /**
58
     * Detects if backend application has been called.
59
     *
60
     * @return bool
61
     */
62
    public function isEnvironmentInBackendMode(): bool
63
    {
64
        return !$this->isEnvironmentInFrontendMode();
65
    }
66
67
    protected function initialize(): void
68
    {
69
        if ($this->isFrontendMode !== null) {
70
            return;
71
        }
72
        // Frontend mode stays false if backend or cli without request object
73
        $this->isFrontendMode = ($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
74
            && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend();
75
    }
76
77
    /**
78
     * A helper method for tests to simulate application behavior, should only be used within TYPO3 Core
79
     *
80
     * @param bool $isFrontendMode
81
     * @internal only used for testing purposes and can be removed at any time.
82
     */
83
    public function setFrontendMode(bool $isFrontendMode): void
84
    {
85
        $this->isFrontendMode = $isFrontendMode;
86
    }
87
}
88