Completed
Push — master ( d58174...6fbad8 )
by Andrii
07:08
created

Vhost::getPhpLogDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 * HiDev plugin for NGINX
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\components;
12
13
use hidev\modifiers\Sudo;
14
use Yii;
15
use yii\helpers\StringHelper;
16
17
/**
18
 * NGINX virtual host component.
19
 */
20
class Vhost extends \hidev\base\Component
21
{
22
    /**
23
     * @var Nginx
24
     */
25
    public $nginx;
26
27
    /**
28
     * @var integer
29
     */
30
    public $timeout;
31
32
    public $ssl;
33
34
    public $_aliases = [];
35
36
    protected $_sslDir;
37
    protected $_ips = [];
38
    protected $_localIps = [];
39
    protected $_domain;
40
    protected $_webDir;
41
    protected $_logDir;
42
    protected $_phpLogDir;
43
    protected $_fpmSocket;
44
    protected $_additionalConfig;
45
46
    public function chmodSSL()
47
    {
48
        $dir = $this->getSslDir();
49
        $this->passthru('chown', ['-R', 'www-data', $dir, Sudo::create()]);
50
        $this->passthru('chgrp', ['-R', 'www-data', $dir, Sudo::create()]);
51
        $this->passthru('chmod', ['-R', 'o-rwx',    $dir, Sudo::create()]);
52
    }
53
54
    public function renderConf()
55
    {
56
        return $this->nginx->render('default.twig', [
57
            'this' => $this,
58
        ]);
59
    }
60
61
    public function setDomain($value)
62
    {
63
        $this->_domain = trim($value);
64
    }
65
66
    public function setDomains($domains)
67
    {
68
        if (!is_array($domains)) {
69
            $domains = preg_split('/[\s,]+/', trim($domains));
70
        }
71
        $this->_domain = array_shift($domains);
72
        $this->_aliases = $domains;
73
    }
74
75
    public function getDomain()
76
    {
77
        if ($this->_domain === null || $this->_domain === 'default') {
78
            $this->_domain = $this->take('package')->name;
79
        }
80
81
        return $this->_domain;
82
    }
83
84
    public function setIp($value)
85
    {
86
        $this->_ips = [$value];
87
    }
88
89
    public function setIps($value)
90
    {
91
        $this->_ips = is_array($value) ? array_unique($value) : [$value];
92
    }
93
94
    public function getIps()
95
    {
96
        if (empty($this->_ips)) {
97
            $this->_ips = $this->findIps();
98
        }
99
100
        return $this->_ips;
101
    }
102
103
    public function findIps()
104
    {
105
        $ips = gethostbyname($this->getDomain());
106
        if ($ips === $this->getDomain()) {
107
            $ips = '';
108
        }
109
110
        return [$ips ?: '127.0.0.1'];
111
    }
112
113
    public function setLocalIps($value)
114
    {
115
        $this->_localIps = is_array($value) ? array_unique($value) : [$value];
116
    }
117
118
    public function getLocalIps()
119
    {
120
        if (empty($this->_localIps)) {
121
            $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps();
122
        }
123
124
        return $this->_localIps;
125
    }
126
127
    public function setWebDir($value)
128
    {
129
        $this->_webDir = Yii::getAlias($value);
130
    }
131
132
    public function getWebDir()
133
    {
134
        if ($this->_webDir === null) {
135
            $this->_webDir = Yii::getAlias('@root/web');
136
        }
137
138
        return $this->_webDir;
139
    }
140
141
    public function setPhpLogDir($value)
142
    {
143
        $this->_phpLogDir = Yii::getAlias($value);
144
    }
145
146
    public function getPhpLogDir()
147
    {
148
        if ($this->_phpLogDir === null) {
149
            $dir = $this->getLogDir();
150
            if (StringHelper::endsWith($dir, 'nginx')) {
151
                $dir = substr($dir, 0, -5) . 'php';
152
            }
153
            $this->_phpLogDir = $dir;
154
        }
155
156
        return $this->_phpLogDir;
157
    }
158
159
    public function setLogDir($value)
160
    {
161
        $this->_logDir = Yii::getAlias($value);
162
    }
163
164
    public function getLogDir()
165
    {
166
        if ($this->_logDir === null) {
167
            $this->_logDir = $this->nginx->getLogDir();
168
        }
169
170
        return $this->_logDir;
171
    }
172
173
    public function setFpmSocket($value)
174
    {
175
        $this->_fpmSocket = $value;
176
    }
177
178
    public function getFpmSocket()
179
    {
180
        if ($this->_fpmSocket === null) {
181
            $this->_fpmSocket = $this->nginx->getFpmSocket();
182
        }
183
184
        return $this->_fpmSocket;
185
    }
186
187
    public function setSslDir($value)
188
    {
189
        $this->_sslDir = Yii::getAlias($value);
190
        if ($this->_sslDir[0] !== '/') {
191
            $this->_sslDir = Yii::getAlias('@root/' . $this->_sslDir);
192
        }
193
    }
194
195
    public function getSslDir()
196
    {
197
        if ($this->_sslDir === null) {
198
            $this->_sslDir = Yii::getAlias('@root/ssl');
199
        }
200
201
        return $this->_sslDir;
202
    }
203
204
    public function setAliases($aliases)
205
    {
206
        if (!is_array($aliases)) {
207
            $aliases = preg_split('/[\s,]+/', trim($aliases));
208
        }
209
        $this->_aliases = $aliases;
210
    }
211
212
    public function getAliases()
213
    {
214
        return $this->_aliases;
215
    }
216
217
    public function getDomains()
218
    {
219
        $domains = $this->getAliases();
220
        array_unshift($domains, $this->getDomain());
221
222
        return $domains;
223
    }
224
225
    public function getServerName()
226
    {
227
        return implode(' ', $this->getDomains());
228
    }
229
230
    public function getAdditionalConfig()
231
    {
232
        return $this->_additionalConfig;
233
    }
234
235
    public function setAdditionalConfig($additinalConfig)
236
    {
237
        $this->_additionalConfig = $additinalConfig;
238
    }
239
}
240