1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Server list |
5
|
|
|
* |
6
|
|
|
* @author Alireza Josheghani <[email protected]> |
7
|
|
|
* @since 29 Sep 2018 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Josh\Console\Commands; |
11
|
|
|
|
12
|
|
|
use Josh\Console\ConsoleStyle as Style; |
13
|
|
|
use Symfony\Component\Console\Helper\Table; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
class ServerListCommand extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* configure command |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function configure() |
27
|
|
|
{ |
28
|
|
|
$this->setName('server:list') |
29
|
|
|
->setDescription('servers list'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* execute command |
34
|
|
|
* |
35
|
|
|
* @param InputInterface $input |
36
|
|
|
* @param OutputInterface $output |
37
|
|
|
*/ |
38
|
|
|
protected function execute(InputInterface $input , OutputInterface $output) |
39
|
|
|
{ |
40
|
|
|
$command = new Style($input, $output); |
41
|
|
|
|
42
|
|
View Code Duplication |
if (! file_exists($file = $this->getHomeDir() . "/.Josh/servers.json")) { |
|
|
|
|
43
|
|
|
|
44
|
|
|
if (! is_dir($dir = $this->getHomeDir() . "/.Josh")) { |
45
|
|
|
mkdir($dir,0777); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
touch($file); |
49
|
|
|
chmod($file,0777); |
50
|
|
|
file_put_contents($file,json_encode([])); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$servers = json_decode(file_get_contents($file), true); |
54
|
|
|
|
55
|
|
|
if (count($servers) == 0) { |
56
|
|
|
|
57
|
|
|
$command->line("No server added to the list. use [ server:add ] to add one."); |
58
|
|
|
} else { |
59
|
|
|
|
60
|
|
|
$table = new Table($output); |
61
|
|
|
|
62
|
|
|
$table->setHeaders(array('Id', 'Name', 'Ip address', 'Last connect')) |
63
|
|
|
->setRows($servers); |
64
|
|
|
|
65
|
|
|
$table->render(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** Get home directory |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getHomeDir() |
74
|
|
|
{ |
75
|
|
|
if(empty($_SERVER['HOME'])){ |
76
|
|
|
return posix_getpwuid(posix_getuid()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $_SERVER['HOME']; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.