Completed
Push — master ( ac874d...15a874 )
by Timo
48:30 queued 44:41
created

CliEnvironment::backup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Environment;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2016 Timo Hund <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Core\Environment;
28
use TYPO3\CMS\Core\SingletonInterface;
29
30
/**
31
 * Helper class for the cli environment helps to define the variables and constants
32
 * that are required in the cli context to allow frontend related operations in the cli context.
33
 *
34
 * @author Timo Hund <[email protected]>
35
 */
36
class CliEnvironment implements SingletonInterface
37
{
38
39
    /**
40
     * @var array
41
     */
42
    protected $backupServerVariables = [];
43
44
    /**
45
     * @var bool
46
     */
47
    protected $isInitialized = false;
48
49
    /**
50
     * @return void
51
     */
52
    public function backup()
53
    {
54
        $this->backupServerVariables = $_SERVER;
55
    }
56
57
    /**
58
     * Initializes the frontend related server variables for the cli context.
59
     *
60
     * @param string $webRoot
61
     * @param string $scriptFileName
62
     * @param string $phpSelf
63
     * @param string $scriptName
64
     * @throws WebRootAllReadyDefinedException
65
     * @return bool
66
     */
67 3
    public function initialize($webRoot, $scriptFileName = '', $phpSelf = '/index.php', $scriptName = '/index.php')
68
    {
69
        // if the environment has be initialized once, we do not need to initialize it twice.
70 3
        if ($this->isInitialized) {
71 1
            return false;
72
        }
73
74 3
        if (defined('TYPO3_PATH_WEB')) {
75 1
            throw new WebRootAllReadyDefinedException('TYPO3_PATH_WEB is already defined');
76
        }
77
78 3
        if ($scriptFileName === '') {
79 3
            $scriptFileName = Environment::getPublicPath() . '/';
80
        }
81
82 3
        define('TYPO3_PATH_WEB', $webRoot);
83 3
        $_SERVER['SCRIPT_FILENAME'] = $scriptFileName;
84 3
        $_SERVER['PHP_SELF'] = $phpSelf;
85 3
        $_SERVER['SCRIPT_NAME'] = $scriptName;
86
87 3
        $this->isInitialized = true;
88 3
        return true;
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    public function getIsInitialized()
95
    {
96
        return $this->isInitialized;
97
    }
98
99
    /**
100
     * @return void
101
     */
102 1
    public function restore()
103
    {
104 1
        $_SERVER = $this->backupServerVariables;
105 1
    }
106
}
107