Completed
Push — master ( b7f376...4dda57 )
by Andrii
07:44
created

VhostController::getAdditionalConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * HiDev Nginx plugin
4
 *
5
 * @link      https://github.com/hiqdev/hidev-nginx
6
 * @package   hidev-nginx
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\nginx\controllers;
12
13
use hidev\modifiers\Sudo;
14
use Yii;
15
16
/**
17
 * Goal for Nginx virtual host.
18
 */
19
class VhostController extends \hidev\controllers\CommonController
20
{
21
    /**
22
     * @var NginxController
23
     */
24
    public $nginx;
25
26
    /**
27
     * @var integer
28
     */
29
    public $timeout;
30
31
    public $ssl;
32
33
    public $_aliases = [];
34
35
    protected $_sslDir;
36
    protected $_ips = [];
37
    protected $_localIps = [];
38
    protected $_domain;
39
    protected $_webDir;
40
    protected $_logDir;
41
    protected $_fpmSocket;
42
    protected $_additionalConfig;
43
44
    public function actionChmodSsl()
45
    {
46
        $dir = $this->getSslDir();
47
        $this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]);
48
        $this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]);
49
        $this->passthru('chmod', ['-R', 'o-rwx',    $dir, Sudo::create()]);
50
    }
51
52
    public function renderConf()
53
    {
54
        return $this->nginx->render('default.twig', [
55
            'this' => $this,
56
        ]);
57
    }
58
59
    public function setDomain($value)
60
    {
61
        $this->_domain = $value;
62
    }
63
64
    public function getDomain()
65
    {
66
        if ($this->_domain === null || $this->_domain === 'default') {
67
            $this->_domain = $this->takePackage()->name;
68
        }
69
70
        return $this->_domain;
71
    }
72
73
    public function setIp($value)
74
    {
75
        $this->_ips = [$value];
76
    }
77
78
    public function setIps($value)
79
    {
80
        $this->_ips = is_array($value) ? array_unique($value) : [$value];
81
    }
82
83
    public function getIps()
84
    {
85
        if (empty($this->_ips)) {
86
            $this->_ips = $this->findIps();
87
        }
88
89
        return $this->_ips;
90
    }
91
92
    public function findIps()
93
    {
94
        return [gethostbyname($this->getDomain()) ?: '127.0.0.1'];
95
    }
96
97
    public function setLocalIps($value)
98
    {
99
        $this->_localIps = is_array($value) ? array_unique($value) : [$value];
100
    }
101
102
    public function getLocalIps()
103
    {
104
        if (empty($this->_localIps)) {
105
            $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps();
106
        }
107
108
        return $this->_localIps;
109
    }
110
111
    public function setWebDir($value)
112
    {
113
        $this->_webDir = Yii::getAlias($value);
114
    }
115
116
    public function getWebDir()
117
    {
118
        if ($this->_webDir === null) {
119
            $this->_webDir = $this->takeGoal('start')->buildRootPath('web');
120
        }
121
122
        return $this->_webDir;
123
    }
124
125
    public function setLogDir($value)
126
    {
127
        $this->_logDir = Yii::getAlias($value);
128
    }
129
130
    public function getLogDir()
131
    {
132
        if ($this->_logDir === null) {
133
            $this->_logDir = $this->nginx->getLogDir();
134
        }
135
136
        return $this->_logDir;
137
    }
138
139
    public function setFpmSocket($value)
140
    {
141
        $this->_fpmSocket = $value;
142
    }
143
144
    public function getFpmSocket()
145
    {
146
        if ($this->_fpmSocket === null) {
147
            $this->_fpmSocket = $this->nginx->getFpmSocket();
148
        }
149
150
        return $this->_fpmSocket;
151
    }
152
153
    public function setSslDir($value)
154
    {
155
        $this->_sslDir = Yii::getAlias($value);
156
        if ($this->_sslDir[0] !== '/') {
157
            $this->_sslDir = $this->takeGoal('start')->buildRootPath($this->_sslDir);
158
        }
159
    }
160
161
    public function getSslDir()
162
    {
163
        if ($this->_sslDir === null) {
164
            $this->_sslDir = $this->takeGoal('start')->buildRootPath('ssl');
165
        }
166
167
        return $this->_sslDir;
168
    }
169
170
    public function setAliases($aliases)
171
    {
172
        if (!is_array($aliases)) {
173
            $aliases = preg_split('/[\s,]+/', trim($aliases));
174
        }
175
        $this->_aliases = $aliases;
176
    }
177
178
    public function getAliases()
179
    {
180
        return $this->_aliases;
181
    }
182
183
    public function getDomains()
184
    {
185
        $domains = $this->getAliases();
186
        array_unshift($domains, $this->getDomain());
187
188
        return $domains;
189
    }
190
191
    public function getServerName()
192
    {
193
        return implode(' ', $this->getDomains());
194
    }
195
196
    public function getAdditionalConfig()
197
    {
198
        return $this->_additionalConfig;
199
    }
200
201
    public function setAdditionalConfig($additinalConfig)
202
    {
203
        $this->_additionalConfig = $additinalConfig;
204
    }
205
}
206