Completed
Push — master ( 4dda57...270521 )
by Andrii
02:39
created

NginxController::getItemConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
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 Exception;
14
use hidev\base\File;
15
use hidev\modifiers\Sudo;
16
use Yii;
17
18
/**
19
 * Goal for Nginx.
20
 */
21
class NginxController extends \hidev\controllers\CommonController
22
{
23
    use \hiqdev\yii2\collection\ManagerTrait;
24
25
    protected $_logDir;
26
    protected $_etcDir;
27
    protected $_fpmSocket;
28
29
    public $defaultClass = VhostController::class;
30
31 1
    public function actionDoDump()
32
    {
33 1
        foreach ($this->getItems() as $vhost) {
34 1
            $conf = $vhost->renderConf();
35 1
            file_put_contents($vhost->getDomain() . '.conf', $conf);
36 1
        }
37 1
    }
38
39 1
    public function actionDump()
40
    {
41 1
        return $this->perform('do-dump');
42
    }
43
44
    public function actionDeploy($aliases = [])
0 ignored issues
show
Unused Code introduced by
The parameter $aliases is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        return $this->perform('do-deploy');
47
    }
48
49
    public function actionDoDeploy()
50
    {
51
        $etcDir = $this->getEtcDir();
52
        if (!is_dir($etcDir)) {
53
            throw new InvalidParamException("Non existing Nginx etcDir: $etcDir");
54
        }
55
        $enabledDir   = $etcDir . DIRECTORY_SEPARATOR . 'sites-enabled';
56
        $availableDir = $etcDir . DIRECTORY_SEPARATOR . 'sites-available';
57
        static::mkdir($enabledDir);
58
        static::mkdir($availableDir);
59
        foreach ($this->getItems() as $vhost) {
60
            $conf = $vhost->renderConf();
61
            $name = $vhost->getDomain() . '.conf';
62
            $file = File::plain($availableDir . DIRECTORY_SEPARATOR . $name);
63
            $file->save($conf);
64
            $file->symlink($enabledDir . DIRECTORY_SEPARATOR . $name);
65
        }
66
        $this->actionRestart();
67
    }
68
69
    public function actionStart()
70
    {
71
        return $this->make('start');
72
    }
73
74
    public function actionStop()
75
    {
76
        return $this->make('stop');
77
    }
78
79
    public function actionReload()
80
    {
81
        return $this->make('reload');
82
    }
83
84
    public function actionRestart()
85
    {
86
        return $this->make('restart');
87
    }
88
89
    public function actionStatus()
90
    {
91
        return $this->make('status', false);
92
    }
93
94
    public function make($operation, $sudo = true)
95
    {
96
        $args = ['nginx', $operation];
97
        if ($sudo) {
98
            array_push($args, Sudo::create());
99
        }
100
101
        return $this->passthru('service', $args);
102
    }
103
104
    public function actionMake()
105
    {
106
        return $this->make($this->performName ?: 'status');
107
    }
108
109
    public function actionLetsencrypt()
110
    {
111
        return $this->perform('do-letsencrypt');
112
    }
113
114
    public function actionDoLetsencrypt()
115
    {
116
        foreach ($this->getItems() as $vhost) {
117
            $domain = $vhost->getDomain();
118
            $sslDir = $vhost->getSslDir();
119
            $args = ['certonly', '-a', 'webroot', '--webroot-path=' . $vhost->getWebDir()];
120
            foreach (array_reverse($vhost->getDomains()) as $name) {
121
                array_push($args, '-d');
122
                array_push($args, $name);
123
            }
124
            if ($this->passthru('/opt/letsencrypt/letsencrypt-auto', $args)) {
125
                throw new Exception('failed letsencrypt');
126
            }
127
            static::mkdir($sslDir);
128
            $this->passthru('sh', ['-c', "cp /etc/letsencrypt/live/$domain/* $sslDir", Sudo::create()]);
129
            $vhost->actionChmodSsl();
130
        }
131
    }
132
133
    public function actionChmodSsl()
134
    {
135
        foreach ($this->getItems() as $vhost) {
136
            $vhost->actionChmodSsl();
137
        }
138
    }
139
140
    public static function mkdir($path)
141
    {
142
        if (file_exists($path)) {
143
            return true;
144
        }
145
146
        return mkdir($path, 0777, true);
147
    }
148
149
    /**
150
     * Prepares item config.
151
     */
152 1
    public function getItemConfig($name = null, array $config = [])
153
    {
154 1
        return array_merge([
155 1
            'domain' => $name,
156 1
            'nginx'  => $this,
157 1
            'class'  => $this->defaultClass,
158 1
        ], $config);
159
    }
160
161 1
    public function createItem($id, $config = [])
162
    {
163 1
        return Yii::createObject($this->getItemConfig($id, $config), [$id, Yii::$app]);
164
    }
165
166
    public function setLogDir($value)
167
    {
168
        $this->_logDir = $value;
169
    }
170
171 1
    public function getLogDir()
172
    {
173 1
        if ($this->_logDir === null) {
174 1
            $this->_logDir = '/var/log/nginx';
175 1
        }
176
177 1
        return $this->_logDir;
178
    }
179
180
    public function setEtcDir($value)
181
    {
182
        $this->_etcDir = $value;
183
    }
184
185
    public function getEtcDir()
186
    {
187
        if ($this->_etcDir === null) {
188
            $this->_etcDir = $this->findEtcDir();
189
        }
190
191
        return $this->_etcDir;
192
    }
193
194
    public function findEtcDir()
195
    {
196
        $dirs = ['/etc/nginx', '/usr/local/etc/nginx'];
197
        foreach ($dirs as $dir) {
198
            if (is_dir($dir)) {
199
                return $dir;
200
            }
201
        }
202
203
        return reset($dirs);
204
    }
205
206 1
    public function setFpmSocket($value)
207
    {
208 1
        $this->_fpmSocket = $value;
209 1
    }
210
211 1
    public function getFpmSocket()
212
    {
213 1
        if ($this->_fpmSocket === null) {
214
            $this->_fpmSocket = 'unix:' . $this->findFpmSocketFile();
215
        }
216
217 1
        return $this->_fpmSocket;
218
    }
219
220
    public function findFpmSocketFile()
221
    {
222
        $files = ['/run/php/php7.0-fpm.sock', '/var/run/php5-fpm.sock', '/tmp/php-fpm.sock'];
223
        foreach ($files as $file) {
224
            if (file_exists($file)) {
225
                return $file;
226
            }
227
        }
228
229
        return reset($files);
230
    }
231
}
232