Completed
Push — develop ( f8e6eb...81325c )
by Tom
04:33
created

RewriteHtaccessFile::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 3
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Installer\SubCommand;
4
5
use N98\Magento\Command\SubCommand\AbstractSubCommand;
6
7
class RewriteHtaccessFile extends AbstractSubCommand
8
{
9
    /**
10
     * @return void
11
     */
12
    public function execute()
13
    {
14
        $optionName = 'replaceHtaccessFile';
15
        if (
16
            $this->input->getOption('useDefaultConfigParams') !== null
17
            || $this->input->getOption($optionName) === null
18
        ) {
19
            return;
20
        }
21
22
        $this->getCommand()->getApplication()->setAutoExit(false);
23
24
        $flag = $this->getOptionalBooleanOption($optionName, 'Write BaseURL to .htaccess file?', false);
25
26
        if ($flag) {
27
            $this->replaceHtaccessFile();
28
        }
29
    }
30
31
    protected function replaceHtaccessFile()
32
    {
33
        $installationArgs = $this->config->getArray('installation_args');
34
        $baseUrl = $installationArgs['base-url'];
35
        $htaccessFile = $this->config->getString('installationFolder') . '/pub/.htaccess';
36
37
        $this->_backupOriginalFile($htaccessFile);
38
        $this->_replaceContent($htaccessFile, $baseUrl);
39
    }
40
41
    protected function _backupOriginalFile($htaccesFile)
42
    {
43
        copy(
44
            $htaccesFile,
45
            $htaccesFile . '.dist'
46
        );
47
    }
48
49
    /**
50
     * @param string $htaccessFile
51
     * @param string $baseUrl
52
     */
53
    protected function _replaceContent($htaccessFile, $baseUrl)
54
    {
55
        $content = file_get_contents($htaccessFile);
56
        $content = str_replace('#RewriteBase /magento/', 'RewriteBase ' . parse_url($baseUrl, PHP_URL_PATH), $content);
57
        file_put_contents($htaccessFile, $content);
58
    }
59
}
60