Completed
Push — master ( 7235dd...b44091 )
by Sergii
05:14
created

src/Commands/WAMPCommandTrait.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Donii Sergii <[email protected]>
5
 * Date: 11/3/17
6
 * Time: 6:08 PM
7
 */
8
9
namespace sonrac\WAMP\Commands;
10
11
use Monolog\Formatter\LineFormatter;
12
use Monolog\Handler\StreamHandler;
13
use Monolog\Logger as MonologLogger;
14
use Thruway\Logging\ConsoleLogger;
15
use Thruway\Logging\Logger;
16
17
/**
18
 * @trait  WAMPCommandTrait
19
 *
20
 * @author Donii Sergii <[email protected]>
21
 */
22
trait WAMPCommandTrait
23
{
24
    /**
25
     * WAMP router host.
26
     *
27
     * @var string
28
     */
29
    protected $host = '127.0.0.1';
30
31
    /**
32
     * Wamp realm to used.
33
     *
34
     * @var string
35
     */
36
    protected $realm;
37
38
    /**
39
     * WAMP router port.
40
     *
41
     * @var int
42
     */
43
    protected $port = '9090';
44
45
    /**
46
     * Run in debug mode. Echo output to console.
47
     *
48
     * @var bool
49
     */
50
    protected $noDebug = false;
51
52
    /**
53
     * Run in loop or once.
54
     *
55
     * @var bool
56
     */
57
    protected $runOnce = false;
58
59
    /**
60
     * Specify the router protocol as wss.
61
     *
62
     * @var bool
63
     */
64
    protected $tls = false;
65
66
    /**
67
     * WAMP routes path.
68
     *
69
     * @var string
70
     *
71
     * @author Donii Sergii <[email protected]>
72
     */
73
    protected $routePath = null;
74
75
    /**
76
     * Transport provider class.
77
     *
78
     * @var string|\Thruway\Transport\RatchetTransportProvider|null
79
     *
80
     * @author Donii Sergii <[email protected]>
81
     */
82
    protected $transportProvider = 'Thruway\Transport\RatchetTransportProvider';
83
84
    /**
85
     * Run in background.
86
     *
87
     * @var bool
88
     *
89
     * @author Donii Sergii <[email protected]>
90
     */
91
    protected $runInBackground = false;
92
93
    /**
94
     * No loop runner
95
     *
96
     * @var bool
97
     *
98
     * @author Donii Sergii <[email protected]>
99
     */
100
    protected $noLoop = false;
101
102
    /**
103
     * Get option value from input.
104
     *
105
     * @param string $optionName
106
     *
107
     * @return array|null|string
108
     */
109
    protected function getOptionFromInput(string $optionName, $default = null)
110
    {
111
        if (null === $this->input->getOption($optionName)) {
112
            return $default;
113
        }
114
115
        return $this->option($optionName);
116
    }
117
118
    /**
119
     * Get minion config.
120
     *
121
     * @param string|null $optName Option name
122
     * @param mixed       $default Default value
123
     *
124
     * @return array|string|int|null
125
     */
126
    protected function getConfig($optName = null, $default = null)
127
    {
128
        $options = config('minion') ?? [];
129
130
        if (null === $optName) {
131
            return $options ?? [];
132
        }
133
134
        return isset($options[$optName]) ? $options[$optName] : $default;
135
    }
136
137
    /**
138
     * Change WAMP logger
139
     *
140
     * @param string $fileName
141
     *
142
     * @author Donii Sergii <[email protected]>
143
     */
144
    protected function changeWampLogger($fileName = 'wamp-server.log')
145
    {
146
        if (!$this->noDebug) {
147
            return;
148
        }
149
150
        $path = $this->getConfig('pathLogFile') ?? storage_path('logs/' . $fileName);
151
152
        $handler = (new StreamHandler($path, MonologLogger::DEBUG))
153
            ->setFormatter(new LineFormatter(null, null, true, true));
154
155
        $logger = new MonologLogger($fileName, [$handler]);
156
157
        Logger::set($logger);
158
    }
159
160
    /**
161
     * Parse base options
162
     *
163
     * @author Donii Sergii <[email protected]>
164
     */
165
    protected function parseBaseOptions()
166
    {
167
        $this->host = $this->getOptionFromInput('host') ?? $this->getConfig('host', $this->host);
168
        $this->port = $this->getOptionFromInput('port') ?? $this->getConfig('port', $this->port);
169
        $this->realm = $this->getOptionFromInput('realm') ?? $this->getConfig('realm', $this->realm);
170
        $this->tls = $this->getOptionFromInput('tls') ?? $this->getConfig('tls', $this->tls);
171
        $this->transportProvider = $this->getOptionFromInput('transport-provider') ?? $this->getConfig('transportProvider',
172
                $this->transportProvider);
173
        $this->runInBackground = $this->getOptionFromInput('in-background') ?? false;
174
175
        $this->noDebug = $this->getOptionFromInput('no-debug') ?? $this->noDebug;
176
        $this->runOnce = $this->getOptionFromInput('no-loop') ?? $this->runOnce;
177
178
        $this->routePath = $this->getOptionFromInput('route-path') ?? $this->getConfig('routePath', $this->routePath);
179
        $this->routePath = is_string($this->routePath) ? realpath($this->routePath) : null;
180
181
        if (!$this->noDebug) {
182
            Logger::set(new ConsoleLogger());
183
        }
184
    }
185
186
    /**
187
     * Parse input options
188
     *
189
     * @return mixed
190
     *
191
     * @author Donii Sergii <[email protected]>
192
     */
193
    abstract protected function parseOptions();
194
195
    /**
196
     * Get transport provider
197
     *
198
     * @return mixed
199
     *
200
     * @author Donii Sergii <[email protected]>
201
     */
202
    abstract protected function getTransportProvider();
203
204
    /**
205
     * Add pid process
206
     *
207
     * @param int $pid Process pid
208
     * @param string $fileName Filename
209
     *
210
     * @author Donii Sergii <[email protected]>
211
     */
212
    protected function addPidToLog($pid, $fileName = 'server.pids') {
213
        $fileName = storage_path($fileName);
0 ignored issues
show
The function storage_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

213
        $fileName = /** @scrutinizer ignore-call */ storage_path($fileName);
Loading history...
214
        if (!file_exists(storage_path($fileName))) {
215
            file_put_contents($fileName, $pid);
216
217
            return;
218
        }
219
220
        $content = file_get_contents($fileName);
221
222
        file_put_contents($fileName, $content . PHP_EOL . $pid);
223
    }
224
}
225