class
ReplaceVersion{
protected
$filePostFixs
=
array
();
protected
$versionName
= null;
protected
$version
= null;
protected
$path
= null;
public
function
__construct(
$configs
,
$profix
,
$path
){
if
(!
$this
->isCanRun()) {
$this
->error(
'必须在内网环境 10.10.0开头才可运行'
);
}
$this
->setVersion(
$configs
);
$this
->setFilePostFix(
$profix
);
$this
->path =
$path
;
}
protected
function
isCanRun(){
if
(
strpos
(
$_SERVER
[
'HTTP_HOST'
],
'10.10.0'
) !== false) {
return
true;
}
return
false;
}
protected
function
callbackScript(
$match
){
$str
=
$match
[0];
$pattern
=
'/(<script.*?src=\")(.*)?(\"><\/script>)/'
;
return
$this
->callbackMatch(
$str
,
$pattern
);
}
protected
function
callbackCss(
$match
){
$str
=
$match
[0];
$pattern
=
'/(<link.*?href=\")(.*)?(\".*?>)/'
;
return
$this
->callbackMatch(
$str
,
$pattern
);
}
protected
function
callbackMatch(
$str
,
$pattern
){
switch
(
$this
->dealFlag) {
case
'replace'
:
return
$this
->replaceCallbackMatch(
$str
,
$pattern
);
case
'clean'
:
return
$this
->cleanCallbackMatch(
$str
,
$pattern
);
default
:
$this
->error(
'非法模式'
);
}
}
protected
function
replaceCallbackMatch(
$str
,
$pattern
){
if
(!preg_match(
$pattern
,
$str
,
$third
)) {
return
$str
;
}
$arr
=
explode
(
'?'
,
$third
[2]);
$len
=
count
(
$arr
);
$versionName
=
$this
->versionName;
$version
=
$this
->version;
if
(
$len
=== 1) {
$arr
[0] .=
'?'
.
$versionName
.
'='
.
$version
;
}
else
{
if
(preg_match(
'/(^|\&)'
.
$versionName
.
'=(.*?)($|\&)/'
,
$arr
[1])) {
$arr
[1] = preg_replace(
'/(^|\&)'
.
$versionName
.
'=(.*?)($|\&)/'
,
'$1'
.
$versionName
.
'='
.
$version
.
'$3'
,
$arr
[1]);
$arr
[0] .=
'?'
.
$arr
[1];
}
else
{
$arr
[0] .=
'?'
.
$arr
[1].
'&'
.
$versionName
.
'='
.
$version
;
}
}
return
$third
[1].
$arr
[0].
$third
[3];
}
protected
function
cleanCallbackMatch(
$str
,
$pattern
){
if
(!preg_match(
$pattern
,
$str
,
$third
)) {
return
$str
;
}
$arr
=
explode
(
'?'
,
$third
[2]);
$len
=
count
(
$arr
);
$versionName
=
$this
->versionName;
if
(
$len
> 1 &&
strpos
(
$arr
[1],
$versionName
.
'='
) !== false) {
$arr
[1] = preg_replace(
'/(^|\&)'
.
$versionName
.
'=(.*?)($|\&)/'
,
'$1'
,
$arr
[1]);
substr
(
$arr
[1], -1) ===
'&'
&& (
$arr
[1] =
substr
(
$arr
[1], 0, -1));
$arr
[0] .=
strlen
(
$arr
[1]) > 0 ?
'?'
.
$arr
[1] :
''
;
$str
=
$third
[1].
$arr
[0].
$third
[3];
}
return
$str
;
}
protected
function
run(){
if
(
$this
->path ==
''
) {
$this
->error(
'empty path'
);
return
;
}
if
(
is_dir
(
$this
->path)) {
$this
->setDirFilesVersion(
$this
->path );
}
else
if
(
is_file
(
$this
->path)){
$this
->setFileVersion(
$this
->path );
}
else
{
$this
->error(
'error path'
);
}
}
public
function
replace(){
$this
->dealFlag =
'replace'
;
$this
->run();
echo
'replace success'
;
}
public
function
clean(){
$this
->dealFlag =
'clean'
;
$this
->run();
echo
'clean success'
;
}
protected
function
success(){
}
protected
function
error(
$errorMsg
){
echo
$errorMsg
;
exit
();
}
protected
function
setDirFilesVersion(
$dir
){
$handle
= null;
$file
= null;
if
(
$handle
= opendir(
$dir
)) {
while
( false !== (
$file
= readdir(
$handle
)) ) {
if
(
$file
===
'.'
||
$file
===
'..'
||
strpos
(
$file
,
'.'
) === -1 ) {
continue
;}
$this
->setFileVersion(
$file
);
}
}
}
protected
function
setFileVersion(
$file
){
$temp
= null;
$temp
=
explode
(
'.'
,
$file
) ;
if
( !
$this
->isNeedReplacePostFix(
array_pop
(
$temp
)) ) {
return
;}
$content
= null;
$content
=
file_get_contents
(
$file
);
$content
= preg_replace_callback(
'/<script.*?><\/script>/'
,
array
(&
$this
,
'callbackScript'
),
$content
);
$content
= preg_replace_callback(
'/<link.*?type="text\/css".*?>/'
,
array
(&
$this
,
'callbackCss'
),
$content
);
file_put_contents
(
$file
,
$content
);
}
protected
function
setVersion(
$configs
){
if
(
is_array
(
$configs
) &&
$configs
> 0) {
foreach
(
$configs
as
$key
=>
$value
) {
$this
->version =
$value
;
$this
->versionName =
$key
;
}
}
else
if
(
is_string
(
$configs
) &&
$configs
!=
''
){
$configs
=
explode
(
','
,
$configs
);
$this
->versionName =
$configs
[0];
count
(
$configs
) == 2 && (
$this
->version =
$configs
[1]);
}
else
{
$this
->error(
'the version is empty'
);
}
}
protected
function
isNeedReplacePostFix(
$profix
){
if
(in_array(
$profix
,
$this
->filePostFixs)) {
return
true;
}
return
false;
}
public
function
setFilePostFix(
$profix
){
if
(
is_array
(
$profix
)) {
count
(
$profix
) > 0 && (
$this
->filePostFixs =
array_merge
(
$this
->filePostFixs,
$profix
) );
}
else
if
(
is_string
(
$profix
)){
$this
->filePostFixs[] =
$profix
;
}
}
}