diff --git a/framework/yii/base/ErrorHandler.php b/framework/yii/base/ErrorHandler.php index 99428fc..41fa7f9 100644 --- a/framework/yii/base/ErrorHandler.php +++ b/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) { diff --git a/framework/yii/gii/CodeFile.php b/framework/yii/gii/CodeFile.php index 628adf9..bd77df6 100644 --- a/framework/yii/gii/CodeFile.php +++ b/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 ''; } diff --git a/framework/yii/gii/components/TextDiff.php b/framework/yii/gii/components/TextDiff.php deleted file mode 100644 index d8f09ec..0000000 --- a/framework/yii/gii/components/TextDiff.php +++ /dev/null @@ -1,30 +0,0 @@ - - * @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); - } -} diff --git a/framework/yii/helpers/StringHelperBase.php b/framework/yii/helpers/StringHelperBase.php index e7d1d1e..c72fabc 100644 --- a/framework/yii/helpers/StringHelperBase.php +++ b/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(), + ); + } }