标题: php部署svn
时间: 2014-04-25
<?php
/**
* For svn use by php
*
* This class allow you checkout svn files through web
* And allow you update files
* I hope in the later version, it can handle commit and other actions
*
* @example
* <code>
* $phpsvn = new phpsvn();
* $phpsvn->setOptions(array(
* 'username' => 'yourname',
* 'password' => 'yourpassword'
* ));
* $phpsvn->setPath($svnpath, $localpath);
* $r = $phpsvn->checkout();
* if($r === false){
* echo $phpsvn->error();
* }else{
* print_r($r);
* }
* </code>
* @author hufeng<hufeng@staff.sina.com.cn>
* @version v1.0
* @access sflib
* @package sflib
* @category phpsvn
* @copyright 2012-2013@copyright sflib
*/
class phpsvn
{
/**
* svn command options
*
* @var array
*/
private $opts = array(
'username' => '',
'password' => '',
'config-dir' => '', ///usr/home/finance/.subversion
);
/**
* error log file
*
* Will not save data in this file
* Just a tmp file store tmp error message
* @var string
*/
private $errorfile = '/tmp/phpsvn.err';
/**
* Error message
*
* If no error, error = ''
* @var string
*/
private $error = '';
/**
* The value to return
*
* @var array
*/
private $retValue = array();
/**
* Svn path
*
* @var string
*/
private $svnpath = '';
/**
* Local file path where file stored
*
* @var string
*/
private $targetPath = '.';
/**
* actions tags
*
* @var array
*/
private $shortTags = array('a', 'u', 'd');
/**
* Set opts of svn command
*
* allow opts:
* username : your svn username
* password : your svn password
* config-dir : your execute user config dir
* @param array $options
*/
public function setOptions($options = array()){
$this->opts = array_merge($this->opts, $options);
}
/**
* Set svn path & localpath
*
* @param string $svnpath svn path
* @param string $targetpath local path
* @return void
*/
public function setPath($svnpath, $targetpath = '.'){
$this->svnpath = $svnpath;
$this->targetPath = realpath($targetpath);
}
/**
* update from server
*
* @return mixed array on success or false on error
*/
public function update(){
return $this->doCmd('up');
}
/**
* commit file to server
*
* @return mixed array on success or false on error
*/
public function commit(){
return $this->doCmd('ci');
}
/**
* Add file to svn
*
* @param string $file filename
* @return mixed array on success or false on error
*/
public function add($file){
return $this->doCmd('add', $file);
}
/**
* Chectout file from svn to local
*
* @return mixed array on success or false on error
*/
public function checkout(){
return $this->doCmd('co', $this->svnpath);
//Checked out revision 240772
}
/**
* Execute command for svn
*
* support commands:add/checkout(co)/cleanup/commit(ci)/copy(cp)/delete(del,remove,rm)/diff(di)/update (up)
* todo commands:export
* help (?, h)
* import
* info
* list (ls)
* lock
* log
* merge
* mkdir
* move (mv, rename, ren)
* propdel (pdel, pd)
* propedit (pedit, pe)
* propget (pget, pg)
* proplist (plist, pl)
* propset (pset, ps)
* resolved
* revert
* status (stat, st)
* switch (sw)
* @param string $cmd
* @param string $param
*/
public function doCmd($cmd, $param = ''){
chdir($this->targetPath);
$cmd = "{$cmd} {$param}";
$result = $this->shell($cmd);
return $this->result($result);
}
/**
* Error message last time
*
* @return string error message
*/
public function error(){
return $this->error;
}
/**
197. * Format the result handle
198. *
199. * @param string $result result string
200. * @return string
*/
private function result($result){
if($result === false){
return false;
}
foreach(explode("\n", $result) as $line){
$line = trim($line);
$this->retLine($line);
}
return $this->retValue;
}
private function retLine($line){
$line = strtolower($line);
if(emptyempty($line)){
return;
}
$retValue = array();
if(in_array($line[0], $this->shortTags)){
$retValue['a'] = $line[0];
$retValue['v'] = trim(substr($line, 2));
}else{
preg_match('/([0-9]+)/', $line, $match);
$num = intval($match[0]);
if($num > 0){
$retValue['a'] = 'v';
$retValue['v'] = $num;
}
}
$this->retValue[] = $retValue;
}
/**
234. * Get svn file version from result line
235. *
236. * @param string $line result line
237. * @return mixed version number or false if on error
*/
private function getVersionByLine($line){
$line = trim(strtolower($line));
if(preg_match('/([0-9]+)/', $line, $match)){
return $match[0];
}
return false;
}
/**
247. * Exec shell command
248. *
249. * @access private
250. * @param string $cmd command to be executed
251. * @return string result string should been displayed on stdout,
252. * @return return false if on error
*/
private function shell($cmd){
$opts = '';
foreach($this->opts as $key => $item){
if(!emptyempty($item)){
$opts .= "--{$key} {$item} ";
}
}
$result = @shell_exec("svn {$opts}" . $cmd . ' 2> ' . $this->errorfile);
if($this->isResultError()){
return false;
}
return $result;
}
/**
269. * Check if on error
270. *
271. * @param string $result shell result string
272. * @return boolen
*/
private function isResultError(){
$this->error = file_get_contents($this->errorfile);
if(emptyempty($this->error)){
return false;
}
return true;
}
}
?>
『回复列表(1|隐藏机器人聊天)』