// todo terry: 上线前删除掉
public function actionVersion()
{
//$string = $this->readFile('/www/wwwroot/xxxx/services/role/ManagerService.php');
//$string = $this->addVersion($string);
//echo $string;exit;
$basePatch = '/www/wwwroot/xxx.xxx.cn';
//1.便利所有的php文件
FileHelper::findFiles($basePatch, [
'filter' => function ($path) use ($basePatch) {
//echo $path . "\n";
if (!file_exists($path)) {
return;
}
$extension = $this->getExtension($path);
if ($extension != 'php') {
return;
}
if (strstr($path, $basePatch .'/vendor')) {
return;
}
if ($path == $basePatch .'/modules/console/controllers/SystemController.php') {
return;
}
$string = $this->readFile($path);
if (!$string) {
return;
}
$string = $this->addVersion($string);
$this->writeFile($path, $string);
echo $path . "\n";
//exit;
}
]);
//2.替换字符
//3.覆盖写入
}
private function writeFile($file_path, $txt)
{
$myfile = fopen($file_path, "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
}
private function readFile($file_path) {
if(file_exists($file_path)){
return file_get_contents($file_path);//将整个文件内容读入到一个字符串中
}
}
private function addVersion($string)
{
$pattern = '/^<\?php([\s\S]*?)\*\/([\s\S]*?)\nnamespace\s{1}/i';
$replacement = '<?php
namespace ';
$string = preg_replace($pattern, $replacement, $string);
$pattern = '/^<\?php{1}/i';
$replacement = '<?php
/**
* 商城系统1.0
* @author xxxxx有限公司
* @copyright Copyright (c) 2015-2021
* @link https://www.xxxx.com
*/
';
$string = preg_replace($pattern, $replacement, $string);
return $string;
}
private function getExtension($file)
{
return substr(strrchr($file, '.'), 1);
}