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); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
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 | if (!empty($value)) { |
||
116 | if (is_string($value) && strpos($value, ',')!==false) { |
||
117 | $value = explode(',', $value); |
||
118 | } |
||
119 | $this->_localIps = is_array($value) ? array_unique($value) : [$value]; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | public function getLocalIps() |
||
124 | { |
||
125 | if (empty($this->_localIps)) { |
||
126 | $this->_localIps = $this->ssl ? ['127.0.0.1'] : $this->getIps(); |
||
127 | } |
||
128 | |||
129 | return $this->_localIps; |
||
130 | } |
||
131 | |||
132 | public function setWebDir($value) |
||
133 | { |
||
134 | $this->_webDir = Yii::getAlias($value); |
||
135 | } |
||
136 | |||
137 | public function getWebDir() |
||
138 | { |
||
139 | if ($this->_webDir === null) { |
||
140 | $this->_webDir = Yii::getAlias('@root/public'); |
||
141 | } |
||
142 | |||
143 | return $this->_webDir; |
||
144 | } |
||
145 | |||
146 | public function setPhpLogDir($value) |
||
147 | { |
||
148 | $this->_phpLogDir = Yii::getAlias($value); |
||
149 | } |
||
150 | |||
151 | public function getPhpLogDir() |
||
152 | { |
||
153 | if ($this->_phpLogDir === null) { |
||
154 | $dir = $this->getLogDir(); |
||
155 | if (StringHelper::endsWith($dir, 'nginx')) { |
||
156 | $dir = substr($dir, 0, -5) . 'php'; |
||
157 | } |
||
158 | $this->_phpLogDir = $dir; |
||
159 | } |
||
160 | |||
161 | return $this->_phpLogDir; |
||
162 | } |
||
163 | |||
164 | public function setLogDir($value) |
||
165 | { |
||
166 | $this->_logDir = Yii::getAlias($value); |
||
167 | } |
||
168 | |||
169 | public function getLogDir() |
||
170 | { |
||
171 | if ($this->_logDir === null) { |
||
172 | $this->_logDir = $this->nginx->getLogDir(); |
||
173 | } |
||
174 | |||
175 | return $this->_logDir; |
||
176 | } |
||
177 | |||
178 | public function setFpmSocket($value) |
||
179 | { |
||
180 | $this->_fpmSocket = $value; |
||
181 | } |
||
182 | |||
183 | public function getFpmSocket() |
||
184 | { |
||
185 | if ($this->_fpmSocket === null) { |
||
186 | $this->_fpmSocket = $this->nginx->getFpmSocket(); |
||
187 | } |
||
188 | |||
189 | return $this->_fpmSocket; |
||
190 | } |
||
191 | |||
192 | public function setSslDir($value) |
||
193 | { |
||
194 | $this->_sslDir = Yii::getAlias($value); |
||
195 | if ($this->_sslDir[0] !== '/') { |
||
196 | $this->_sslDir = Yii::getAlias('@root/' . $this->_sslDir); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public function getSslDir() |
||
201 | { |
||
202 | if ($this->_sslDir === null) { |
||
203 | $this->_sslDir = Yii::getAlias('@root/ssl'); |
||
204 | } |
||
205 | |||
206 | return $this->_sslDir; |
||
207 | } |
||
208 | |||
209 | public function setAliases($aliases) |
||
210 | { |
||
211 | if (!is_array($aliases)) { |
||
212 | $aliases = preg_split('/[\s,]+/', trim($aliases)); |
||
213 | } |
||
214 | $this->_aliases = $aliases; |
||
215 | } |
||
216 | |||
217 | public function getAliases() |
||
218 | { |
||
219 | return $this->_aliases; |
||
220 | } |
||
221 | |||
222 | public function getDomains() |
||
223 | { |
||
224 | $domains = $this->getAliases(); |
||
225 | array_unshift($domains, $this->getDomain()); |
||
226 | |||
227 | return $domains; |
||
228 | } |
||
229 | |||
230 | public function getServerName() |
||
231 | { |
||
232 | return implode(' ', $this->getDomains()); |
||
233 | } |
||
234 | |||
235 | public function getAdditionalConfig() |
||
236 | { |
||
237 | return $this->_additionalConfig; |
||
238 | } |
||
239 | |||
240 | public function setAdditionalConfig($additinalConfig) |
||
241 | { |
||
242 | $this->_additionalConfig = $additinalConfig; |
||
243 | } |
||
244 | } |
||
245 |