Browse Source

Added StringHelper::diff().

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
c7c7683f8f
  1. 18
      framework/yii/base/ErrorHandler.php
  2. 4
      framework/yii/gii/CodeFile.php
  3. 30
      framework/yii/gii/components/TextDiff.php
  4. 47
      framework/yii/helpers/StringHelperBase.php

18
framework/yii/base/ErrorHandler.php

@ -114,17 +114,15 @@ class ErrorHandler extends Component
'exception' => $exception,
));
}
} elseif ($exception instanceof Arrayable) {
$response->data = $exception;
} else {
if ($exception instanceof Arrayable) {
$response->data = $exception;
} else {
$response->data = array(
'type' => get_class($exception),
'name' => 'Exception',
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
);
}
$response->data = array(
'type' => get_class($exception),
'name' => 'Exception',
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
);
}
if ($exception instanceof HttpException) {

4
framework/yii/gii/CodeFile.php

@ -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 '';
}

30
framework/yii/gii/components/TextDiff.php

@ -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);
}
}

47
framework/yii/helpers/StringHelperBase.php

@ -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(),
);
}
}

Loading…
Cancel
Save