|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Siam; |
| 4 | + |
| 5 | + |
| 6 | +class HostsManage { |
| 7 | + // hosts 文件路径 |
| 8 | + protected $file; |
| 9 | + // hosts 记录数组 |
| 10 | + protected $hosts = array(); |
| 11 | + // 配置文件路径,默认为 __FILE__ . '.ini'; |
| 12 | + protected $configFile; |
| 13 | + // 从 ini 配置文件读取出来的配置数组 |
| 14 | + protected $config = array(); |
| 15 | + // 配置文件里面需要配置的域名 |
| 16 | + protected $domain = array(); |
| 17 | + // 配置文件获取的 ip 数据 |
| 18 | + protected $ip = array(); |
| 19 | + public function __construct($file, $config_file = null) { |
| 20 | + $this->file = $file; |
| 21 | + if ($config_file) { |
| 22 | + $this->configFile = $config_file; |
| 23 | + } else { |
| 24 | + $this->configFile = __FILE__ . '.ini'; |
| 25 | + } |
| 26 | + $this->initHosts() |
| 27 | + ->initCfg(); |
| 28 | + } |
| 29 | + public function __destruct() { |
| 30 | + $this->write(); |
| 31 | + } |
| 32 | + public function initHosts() { |
| 33 | + $lines = file($this->file); |
| 34 | + foreach ($lines as $line) { |
| 35 | + $line = trim($line); |
| 36 | + if (empty($line) || $line[0] == '#') { |
| 37 | + continue; |
| 38 | + } |
| 39 | + $item = preg_split('/\s+/', $line); |
| 40 | + $this->hosts[$item[1]] = $item[0]; |
| 41 | + } |
| 42 | + return $this; |
| 43 | + } |
| 44 | + public function initCfg() { |
| 45 | + if (! file_exists($this->configFile)) { |
| 46 | + $this->config = array(); |
| 47 | + } else { |
| 48 | + $this->config = (parse_ini_file($this->configFile, true)); |
| 49 | + } |
| 50 | + $this->domain = array_keys($this->config['domain'] ?? []); |
| 51 | + $this->ip = $this->config['ip']?? ""; |
| 52 | + return $this; |
| 53 | + } |
| 54 | + /** |
| 55 | + * 删除配置文件里域的 hosts |
| 56 | + */ |
| 57 | + public function delAllGroup() { |
| 58 | + foreach ($this->domain as $domain) { |
| 59 | + $this->delRecord($domain); |
| 60 | + } |
| 61 | + } |
| 62 | + /** |
| 63 | + * 将域配置为指定 ip |
| 64 | + * @param string $env |
| 65 | + * @return static |
| 66 | + */ |
| 67 | + public function addGroup($env) { |
| 68 | + if (! isset($this->ip[$env])) { |
| 69 | + return $this; |
| 70 | + } |
| 71 | + foreach ($this->domain as $domain) { |
| 72 | + $this->addRecord($domain, $this->ip[$env]); |
| 73 | + } |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 添加一条 host 记录 |
| 79 | + * @param string $domain |
| 80 | + * @param string $ip |
| 81 | + * @return HostsManage |
| 82 | + */ |
| 83 | + function addRecord($domain, $ip) { |
| 84 | + $this->hosts[$domain] = $ip; |
| 85 | + return $this; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * 删除一条 host 记录 |
| 90 | + * @param string $domain |
| 91 | + * @return HostsManage |
| 92 | + */ |
| 93 | + function delRecord($domain) { |
| 94 | + unset($this->hosts[$domain]); |
| 95 | + return $this; |
| 96 | + } |
| 97 | + /** |
| 98 | + * 写入 host 文件 |
| 99 | + */ |
| 100 | + public function write() { |
| 101 | + $str = ''; |
| 102 | + foreach ($this->hosts as $domain => $ip) { |
| 103 | + $str .= $ip . "\t" . $domain . PHP_EOL; |
| 104 | + } |
| 105 | + file_put_contents($this->file, $str); |
| 106 | + return $this; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return array |
| 111 | + */ |
| 112 | + public function getHosts(): array |
| 113 | + { |
| 114 | + return $this->hosts; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param array $hosts |
| 119 | + */ |
| 120 | + public function setHosts(array $hosts) |
| 121 | + { |
| 122 | + $this->hosts = $hosts; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +} |
0 commit comments