mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 05:45:33 +08:00
Added StringHelper::diff().
This commit is contained in:
@@ -114,17 +114,15 @@ class ErrorHandler extends Component
|
|||||||
'exception' => $exception,
|
'exception' => $exception,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
} elseif ($exception instanceof Arrayable) {
|
||||||
|
$response->data = $exception;
|
||||||
} else {
|
} else {
|
||||||
if ($exception instanceof Arrayable) {
|
$response->data = array(
|
||||||
$response->data = $exception;
|
'type' => get_class($exception),
|
||||||
} else {
|
'name' => 'Exception',
|
||||||
$response->data = array(
|
'message' => $exception->getMessage(),
|
||||||
'type' => get_class($exception),
|
'code' => $exception->getCode(),
|
||||||
'name' => 'Exception',
|
);
|
||||||
'message' => $exception->getMessage(),
|
|
||||||
'code' => $exception->getCode(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($exception instanceof HttpException) {
|
if ($exception instanceof HttpException) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use Yii;
|
|||||||
use yii\base\Object;
|
use yii\base\Object;
|
||||||
use yii\gii\components\TextDiff;
|
use yii\gii\components\TextDiff;
|
||||||
use yii\helpers\Html;
|
use yii\helpers\Html;
|
||||||
|
use yii\helpers\StringHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CodeFile represents a code file to be generated.
|
* 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'))) {
|
if (in_array($type, array('jpg', 'gif', 'png', 'exe'))) {
|
||||||
return false;
|
return false;
|
||||||
} elseif ($this->operation === self::OP_OVERWRITE) {
|
} 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 {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
namespace yii\helpers;
|
namespace yii\helpers;
|
||||||
|
|
||||||
|
use yii\base\InvalidParamException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* StringHelperBase provides concrete implementation for [[StringHelper]].
|
* StringHelperBase provides concrete implementation for [[StringHelper]].
|
||||||
*
|
*
|
||||||
@@ -66,4 +68,49 @@ class StringHelperBase
|
|||||||
}
|
}
|
||||||
return $path;
|
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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user