免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2272 | 回复: 5
打印 上一主题 下一主题

【运维管理】使用PHP操作SVN基础类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-20 20:16 |只看该作者 |倒序浏览

【运维管理】使用PHP操作SVN基础类








我不想使用PECL包中php svn扩展,一方面对PHP版本有要求,另外一方面对svn代码包有限制,不过最严重的是:对服务器PHP环境的依赖。

一个基础的svn类,应该实现基本的svn操作,如:checkout、update、commit、copy(最常用到)

【目的】

作为一个码农,写出一个被众人使用的组件、类库总是一件令人兴奋的事情,另外,代码的重用性也提倡将某一个方面的操作使用代码封装。

在自动化软件管理中,将代码从svn获取到当前地点,然后再对代码进行部署,并且通过可视化界面对此流程进行管理,这是个不错的想法。另外,通过对svn的管理,实现对代码版本的管理也是一件很有意义的事情。定期去检查代码,然后通知管理员改动信息,都是很好的想法。

【流程】

我想过从svn的协议部分下手,太过复杂,想我几天内是没有办法把它弄完的。因此,通过PHP的shell_exec()下手是个不错的选择。




[php] view plaincopy
  1. 01.$result = @shell_exec("svn {$opts}" . $cmd . ' 2> ' . $this->errorfile);  
复制代码
通过php执行shell,可以有各种空间来进行扩展和使用,但是有如下几点需要注意:

【环境依赖】

依赖Linux环境,

对操作目录、临时目录有操作权限

如果是web环境,同时需要保证apache的执行者具有权限。

【错误处理】

在调用SHELL的时候,倘若svn出错(这是不可避免的),错误不会通过标准输出(STDOUT)输出,而是通过标准错误(STDERR)来输出,标准错误不会被PHP捕捉到,但是会被apache或者操作系统捕捉到,造成PHP执行失败和无法显示的问题。

在这里,我选择了利用shell将错误信息重定向到一个临时文件,然后去读取临时文件来判断执行时是否有错误发生。




[php] view plaincopy
  1. 01.       private function shell($cmd){  
  2. 02.    $opts = '';  
  3. 03.    foreach($this->opts as $key => $item){  
  4. 04.        if(!emptyempty($item)){  
  5. 05.            $opts .= "--{$key} {$item} ";  
  6. 06.        }  
  7. 07.    }  
  8. 08.    $result = @shell_exec("svn {$opts}" . $cmd . ' 2> ' . $this->errorfile);  
  9. 09.    if($this->isResultError()){  
  10. 10.        return false;  
  11. 11.    }  
  12. 12.    return $result;  
  13. 13.}
复制代码
【选项】

选项包括:svn的用户名和密码,或者存储svn基本数据的目录(当然前提是有权限读取),可以通过设置来进行




[php] view plaincopy
  1. 01.       public function setOptions($options = array()){  
  2. 02.    $this->opts = array_merge($this->opts, $options);  
  3. 03.}  
复制代码
【最终】

通过执行测试 和 phpdoc生成文档,即可完成一个基础类的创建

【开闭原则】

也许你知道,也许也不知道,说实话,我对它的理解仅仅局限于字面上。

使每一个与你共事的同事了解开闭原则是一件很重要的事,它能帮助你编写更多可重用的代码类库。

开(对扩展开放)闭(对修改关闭)保证了代码的可重用性和可维护性。




最后,将整个类库贴上。




[php] view plaincopy
  1. 01.<?php  
  2. 02./**
  3. 03. * For svn use by php
  4. 04. *  
  5. 05. * This class allow you checkout svn files through web
  6. 06. * And allow you update files
  7. 07. * I hope in the later version, it can handle commit and other actions
  8. 08. *  
  9. 09. * @example
  10. 10. * <code>
  11. 11. * $phpsvn = new phpsvn();
  12. 12. * $phpsvn->setOptions(array(
  13. 13. *  'username' => 'yourname',
  14. 14. *  'password' => 'yourpassword'
  15. 15. * ));
  16. 16. * $phpsvn->setPath($svnpath, $localpath);
  17. 17. * $r = $phpsvn->checkout();
  18. 18. * if($r === false){
  19. 19. *  echo $phpsvn->error();
  20. 20. * }else{
  21. 21. *  print_r($r);
  22. 22. * }
  23. 23. * </code>
  24. 24. * @author hufeng<hufeng@staff.sina.com.cn>
  25. 25. * @version v1.0
  26. 26. * @access sflib
  27. 27. * @package sflib
  28. 28. * @category phpsvn
  29. 29. * @copyright 2012-2013@copyright sflib  
  30. 30. */  
  31. 31.  
  32. 32.  
  33. 33.class phpsvn  
  34. 34.{  
  35. 35.    /**
  36. 36.     * svn command options
  37. 37.     *  
  38. 38.     * @var array
  39. 39.     */  
  40. 40.    private $opts = array(  
  41. 41.        'username' => '',  
  42. 42.        'password' => '',  
  43. 43.        'config-dir' => '',  ///usr/home/finance/.subversion  
  44. 44.    );  
  45. 45.      
  46. 46.    /**
  47. 47.     * error log file
  48. 48.     *  
  49. 49.     * Will not save data in this file
  50. 50.     * Just a tmp file store tmp error message
  51. 51.     * @var string
  52. 52.     */  
  53. 53.    private $errorfile = '/tmp/phpsvn.err';  
  54. 54.      
  55. 55.    /**
  56. 56.     * Error message
  57. 57.     *  
  58. 58.     * If no error, error = ''
  59. 59.     * @var string
  60. 60.     */  
  61. 61.    private $error = '';  
  62. 62.      
  63. 63.    /**
  64. 64.     * The value to return
  65. 65.     *  
  66. 66.     * @var array
  67. 67.     */  
  68. 68.    private $retValue = array();  
  69. 69.      
  70. 70.    /**
  71. 71.     * Svn path
  72. 72.     *  
  73. 73.     * @var string
  74. 74.     */  
  75. 75.    private $svnpath = '';  
  76. 76.      
  77. 77.    /**
  78. 78.     * Local file path where file stored
  79. 79.     *  
  80. 80.     * @var string
  81. 81.     */  
  82. 82.    private $targetPath = '.';  
  83. 83.      
  84. 84.    /**
  85. 85.     * actions tags
  86. 86.     *  
  87. 87.     * @var array
  88. 88.     */  
  89. 89.    private $shortTags = array('a', 'u', 'd');  
  90. 90.      
  91. 91.    /**
  92. 92.     * Set opts of svn command
  93. 93.     *  
  94. 94.     * allow opts:
  95. 95.     * username : your svn username
  96. 96.     * password : your svn password
  97. 97.     * config-dir : your execute user config dir
  98. 98.     * @param array $options
  99. 99.     */  
  100. 100.    public function setOptions($options = array()){  
  101. 101.        $this->opts = array_merge($this->opts, $options);  
  102. 102.    }  
  103. 103.      
  104. 104.    /**
  105. 105.     * Set svn path & localpath
  106. 106.     *  
  107. 107.     * @param string $svnpath svn path
  108. 108.     * @param string $targetpath local path
  109. 109.     * @return void
  110. 110.     */  
  111. 111.    public function setPath($svnpath, $targetpath = '.'){  
  112. 112.        $this->svnpath = $svnpath;  
  113. 113.        $this->targetPath = realpath($targetpath);  
  114. 114.    }  
  115. 115.      
  116. 116.    /**
  117. 117.     * update from server
  118. 118.     *  
  119. 119.     * @return mixed array on success or false on error
  120. 120.     */  
  121. 121.    public function update(){  
  122. 122.        return $this->doCmd('up');  
  123. 123.    }  
  124. 124.      
  125. 125.    /**
  126. 126.     * commit file to server
  127. 127.     *  
  128. 128.     * @return mixed array on success or false on error
  129. 129.     */  
  130. 130.    public function commit(){  
  131. 131.        return $this->doCmd('ci');  
  132. 132.    }  
  133. 133.      
  134. 134.    /**
  135. 135.     * Add file to svn
  136. 136.     *  
  137. 137.     * @param string $file filename
  138. 138.     * @return mixed array on success or false on error
  139. 139.     */  
  140. 140.    public function add($file){  
  141. 141.        return $this->doCmd('add', $file);  
  142. 142.    }  
  143. 143.  
  144. 144.    /**
  145. 145.     * Chectout file from svn to local
  146. 146.     *  
  147. 147.     * @return mixed array on success or false on error
  148. 148.     */  
  149. 149.    public function checkout(){  
  150. 150.        return $this->doCmd('co', $this->svnpath);  
  151. 151.        //Checked out revision 240772  
  152. 152.    }  
  153. 153.      
  154. 154.    /**
  155. 155.     * Execute command for svn
  156. 156.     *  
  157. 157.     * support commands:add/checkout(co)/cleanup/commit(ci)/copy(cp)/delete(del,remove,rm)/diff(di)/update (up)
  158. 158.     * todo commands:export
  159. 159.     * help (?, h)
  160. 160.     * import
  161. 161.     * info
  162. 162.     * list (ls)
  163. 163.     * lock
  164. 164.     * log
  165. 165.     * merge
  166. 166.     * mkdir
  167. 167.     * move (mv, rename, ren)
  168. 168.     * propdel (pdel, pd)
  169. 169.     * propedit (pedit, pe)
  170. 170.     * propget (pget, pg)
  171. 171.     * proplist (plist, pl)
  172. 172.     * propset (pset, ps)
  173. 173.     * resolved
  174. 174.     * revert
  175. 175.     * status (stat, st)
  176. 176.     * switch (sw)
  177. 177.     * @param string $cmd
  178. 178.     * @param string $param
  179. 179.     */  
  180. 180.    public function doCmd($cmd, $param = ''){  
  181. 181.        chdir($this->targetPath);  
  182. 182.        $cmd = "{$cmd} {$param}";  
  183. 183.        $result = $this->shell($cmd);  
  184. 184.        return $this->result($result);  
  185. 185.    }  
  186. 186.      
  187. 187.    /**
  188. 188.     * Error message last time
  189. 189.     *  
  190. 190.     * @return string error message
  191. 191.     */  
  192. 192.    public function error(){  
  193. 193.        return $this->error;  
  194. 194.    }  
  195. 195.      
  196. 196.    /**
  197. 197.     * Format the result handle
  198. 198.     *  
  199. 199.     * @param string $result result string
  200. 200.     * @return string
  201. 201.     */  
  202. 202.    private function result($result){  
  203. 203.        if($result === false){  
  204. 204.            return false;  
  205. 205.        }  
  206. 206.        foreach(explode("\n", $result) as $line){  
  207. 207.            $line = trim($line);  
  208. 208.            $this->retLine($line);  
  209. 209.        }  
  210. 210.        return $this->retValue;  
  211. 211.    }  
  212. 212.      
  213. 213.    private function retLine($line){  
  214. 214.        $line = strtolower($line);  
  215. 215.        if(emptyempty($line)){  
  216. 216.            return;  
  217. 217.        }  
  218. 218.        $retValue = array();  
  219. 219.        if(in_array($line[0], $this->shortTags)){  
  220. 220.            $retValue['a'] = $line[0];  
  221. 221.            $retValue['v'] = trim(substr($line, 2));  
  222. 222.        }else{  
  223. 223.            preg_match('/([0-9]+)/', $line, $match);  
  224. 224.            $num = intval($match[0]);  
  225. 225.            if($num > 0){  
  226. 226.                $retValue['a'] = 'v';  
  227. 227.                $retValue['v'] = $num;  
  228. 228.            }  
  229. 229.        }  
  230. 230.        $this->retValue[] = $retValue;  
  231. 231.    }  
  232. 232.      
  233. 233.    /**
  234. 234.     * Get svn file version from result line
  235. 235.     *  
  236. 236.     * @param string $line result line
  237. 237.     * @return mixed version number or false if on error
  238. 238.     */  
  239. 239.    private function getVersionByLine($line){  
  240. 240.        $line = trim(strtolower($line));  
  241. 241.        if(preg_match('/([0-9]+)/', $line, $match)){  
  242. 242.            return $match[0];  
  243. 243.        }  
  244. 244.        return false;  
  245. 245.    }  
  246. 246.    /**
  247. 247.     * Exec shell command
  248. 248.     *  
  249. 249.     * @access private
  250. 250.     * @param string $cmd command to be executed
  251. 251.     * @return string result string should been displayed on stdout,  
  252. 252.     * @return return false if on error
  253. 253.     */  
  254. 254.    private function shell($cmd){  
  255. 255.        $opts = '';  
  256. 256.        foreach($this->opts as $key => $item){  
  257. 257.            if(!emptyempty($item)){  
  258. 258.                $opts .= "--{$key} {$item} ";  
  259. 259.            }  
  260. 260.        }  
  261. 261.        $result = @shell_exec("svn {$opts}" . $cmd . ' 2> ' . $this->errorfile);  
  262. 262.        if($this->isResultError()){  
  263. 263.            return false;  
  264. 264.        }  
  265. 265.        return $result;  
  266. 266.    }  
  267. 267.      
  268. 268.    /**
  269. 269.     * Check if on error
  270. 270.     *  
  271. 271.     * @param string $result shell result string
  272. 272.     * @return boolen
  273. 273.     */  
  274. 274.    private function isResultError(){  
  275. 275.        $this->error = file_get_contents($this->errorfile);  
  276. 276.        if(emptyempty($this->error)){  
  277. 277.            return false;  
  278. 278.        }  
  279. 279.        return true;  
  280. 280.    }  
  281. 281.}  
  282. 282.  
  283. 283.?>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-20 20:17 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-01-22 11:12 |只看该作者
收藏收藏

论坛徽章:
0
4 [报告]
发表于 2012-01-22 12:30 |只看该作者
正好可以学习学习。

论坛徽章:
3
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:19
5 [报告]
发表于 2012-01-22 23:01 |只看该作者
楼主想法很好,直接不行,间接也很好,死马当活马医!

论坛徽章:
0
6 [报告]
发表于 2012-01-29 11:06 |只看该作者
有想法,了解了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP