sonrac /
laravel-wamp
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Created by PhpStorm. |
||
| 4 | * User: Donii Sergii <[email protected]> |
||
| 5 | * Date: 11/7/17 |
||
| 6 | * Time: 3:39 PM |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace sonrac\WAMP\Commands; |
||
| 10 | |||
| 11 | use Illuminate\Console\Command; |
||
| 12 | |||
| 13 | |||
| 14 | /** |
||
| 15 | * @class DownWAMP |
||
| 16 | * Down WAMP servers & clients command. |
||
| 17 | * |
||
| 18 | * @author Donii Sergii <[email protected]> |
||
| 19 | */ |
||
| 20 | class DownWAMP extends Command |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | */ |
||
| 25 | protected $signature = 'wamp:stop {--server-only} {--client-only}'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | protected $description = 'Stop all background running server & clients |
||
| 31 | {--server-only : Stop only running server instances} |
||
| 32 | {--client-only : Stop only running client instances} |
||
| 33 | '; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Handle command |
||
| 37 | * |
||
| 38 | * @author Donii Sergii <[email protected]> |
||
| 39 | */ |
||
| 40 | 2 | public function fire() { |
|
| 41 | 2 | $clientsOnly = $this->option('client-only'); |
|
| 42 | 2 | $serversOnly = $this->option('server-only'); |
|
| 43 | |||
| 44 | 2 | if (!$serversOnly) { |
|
| 45 | 1 | $this->stopInstances(storage_path('clients.pids')); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 46 | } |
||
| 47 | |||
| 48 | 2 | if (!$clientsOnly) { |
|
| 49 | 1 | $this->stopInstances(storage_path('servers.pids')); |
|
| 50 | } |
||
| 51 | 2 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Handle command alias |
||
| 55 | * |
||
| 56 | * @author Donii Sergii <[email protected]> |
||
| 57 | */ |
||
| 58 | 2 | public function handle() { |
|
| 59 | 2 | $this->fire(); |
|
| 60 | 2 | } |
|
| 61 | |||
| 62 | 2 | private function stopInstances($file) { |
|
| 63 | 2 | if (file_exists($file)) { |
|
| 64 | 2 | $content = explode(PHP_EOL, file_get_contents($file)); |
|
| 65 | |||
| 66 | 2 | if (count($content)) { |
|
| 67 | 2 | foreach ($content as $pid) { |
|
| 68 | 2 | $pid = (int) trim($pid); |
|
| 69 | |||
| 70 | 2 | if ($pid > 0) { |
|
| 71 | 2 | exec('kill ' . $pid); |
|
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | 2 | exec('rm ' . $file); |
|
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | 2 | } |
|
| 80 | |||
| 81 | } |