项目中最主要的就是开发接口,开发完接口以后,接口文档如何根据svn实时变化,而自动生成呢?
今天我们就来讨论一下这个事情,先说说场景:
接口文档 读取 json 文件,自动遍历数据,显示在页面上。 页面提供一个 接口查询功能
查询功能:遍历整个 json 文件 找到对应数据,并重新赋值到一个新的数组。然后显示这个数组内容即可。
好了,看到 json 文件,那么这个文件是如何生成的?
都生成哪些文件的内容呢?
文件如下:
Controller
Models
目前就是这两类文件.
/**
* 方法使用
* @function test
* @param string $name <required> 用户名
* @return array
*/
public function test($name,$age){
}
就是上面的文件格式。
我们通过类的反射可以很方便的拿到这段方法的描述,然后 生成json 文件。
问题:什么情况下去自动生成json 文件?
在svn 版本发生变化的时候,去生成
php文档:http://php.net/manual/zh/svn.constants.php#svn.constants.auth
phpSVN文档:http://php.net/manual/zh/function.svn-auth-set-parameter.php
<?php $revision_array = array(3099, 3339, 2573,3351); /* svn的版本号 */ $svnPeer = new svnPeer(); $filelist = $svnPeer->_get_file_list($revision_array); if (!empty($filelist)) { $lbv_export = $svnPeer->_svn_export_list($filelist, 'trunk889'); if (true === $lbv_export) { echo '导出成功'; } else { echo '导出失败'; } } else { echo '获取文件列表失败'; } /** * php操作svn类,全部利用php内置的svn函数 * * @author wengxianhu * @date 2013-08-05 */ class svnPeer { /* svn用户名 */ public $svn_user = 'wengxianhu'; /* svn密码 */ public $svn_password = 'wxh025'; /* 来源路径 */ public $source_path = '/var/www/trunk/'; /* 目标路径 */ public $dest_path = '/var/www/'; /** * 构造函数 * * @author wengxianhu * @date 2013-08-05 * @return void */ public function __construct () { $this->_svn_connect(); } /** * 配置SVN使用默认的用户名和密码 * * @author wengxianhu * @date 2013-08-05 * @return void */ public function _svn_connect () { svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $this->svn_user); svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $this->svn_password); } /** * 根据svn版本号获取所有的文件路径 * * @author wengxianhu * @date 2013-08-05 * @param array $revision_array 版本号列表 * @return array */ public function _get_file_list ($revision_array = array()) { if (!empty($revision_array)) { $filelist = array(); $log_list = array(); rsort($revision_array, SORT_NUMERIC); foreach ($revision_array as $_k=>$_v) { $log_list = @svn_log($this->source_path, $_v, $_v); if (false === $log_list) { return false; } else { $log_list = current($log_list); foreach ($log_list['paths'] as $s_k=>$s_v) { $s_v['path'] = preg_replace('/^\/[^\/]+\/(.*)$/i', '$1', $s_v['path']); $filetmp = $s_v['path']; if (is_file($this->source_path . $s_v['path'])) { if (false === $this->multidimensional_search($filelist, array('filepath'=>$s_v['path']))) { $filelist[] = array( 'revision_no' => $log_list['rev'], 'filepath' => $s_v['path'] ); } } } } } return $filelist; } } /** * 对多维数组进行搜索 * * @author wengxianhu * @date 2013-08-05 * @param array $parents 被搜索数组 * @param array $searched 搜索数组 * @return boolean */ public function multidimensional_search ($parents = array(), $searched = array()) { if (empty($searched) || empty($parents)) { return false; } foreach ($parents as $key => $value) { $exists = true; foreach ($searched as $skey => $svalue) { $exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue); } if ($exists) { return $key; } } return false; } /** * 根据svn版本号导出相应的文件 * * @author wengxianhu * @date 2013-08-05 * @param array $file_array 文件路径名 * @param string $package_name 包名 * @return boolean 成功为true,失败为false */ public function _svn_export_list ($file_array = array(), $package_name = '') { $info = true; $this->dest_path = $this->dest_path . $package_name; if (file_exists($this->dest_path)) { $this->delDirAndFile($this->dest_path); } foreach ($file_array as $_k=>$_v) { $source_files = $this->source_path . $_v['filepath']; $dest_files = $this->dest_path . '/' . $_v['filepath']; $revision_no = (int)$_v['revision_no']; $this->_mkdirm(dirname($dest_files)); $lbv_export = @svn_export($source_files, $dest_files, false, $revision_no); if (false === $lbv_export) { $info = false; break; } } return $info; } /** * 创建文件夹 * * @author wengxianhu * @date 2013-08-05 * string $path 文件路径(不包括文件名) * return void */ public function _mkdirm ($path) { if (!file_exists($path)) { $this->_mkdirm(dirname($path)); mkdir($path, 0755); } } /** * 循环删除目录和文件函数 * * @author wengxianhu * @date 2013-08-15 * @param string $dirName 目录路径 * return array */ public function delDirAndFile($dirName) { if ( $handle = opendir( "$dirName" ) ) { while ( false !== ( $item = readdir( $handle ) ) ) { if ( $item != "." && $item != ".." ) { if ( is_dir( "$dirName/$item" ) ) { $this->delDirAndFile( "$dirName/$item" ); } else { unlink( "$dirName/$item" ); } } } closedir( $handle ); rmdir( $dirName ); } } }
代码引用:https://www.jb51.net/article/142645.htm
代码封装2
define('SVN_USERNAME',''); define('SVN_PASSWORD',''); class svnUtil { /** * List directory entries in the repository * * @param string a specific project repository path * @return bool true, if validated successfully, otherwise false */ static public function ls($repository) { $command = "svn ls " . $repository; $output = svnUtil::runCmd($command); $output = implode("<br>", $output); if (strpos($output, 'non-existent in that revision')) { return false; } return "<br>" . $command . "<br>" . $output; } /** * Duplicate something in working copy or repository, remembering history * * @param $src * @param $dst * @param $comment string specify log message * @return bool true, if copy successfully, otherwise return the error message * * @todo comment need addslashes for svn commit */ static public function copy($src, $dst, $comment) { $command = "svn cp $src $dst -m '$comment'"; $output = svnUtil::runCmd($command); $output = implode("<br>", $output); if (strpos($output, 'Committed revision')) { return true; } return "<br>" . $command . "<br>" . $output; } /** * Remove files and directories from version control * * @param $url * @return bool true, if delete successfully, otherwise return the error message * * @todo comment need addslashes for svn commit */ static public function delete($url, $comment) { $command = "svn del $url -m '$comment'"; $output = svnUtil::runCmd($command); $output = implode('<br>', $output); if (strpos($output, 'Committed revision')) { return true; } return "<br>" . $command . "<br>" . $output; } /** * Move and/or rename something in working copy or repository * * @param $src string trunk path * @param $dst string new branch path * @param $comment string specify log message * @return bool true, if move successfully, otherwise return the error message * * @todo comment need addslashes for svn commit */ static public function move($src, $dst, $comment) { $command = "svn mv $src $dst -m '$comment'"; $output = svnUtil::runCmd($command); $output = implode('<br>', $output); if (strpos($output, 'Committed revision')) { return true; } return "<br>" . $command . "<br>" . $output; } /** * Create a new directory under version control * * @param $url string * @param $comment string the svn message * @return bool true, if create successfully, otherwise return the error message * * @todo comment need addslashes for svn commit */ static public function mkdir($url, $comment) { $command = "svn mkdir $url -m '$comment'"; $output = svnUtil::runCmd($command); $output = implode('<br>', $output); if (strpos($output, 'Committed revision')) { return true; } return "<br>" . $command . "<br>" . $output; } static public function diff($pathA, $pathB) { $output = svnUtil::runCmd("svn diff $pathA $pathB"); return implode('<br>', $output); } static public function checkout($url, $dir) { $command = "cd $dir && svn co $url"; $output = svnUtil::runCmd($command); $output = implode('<br>', $output); if (strstr($output, 'Checked out revision')) { return true; } return "<br>" . $command . "<br>" . $output; } static public function update($path) { $command = "cd $path && svn up"; $output = svnUtil::runCmd($command); $output = implode('<br>', $output); preg_match_all("/[0-9]+/", $output, $ret); if (!$ret[0][0]){ return "<br>" . $command . "<br>" . $output; } return $ret[0][0]; } static public function merge($revision, $url, $dir) { $command = "cd $dir && svn merge -r1:$revision $url"; $output = implode('<br>', svnUtil::runCmd($command)); if (strstr($output, 'Text conflicts')) { return 'Command: ' . $command .'<br>'. $output; } return true; } static public function commit($dir, $comment) { $command = "cd $dir && svn commit -m'$comment'"; $output = implode('<br>', svnUtil::runCmd($command)); if (strpos($output, 'Committed revision') || empty($output)) { return true; } return $output; } static public function getStatus($dir) { $command = "cd $dir && svn st"; return svnUtil::runCmd($command); } static public function hasConflict($dir) { $output = svnUtil::getStatus($dir); foreach ($output as $line){ if ('C' == substr(trim($line), 0, 1) || ('!' == substr(trim($line), 0, 1))){ return true; } } return false; } /** * Show the log messages for a set of path with XML * * @param path string * @return log message string */ static public function getLog($path) { $command = "svn log $path --xml"; $output = svnUtil::runCmd($command); return implode('', $output); } static public function getPathRevision($path) { $command = "svn info $path --xml"; $output = svnUtil::runCmd($command); $string = implode('', $output); $xml = new SimpleXMLElement($string); foreach ($xml->entry[0]->attributes() as $key=>$value){ if ('revision' == $key) { return $value; } } } static public function getHeadRevision($path) { $command = "cd $path && svn up"; $output = svnUtil::runCmd($command); $output = implode('<br>', $output); preg_match_all("/[0-9]+/", $output, $ret); if (!$ret[0][0]){ return "<br>" . $command . "<br>" . $output; } return $ret[0][0]; } /** * Run a cmd and return result * * @param string command line * @param boolen true need add the svn authentication * @return array the contents of the output that svn execute */ static protected function runCmd($command) { $authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive ';//--config-dir '.SVN_CONFIG_DIR.'.subversion'; exec($command . $authCommand . " 2>&1", $output); return $output; } }
问题:如何检测到svn版本发生了变化?
linxu Crontab 定时任务
其他php类专题
更多关于PHP相关内容感兴趣的读者可查看本站专题:
《PHP目录操作技巧汇总》、《php文件操作总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》