Download the PHP package mkgrow/docx-merge without Composer
On this page you can find all versions of the php package mkgrow/docx-merge. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mkgrow/docx-merge
More information about mkgrow/docx-merge
Files in mkgrow/docx-merge
Package docx-merge
Short Description Framework-agnostic DOCX document merging by markers.
License MIT
Informations about the package docx-merge
DocxMerge
A framework-agnostic PHP 8.2+ Composer library that merges DOCX documents by substituting ${MARKER} placeholders in a template with content extracted from source .docx files. Supports multi-section extraction, style deduplication, numbering resequencing, media copying, and header/footer merging -- all while maintaining valid OOXML document structure.
Requirements
| Requirement | Version |
|---|---|
| PHP | >= 8.2 |
| ext-zip | * |
| ext-dom | * |
| ext-xml | * |
| ext-mbstring | * |
| psr/log | ^3.0 |
Installation
Quick Start
The template document must contain ${MARKER} placeholders (e.g., ${INTRODUCTION}, ${CONCLUSION}) as paragraph text. Each placeholder is replaced with the full body content of the corresponding source DOCX file.
Usage
Simple single-marker merge
Multiple markers with different sources
Section targeting with MergeDefinition
When a source document has multiple sections, use MergeDefinition to extract a specific section by its zero-based index:
Configuring MergeOptions
Strict marker mode
Throws MarkerNotFoundException when a marker is not found in the template, instead of silently skipping it:
Custom marker pattern
Override the default ${MARKER} pattern with a custom regex. The first capture group must contain the marker name:
Reprocessing mode
Merge additional markers into a previously generated output file:
Handling the MergeResult
MergeResult provides structured feedback about the merge operation:
Error handling with typed exceptions
All exceptions extend DocxMergeException, allowing both broad and fine-grained error handling:
Using a PSR-3 logger
Pass any PSR-3 compatible logger to receive diagnostic output:
API Reference
| Class | Description | Documentation |
|---|---|---|
DocxMerger |
Public facade with merge() method |
docs/api/docx-merger.md |
MergeDefinition |
DTO for marker-to-source binding with section targeting | docs/api/merge-definition.md |
MergeOptions |
DTO for merge configuration (pattern, strict mode, reprocessing) | docs/api/merge-options.md |
MergeResult |
DTO for merge results (success, stats, errors, warnings) | docs/api/merge-result.md |
| Exceptions | 6 typed exceptions extending DocxMergeException |
docs/api/exceptions.md |
Architecture Overview
DocxMerge follows a 4-layer architecture with strict downward dependency flow:
| Layer | Components | Responsibility |
|---|---|---|
| 1. Public API | DocxMerger, DTOs |
Entry point, input validation, normalization |
| 2. Orchestration | MergeOrchestrator, MergeContext |
13-phase pipeline coordination, per-operation state |
| 3. Domain Services | 12 services (e.g., MarkerLocator, StyleMerger, IdRemapper) |
Individual merge concerns (styles, numbering, media, relationships) |
| 4. Infrastructure | XmlHelper, ZipHelper, IdTracker, SourceDocumentCache |
XML/ZIP utilities, ID tracking, document caching |
All domain services depend on interfaces and are stateless (per ADR-001). Per-operation mutable state is encapsulated in MergeContext.
For complete architecture documentation, see docs/system-design/.
Development
Prerequisites
- PHP 8.2+ with extensions: zip, dom, xml, mbstring
- Composer 2.x
Setup
Commands
| Command | Description |
|---|---|
composer test |
Run the full Pest v3 test suite |
composer test --filter=Name |
Run a specific describe block or test |
composer test:coverage |
Run tests with coverage gate (minimum 90%) |
composer analyse |
Run PHPStan level 8 on src/ and tests/ |
composer format |
Apply PSR-12 formatting via php-cs-fixer |
composer format:check |
Check PSR-12 compliance without modifying files |
composer ci |
Full quality gate: analyse + format:check + test:coverage |
Testing
The test suite uses Pest v3 with 182 tests and 357 assertions, achieving 95.2% line coverage.
- Unit tests (
tests/Unit/): Pure in-memory XML tests with no filesystem access. - Integration tests (
tests/Integration/): Real.docxfixture-based tests usingtests/Integration/Fixtures/.
Quality Gate
Before submitting changes, ensure the full CI pipeline passes:
This runs PHPStan level 8, PSR-12 format check, and test coverage gate (minimum 90%) in sequence.
Changelog
See CHANGELOG.md for a detailed version history.
License
This project is licensed under the MIT License. See the LICENSE file for details.
All versions of docx-merge with dependencies
ext-dom Version *
ext-mbstring Version *
ext-xml Version *
ext-zip Version *
psr/log Version ^3.0