Follow IDFs coding standards and tweak opening curly braces as well as

quoting where possible
This commit is contained in:
Thomas Keller
2010-08-13 11:20:03 +02:00
committed by Thomas Keller
parent e47d51d14c
commit 31e81118dd
7 changed files with 189 additions and 299 deletions

View File

@@ -70,29 +70,26 @@ class IDF_Scm_Monotone_Stdio
if (is_resource($this->proc))
$this->stop();
$remote_db_access = Pluf::f('mtn_db_access', 'remote') == "remote";
$remote_db_access = Pluf::f('mtn_db_access', 'remote') == 'remote';
$cmd = Pluf::f('idf_exec_cmd_prefix', '') .
Pluf::f('mtn_path', 'mtn') . ' ';
$opts = Pluf::f('mtn_opts', array());
foreach ($opts as $opt)
{
foreach ($opts as $opt) {
$cmd .= sprintf('%s ', escapeshellarg($opt));
}
// FIXME: we might want to add an option for anonymous / no key
// access, but upstream bug #30237 prevents that for now
if ($remote_db_access)
{
if ($remote_db_access) {
$host = sprintf(Pluf::f('mtn_remote_url'), $this->project->shortname);
$cmd .= sprintf('automate remote_stdio %s', escapeshellarg($host));
}
else
{
$repo = sprintf(Pluf::f('mtn_repositories'), $this->project->shortname);
if (!file_exists($repo))
{
if (!file_exists($repo)) {
throw new IDF_Scm_Exception(
"repository file '$repo' does not exist"
);
@@ -101,19 +98,18 @@ class IDF_Scm_Monotone_Stdio
}
$descriptors = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w"),
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w'),
);
$env = array("LANG" => "en_US.UTF-8");
$env = array('LANG' => 'en_US.UTF-8');
$this->proc = proc_open($cmd, $descriptors, $this->pipes,
null, $env);
if (!is_resource($this->proc))
{
throw new IDF_Scm_Exception("could not start stdio process");
if (!is_resource($this->proc)) {
throw new IDF_Scm_Exception('could not start stdio process');
}
$this->_checkVersion();
@@ -154,15 +150,13 @@ class IDF_Scm_Monotone_Stdio
$read, $write = null, $except = null, 0, 20000
);
if ($streamsChanged === false)
{
if ($streamsChanged === false) {
throw new IDF_Scm_Exception(
"Could not select() on read pipe"
'Could not select() on read pipe'
);
}
if ($streamsChanged == 0)
{
if ($streamsChanged == 0) {
return false;
}
@@ -179,8 +173,7 @@ class IDF_Scm_Monotone_Stdio
$this->_waitForReadyRead();
$version = fgets($this->pipes[1]);
if ($version === false)
{
if ($version === false) {
throw new IDF_Scm_Exception(
"Could not determine stdio version, stderr is:\n".
$this->_readStderr()
@@ -191,8 +184,8 @@ class IDF_Scm_Monotone_Stdio
$m[1] != self::$SUPPORTED_STDIO_VERSION)
{
throw new IDF_Scm_Exception(
"stdio format version mismatch, expected '".
self::$SUPPORTED_STDIO_VERSION."', got '".@$m[1]."'"
'stdio format version mismatch, expected "'.
self::$SUPPORTED_STDIO_VERSION.'", got "'.@$m[1].'"'
);
}
@@ -208,33 +201,28 @@ class IDF_Scm_Monotone_Stdio
*/
private function _write(array $args, array $options = array())
{
$cmd = "";
if (count($options) > 0)
{
$cmd = "o";
foreach ($options as $k => $vals)
{
$cmd = '';
if (count($options) > 0) {
$cmd = 'o';
foreach ($options as $k => $vals) {
if (!is_array($vals))
$vals = array($vals);
foreach ($vals as $v)
{
$cmd .= strlen((string)$k) . ":" . (string)$k;
$cmd .= strlen((string)$v) . ":" . (string)$v;
foreach ($vals as $v) {
$cmd .= strlen((string)$k) . ':' . (string)$k;
$cmd .= strlen((string)$v) . ':' . (string)$v;
}
}
$cmd .= "e ";
$cmd .= 'e ';
}
$cmd .= "l";
foreach ($args as $arg)
{
$cmd .= strlen((string)$arg) . ":" . (string)$arg;
$cmd .= 'l';
foreach ($args as $arg) {
$cmd .= strlen((string)$arg) . ':' . (string)$arg;
}
$cmd .= "e\n";
if (!fwrite($this->pipes[0], $cmd))
{
if (!fwrite($this->pipes[0], $cmd)) {
throw new IDF_Scm_Exception("could not write '$cmd' to process");
}
@@ -250,11 +238,10 @@ class IDF_Scm_Monotone_Stdio
private function _readStderr()
{
$err = "";
while (($line = fgets($this->pipes[2])) !== false)
{
while (($line = fgets($this->pipes[2])) !== false) {
$err .= $line;
}
return empty($err) ? "<empty>" : $err;
return empty($err) ? '<empty>' : $err;
}
/**
@@ -273,26 +260,22 @@ class IDF_Scm_Monotone_Stdio
$output = "";
$errcode = 0;
while (true)
{
while (true) {
if (!$this->_waitForReadyRead())
continue;
$data = array(0,"",0);
$idx = 0;
while (true)
{
while (true) {
$c = fgetc($this->pipes[1]);
if ($c === false)
{
if ($c === false) {
throw new IDF_Scm_Exception(
"No data on stdin, stderr is:\n".
$this->_readStderr()
);
}
if ($c == ':')
{
if ($c == ':') {
if ($idx == 2)
break;
@@ -307,24 +290,21 @@ class IDF_Scm_Monotone_Stdio
}
// sanity
if ($this->cmdnum != $data[0])
{
if ($this->cmdnum != $data[0]) {
throw new IDF_Scm_Exception(
"command numbers out of sync; ".
"expected {$this->cmdnum}, got {$data[0]}"
'command numbers out of sync; expected '.
$this->cmdnum .', got '. $data[0]
);
}
$toRead = $data[2];
$buffer = "";
while ($toRead > 0)
{
while ($toRead > 0) {
$buffer .= fread($this->pipes[1], $toRead);
$toRead = $data[2] - strlen($buffer);
}
switch ($data[1])
{
switch ($data[1]) {
case 'w':
case 'p':
case 't':
@@ -340,11 +320,10 @@ class IDF_Scm_Monotone_Stdio
}
}
if ($errcode != 0)
{
if ($errcode != 0) {
throw new IDF_Scm_Exception(
"command '{$this->lastcmd}' returned error code $errcode: ".
implode(" ", $this->oob['e'])
implode(' ', $this->oob['e'])
);
}

View File

@@ -42,11 +42,11 @@ class IDF_Scm_Monotone_Usher
*/
public static function getServerList($state = null)
{
$conn = self::_triggerCommand("LIST $state");
if ($conn == "none")
$conn = self::_triggerCommand('LIST '.$state);
if ($conn == 'none')
return array();
return preg_split("/[ ]/", $conn);
return preg_split('/[ ]/', $conn);
}
/**
@@ -67,19 +67,18 @@ class IDF_Scm_Monotone_Usher
*/
public static function getConnectionList($server = null)
{
$conn = self::_triggerCommand("LISTCONNECTIONS $server");
if ($conn == "none")
$conn = self::_triggerCommand('LISTCONNECTIONS '.$server);
if ($conn == 'none')
return array();
$single_conns = preg_split("/[ ]/", $conn);
$single_conns = preg_split('/[ ]/', $conn);
$ret = array();
foreach ($single_conns as $conn)
{
preg_match("/\(\w+\)([^:]):(\d+)/", $conn, $matches);
foreach ($single_conns as $conn) {
preg_match('/\(\w+\)([^:]):(\d+)/', $conn, $matches);
$ret[$matches[1]][] = (object)array(
"server" => $matches[1],
"address" => $matches[2],
"port" => $matches[3],
'server' => $matches[1],
'address' => $matches[2],
'port' => $matches[3],
);
}
@@ -96,7 +95,7 @@ class IDF_Scm_Monotone_Usher
*/
public static function getStatus($server = null)
{
return self::_triggerCommand("STATUS $server");
return self::_triggerCommand('STATUS '.$server);
}
/**
@@ -110,11 +109,11 @@ class IDF_Scm_Monotone_Usher
*/
public static function matchServer($host, $pattern)
{
$ret = self::_triggerCommand("MATCH $host $pattern");
if (preg_match("/^OK: (.+)/", $ret, $m))
$ret = self::_triggerCommand('MATCH '.$host.' '.$pattern);
if (preg_match('/^OK: (.+)/', $ret, $m))
return $m[1];
preg_match("/^ERROR: (.+)/", $ret, $m);
throw new IDF_Scm_Exception("could not match server: ".$m[1]);
preg_match('/^ERROR: (.+)/', $ret, $m);
throw new IDF_Scm_Exception('could not match server: '.$m[1]);
}
/**
@@ -143,7 +142,7 @@ class IDF_Scm_Monotone_Usher
*/
public static function startServer($server)
{
return self::_triggerCommand("START $server");
return self::_triggerCommand('START '.$server);
}
/**
@@ -156,7 +155,7 @@ class IDF_Scm_Monotone_Usher
*/
public static function killServer($server)
{
return self::_triggerCommand("KILL_NOW $server") == "ok";
return self::_triggerCommand('KILL_NOW '.$server) == 'ok';
}
/**
@@ -166,7 +165,7 @@ class IDF_Scm_Monotone_Usher
*/
public static function shutDown()
{
return self::_triggerCommand("SHUTDOWN") == "ok";
return self::_triggerCommand('SHUTDOWN') == 'ok';
}
/**
@@ -176,7 +175,7 @@ class IDF_Scm_Monotone_Usher
*/
public static function startUp()
{
return self::_triggerCommand("STARTUP") == "ok";
return self::_triggerCommand('STARTUP') == 'ok';
}
/**
@@ -186,60 +185,53 @@ class IDF_Scm_Monotone_Usher
*/
public static function reload()
{
return self::_triggerCommand("RELOAD") == "ok";
return self::_triggerCommand('RELOAD') == 'ok';
}
private static function _triggerCommand($cmd)
{
$uc = Pluf::f('mtn_usher');
if (empty($uc['host']))
{
throw new IDF_Scm_Exception("usher host is empty");
if (empty($uc['host'])) {
throw new IDF_Scm_Exception('usher host is empty');
}
if (!preg_match('/^\d+$/', $uc['port']) ||
$uc['port'] == 0)
{
throw new IDF_Scm_Exception("usher port is invalid");
throw new IDF_Scm_Exception('usher port is invalid');
}
if (empty($uc['user']))
{
throw new IDF_Scm_Exception("usher user is empty");
if (empty($uc['user'])) {
throw new IDF_Scm_Exception('usher user is empty');
}
if (empty($uc['pass']))
{
throw new IDF_Scm_Exception("usher pass is empty");
if (empty($uc['pass'])) {
throw new IDF_Scm_Exception('usher pass is empty');
}
$sock = @fsockopen($uc['host'], $uc['port'], $errno, $errstr);
if (!$sock)
{
if (!$sock) {
throw new IDF_Scm_Exception(
"could not connect to usher: $errstr ($errno)"
);
}
fwrite($sock, "USERPASS {$uc['user']} {$uc['pass']}\n");
if (feof($sock))
{
fwrite($sock, 'USERPASS '.$uc['user'].' '.$uc['pass'].'\n');
if (feof($sock)) {
throw new IDF_Scm_Exception(
"usher closed the connection - probably wrong admin ".
"username or password"
'usher closed the connection - probably wrong admin '.
'username or password'
);
}
fwrite($sock, "$cmd\n");
$out = "";
while (!feof($sock))
{
fwrite($sock, $cmd.'\n');
$out = '';
while (!feof($sock)) {
$out .= fgets($sock);
}
fclose($sock);
$out = rtrim($out);
if ($out == "unknown command")
{
if ($out == 'unknown command') {
throw new IDF_Scm_Exception("unknown command: $cmd");
}