sidworks / composer-patcher
Composer plugin to automatically apply patches to vendor packages without forking
Installs: 14
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Type:composer-plugin
pkg:composer/sidworks/composer-patcher
Requires
- php: ^8.0
- composer-plugin-api: ^2.0
README
A Composer plugin that automatically applies git-format patches to your project. Useful for patching vendor packages without forking.
Features
- Automatic patching - Patches are applied after every
composer installandcomposer update - Development patches - Use
.patch.devfiles for patches that only apply in dev mode - Organized structure - Supports subdirectories to organize patches by package
- Idempotent - Safely re-applies patches on every run (reverses first, then applies)
- Whitespace tolerant - Ignores whitespace differences when applying patches
- Interactive patch creation - Generate patches from modified vendor files with a single command
- Clear reporting - Grouped output showing success/failure status for all patches
Installation
composer require sidworks/composer-patcher
Quick Start
- Create a
patchesdirectory in your project root - Add
.patchfiles (git diff format) - Run
composer install
Directory Structure
your-project/
├── composer.json
├── patches/
│ ├── fix-typo.patch # Applied always
│ ├── debug-helper.patch.dev # Applied only in dev mode
│ └── acme/ # Organize by package
│ └── utils/
│ └── fix-calculation.patch
└── vendor/
Creating Patches
Using the Built-in Command
The easiest way to create a patch from a modified vendor file:
composer sidworks:composer-patcher --create
This interactive command will:
- Ask for the file path (e.g.,
vendor/acme/utils/src/Calculator.php) - Extract the original file from the package
- Generate a diff between original and modified versions
- Ask if you want to save in the package folder (e.g.,
patches/acme/utils/) - Save the patch with your chosen filename
Example session:
Enter the file path (relative to project root):
> vendor/acme/utils/src/Calculator.php
Save in patches/acme/utils/? [Y/n] y
Enter patch filename [Calculator.php.patch]:
>
✓ Patch created successfully!
Location: patches/acme/utils/Calculator.php.patch
Manually Creating Patches
Generate a patch using git diff:
# For tracked files git diff vendor/package/file.php > patches/my-fix.patch # Or from scratch using diff diff -u original.php modified.php > patches/my-fix.patch
Patches must use git-style headers:
--- a/vendor/package/src/File.php +++ b/vendor/package/src/File.php @@ -10,7 +10,7 @@ public function example() { - return 'old'; + return 'new'; }
Patch Types
| Extension | Applied When |
|---|---|
.patch |
Always (install and update) |
.patch.dev |
Only in dev mode (composer install without --no-dev) |
Commands
Apply Patches Manually
composer sidworks:composer-patcher
Runs the patcher manually (useful for testing). Always runs in dev mode.
Create a Patch
composer sidworks:composer-patcher --create
# or
composer sidworks:composer-patcher -c
How It Works
- On
composer installorcomposer update, the plugin activates - All existing patches are reversed (to handle updates cleanly)
- Patches are re-applied in alphabetical order
- Results are displayed grouped by folder
- If any patch fails, Composer exits with error code 1
Requirements
- PHP 8.0+
- Composer 2.x
- Git (for applying patches)
License
MIT