Altax\Util\SSHConfig::parse PHP Method

parse() public static method

parse ssh config
public static parse ( [type] $contents ) : [type]
$contents [type]
return [type]
    public static function parse($contents)
    {
        $servers = array();
        $lines = explode("\n", $contents);
        $key = null;
        foreach ($lines as $line) {
            $line = trim($line);
            if (preg_match("/^(#?)[\\s\t]*([^#\\s\t=]+)[\\s\t=]+(.*)\$/", $line, $matches)) {
                if ($matches && count($matches) != 4) {
                    continue;
                }
                $isComment = $matches[1] == "#";
                $first = $matches[2];
                $second = $matches[3];
                // Check for special comment key/value pairs
                if ($isComment && $key && strpos($first, "altax.") !== false) {
                    $servers[$key][$first] = $second;
                }
                // Ignore commented line.
                if ($isComment) {
                    continue;
                }
                if ($first == "Host") {
                    // a new host section
                    $key = $second;
                    $servers[$key] = array();
                }
                $servers[$key][strtolower($first)] = $second;
            }
        }
        return $servers;
    }

Usage Example

Example #1
0
 public function testParseWithSpeceialComment()
 {
     $config = SSHConfig::parse(file_get_contents(__DIR__ . "/SSHConfigTest/ssh_config4"));
     //        print_r($config);
     $this->assertEquals("true", $config["test-server1"]["altax.ignore"]);
 }