Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
c7c7683f
Commit
c7c7683f
authored
Aug 19, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added StringHelper::diff().
parent
829c10f4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
34 deletions
+51
-34
ErrorHandler.php
framework/yii/base/ErrorHandler.php
+1
-3
CodeFile.php
framework/yii/gii/CodeFile.php
+3
-1
TextDiff.php
framework/yii/gii/components/TextDiff.php
+0
-30
StringHelperBase.php
framework/yii/helpers/StringHelperBase.php
+47
-0
No files found.
framework/yii/base/ErrorHandler.php
View file @
c7c7683f
...
...
@@ -114,8 +114,7 @@ class ErrorHandler extends Component
'exception'
=>
$exception
,
));
}
}
else
{
if
(
$exception
instanceof
Arrayable
)
{
}
elseif
(
$exception
instanceof
Arrayable
)
{
$response
->
data
=
$exception
;
}
else
{
$response
->
data
=
array
(
...
...
@@ -125,7 +124,6 @@ class ErrorHandler extends Component
'code'
=>
$exception
->
getCode
(),
);
}
}
if
(
$exception
instanceof
HttpException
)
{
$response
->
setStatusCode
(
$exception
->
statusCode
);
...
...
framework/yii/gii/CodeFile.php
View file @
c7c7683f
...
...
@@ -11,6 +11,7 @@ use Yii;
use
yii\base\Object
;
use
yii\gii\components\TextDiff
;
use
yii\helpers\Html
;
use
yii\helpers\StringHelper
;
/**
* CodeFile represents a code file to be generated.
...
...
@@ -142,7 +143,8 @@ class CodeFile extends Object
if
(
in_array
(
$type
,
array
(
'jpg'
,
'gif'
,
'png'
,
'exe'
)))
{
return
false
;
}
elseif
(
$this
->
operation
===
self
::
OP_OVERWRITE
)
{
return
TextDiff
::
compare
(
file_get_contents
(
$this
->
path
),
$this
->
content
);
list
(
$diff
,
$addedLines
,
$deletedLines
)
=
StringHelper
::
diff
(
file
(
$this
->
path
),
$this
->
content
);
return
$diff
;
}
else
{
return
''
;
}
...
...
framework/yii/gii/components/TextDiff.php
deleted
100644 → 0
View file @
829c10f4
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\gii\components
;
use
Yii
;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
TextDiff
{
public
static
function
compare
(
$lines1
,
$lines2
)
{
if
(
is_string
(
$lines1
))
{
$lines1
=
explode
(
"
\n
"
,
$lines1
);
}
if
(
is_string
(
$lines2
))
{
$lines2
=
explode
(
"
\n
"
,
$lines2
);
}
$diff
=
new
\Horde_Text_Diff
(
'auto'
,
array
(
$lines1
,
$lines2
));
$renderer
=
new
\Horde_Text_Diff_Renderer_Inline
();
return
$renderer
->
render
(
$diff
);
}
}
framework/yii/helpers/StringHelperBase.php
View file @
c7c7683f
...
...
@@ -7,6 +7,8 @@
namespace
yii\helpers
;
use
yii\base\InvalidParamException
;
/**
* StringHelperBase provides concrete implementation for [[StringHelper]].
*
...
...
@@ -66,4 +68,49 @@ class StringHelperBase
}
return
$path
;
}
/**
* Compares two strings or string arrays, and return their differences.
* This is a wrapper of the Horde_Text_Diff package.
* @param string|array $lines1 the first string or string array to be compared. If it is a string,
* it will be converted into a string array by breaking at newlines.
* @param string|array $lines2 the second string or string array to be compared. If it is a string,
* it will be converted into a string array by breaking at newlines.
* @param string $format the output format. It must be 'context', 'inline', or 'unified'.
* @param string $engine the diff engine to be used. It must be 'auto', 'native', 'shell', 'string', or 'xdiff'.
* @return array the comparison result. The first element is a string representing the detailed comparison result.
* The second and the third elements represent the number of added lines and deleted lines, respectively.
* @throws InvalidParamException if the format or the engine is invalid.
*/
public
static
function
diff
(
$lines1
,
$lines2
,
$format
=
'inline'
,
$engine
=
'auto'
)
{
if
(
!
is_array
(
$lines1
))
{
$lines1
=
explode
(
"
\n
"
,
$lines1
);
}
if
(
!
is_array
(
$lines2
))
{
$lines2
=
explode
(
"
\n
"
,
$lines2
);
}
switch
(
$format
)
{
case
'context'
:
$renderer
=
new
\Horde_Text_Diff_Renderer_Context
();
break
;
case
'inline'
:
$renderer
=
new
\Horde_Text_Diff_Renderer_Inline
();
break
;
case
'unified'
:
$renderer
=
new
\Horde_Text_Diff_Renderer_Unified
();
break
;
default
:
throw
new
InvalidParamException
(
"Output format must be 'context', 'inline' or 'unified'."
);
}
if
(
!
in_array
(
$engine
,
array
(
'auto'
,
'native'
,
'shell'
,
'string'
,
'xdiff'
)))
{
throw
new
InvalidParamException
(
"Engine must be 'auto', 'native', 'shell', 'string' or 'xdiff'."
);
}
$diff
=
new
\Horde_Text_Diff
(
$engine
,
array
(
$lines1
,
$lines2
));
return
array
(
$renderer
->
render
(
$diff
),
$diff
->
countAddedLines
(),
$diff
->
countDeletedLines
(),
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment