ezsystems / diff
Diff实现
4.0.0
2020-02-07 06:09 UTC
Requires
- php: ^7.3
Requires (Dev)
- phpunit/phpunit: ^9.0
- symfony/process: ^4 || ^5
README
PHP的Diff实现,从PHPUnit中独立出来作为一个组件。
安装
您可以使用Composer将此库作为本地、项目特定的依赖项添加到您的项目中
composer require sebastian/diff
如果您仅在开发期间需要此库,例如运行项目测试套件,那么您应该将其添加为开发时依赖项
composer require --dev sebastian/diff
用法
生成diff
可以使用Differ
类生成两个字符串之间差异的文本表示形式
<?php use SebastianBergmann\Diff\Differ; $differ = new Differ; print $differ->diff('foo', 'bar');
上面的代码将产生以下输出
--- Original +++ New @@ @@ -foo +bar
此包中提供了三种输出构建器
UnifiedDiffOutputBuilder
这是默认构建器,它生成的输出类似于udiff,并被PHPUnit使用。
<?php use SebastianBergmann\Diff\Differ; use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; $builder = new UnifiedDiffOutputBuilder( "--- Original\n+++ New\n", // custom header false // do not add line numbers to the diff ); $differ = new Differ($builder); print $differ->diff('foo', 'bar');
StrictUnifiedDiffOutputBuilder
生成(严格)统一diff(unidiffs),与diff -u
类似,与patch
和git apply
兼容。
<?php use SebastianBergmann\Diff\Differ; use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder; $builder = new StrictUnifiedDiffOutputBuilder([ 'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1` 'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed) 'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3 'fromFile' => null, 'fromFileDate' => null, 'toFile' => null, 'toFileDate' => null, ]); $differ = new Differ($builder); print $differ->diff('foo', 'bar');
DiffOnlyOutputBuilder
仅输出有差异的行。
<?php use SebastianBergmann\Diff\Differ; use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder; $builder = new DiffOnlyOutputBuilder( "--- Original\n+++ New\n" ); $differ = new Differ($builder); print $differ->diff('foo', 'bar');
DiffOutputBuilderInterface
您可以将任何实现了DiffOutputBuilderInterface
的输出构建器传递给Differ
类。
解析diff
可以使用Parser
类将统一diff解析为对象图
use SebastianBergmann\Diff\Parser; use SebastianBergmann\Git; $git = new Git('/usr/local/src/money'); $diff = $git->getDiff( '948a1a07768d8edd10dcefa8315c1cbeffb31833', 'c07a373d2399f3e686234c4f7f088d635eb9641b' ); $parser = new Parser; print_r($parser->parse($diff));
上面的代码将产生以下输出
Array
(
[0] => SebastianBergmann\Diff\Diff Object
(
[from:SebastianBergmann\Diff\Diff:private] => a/tests/MoneyTest.php
[to:SebastianBergmann\Diff\Diff:private] => b/tests/MoneyTest.php
[chunks:SebastianBergmann\Diff\Diff:private] => Array
(
[0] => SebastianBergmann\Diff\Chunk Object
(
[start:SebastianBergmann\Diff\Chunk:private] => 87
[startRange:SebastianBergmann\Diff\Chunk:private] => 7
[end:SebastianBergmann\Diff\Chunk:private] => 87
[endRange:SebastianBergmann\Diff\Chunk:private] => 7
[lines:SebastianBergmann\Diff\Chunk:private] => Array
(
[0] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 3
[content:SebastianBergmann\Diff\Line:private] => * @covers SebastianBergmann\Money\Money::add
)
[1] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 3
[content:SebastianBergmann\Diff\Line:private] => * @covers SebastianBergmann\Money\Money::newMoney
)
[2] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 3
[content:SebastianBergmann\Diff\Line:private] => */
)
[3] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 2
[content:SebastianBergmann\Diff\Line:private] => public function testAnotherMoneyWithSameCurrencyObjectCanBeAdded()
)
[4] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 1
[content:SebastianBergmann\Diff\Line:private] => public function testAnotherMoneyObjectWithSameCurrencyCanBeAdded()
)
[5] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 3
[content:SebastianBergmann\Diff\Line:private] => {
)
[6] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 3
[content:SebastianBergmann\Diff\Line:private] => $a = new Money(1, new Currency('EUR'));
)
[7] => SebastianBergmann\Diff\Line Object
(
[type:SebastianBergmann\Diff\Line:private] => 3
[content:SebastianBergmann\Diff\Line:private] => $b = new Money(2, new Currency('EUR'));
)
)
)
)
)
)