ondrejmirtes/php-merge
Composer 安装命令:
composer require ondrejmirtes/php-merge
包简介
A PHP merge utility using the Diff php library or the command line git.
关键字:
README 文档
README
Introduction
When working with revisions of text one sometimes faces the problem that there are several revisions based off the same original text. Rather than choosing one and discarding the other we want to merge the two revisions.
Git does that already wonderfully. In a php application we want a simple tool
that does the same. There is the xdiff PECL extension
which has the xdiff_string_merge3
function. But xdiff_string_merge3 does not behave the same way as git and
xdiff may not be available on your system.
PhpMerge is a small library that solves this problem. There are two classes:
\PhpMerge\PhpMerge and \PhpMerge\GitMerge that implement the
\PhpMerge\PhpMergeInterface which has just a merge method.
PhpMerge uses SebastianBergmann\Diff\Differ to get the differences between
the different versions and calculates the merged text from it.
GitMerge uses Symplify\GitWrapper\GitWrapper, writes the text to a temporary file
and uses the command line git to merge the text.
Usage
Simple example:
use PhpMerge\PhpMerge; // Create a merger instance. $merger = new PhpMerge(); // Get the texts to merge. $original = <<<'EOD' unchanged replaced unchanged normal unchanged unchanged removed EOD; $version1= <<<'EOD' added unchanged replacement unchanged normal unchanged unchanged EOD; $version2 = <<<'EOD' unchanged replaced unchanged normal?? unchanged unchanged EOD; $expected = <<<'EOD' added unchanged replacement unchanged normal?? unchanged unchanged EOD; $result = $merger->merge($original, $version1, $version2); // $result === $expected;
With merge conflicts:
// Continuing from before with: use Phpmerge\MergeException; use PhpMerge\MergeConflict; $conflicting = <<<'EOD' unchanged replaced unchanged normal!! unchanged unchanged EOD; try { $merger->merge($original, $version2, $conflicting); } catch (MergeException $exception) { /** @var MergeConflict[] $conflicts */ $conflicts = $exception->getConflicts(); $original_lines = $conflicts[0]->getBase(); // $original_lines === ["normal\n"]; $version2_lines = $conflicts[0]->getRemote(); // $version2_lines === ["normal??\n"]; $conflicting_lines = $conflicts[0]->getLocal(); // $conflicting_lines === ["normal!!\n"]; $line_numer_of_conflict = $conflicts[0]->getBaseLine(); // $line_numer_of_conflict === 3; // Count starts with 0. // It is also possible to get the merged version using the first version // to resolve conflicts. $merged = $exception->getMerged(); // $merged === $version2; // In this case, but in general there could be non-conflicting changes. $line_in_merged = $conflicts[0]->getMergedLine(); // $line_in_merged === 3; // Count starts with 0. }
Using the command line git to perform the merge:
use PhpMerge\GitMerge; $merger = new GitMerge(); // Use as the previous example.
Installation
PhpMerge can be installed with Composer by adding the library as a dependency to your composer.json file.
{
"require": {
"ondrejmirtes/php-merge": "~4.0"
}
}
To use the command line git with GitMerge:
{
"require": {
"ondrejmirtes/php-merge": "~4.0",
"symplify/git-wrapper": "^9.1|^10.0"
}
}
Please refer to Composer's documentation for installation and usage instructions.
Difference to ~3.0
In the ~4.0 version we switch from cpliakas/git-wrapper to symplify/git-wrapper since the former is deprecated.
This update means that there is no change when only using PhpMerge\PhpMerge.
ondrejmirtes/php-merge 适用场景与选型建议
ondrejmirtes/php-merge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.15M 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「git」 「merge」 「php-merge」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ondrejmirtes/php-merge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ondrejmirtes/php-merge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ondrejmirtes/php-merge 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP PDF Merger
A PHP merge utility using the Diff php library or the command line git.
Easily manage git hooks in your composer config
Forked from https://github.com/deltaaskii/lara-pdf-merger. Add condition if(!class_exists('TCPDF')) (depreciated in php 7.2) to foreach.
MediaWiki extension that allows embedding external content, specified by URL, into your wiki pages
Nagios plugin to verify a git repository.
统计信息
- 总下载量: 3.15M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-05-28