Vendor patches, the easy way!
Sometimes, you need to patch your project files until a bugfix is shipped to the upstream repository. cweagans/composer-patches is a Composer package that automates applying patches during the Composer install procedure.
Thanks to cweagans/composer-patches, applying the patches is easy. However, creating the patch files can take some time. In the past, I created the patch files via Git. I added the old and new states, extracted the patch, made sure the paths in the patch file pointed to the correct vendor directory and thoroughly tested whether the patch was properly applied.
If the patch cannot be applied, you are presented with a nice "fatal error" message. Figuring out what the real issue is can be quite annoying.
Recently, I came across the symplify/vendor-patches project, which aims to simplify the patching process.
First, let's install the Composer packages as developer dependencies:
composer require cweagans/composer-patches symplify/vendor-patches --dev
Next, find the file you want to change and copy it to a new file with an .old
suffix:
cp vendor/sylius/invoicing-plugin/src/EventListener/Workflow/Payment/ProduceOrderPaymentPaidListener.php vendor/sylius/invoicing-plugin/src/EventListener/Workflow/Payment/ProduceOrderPaymentPaidListener.php.old
Edit the original file and apply the changes needed. Since you modify the original file, you can instantly test your changes and make modifications until you are happy.
By running the vendor/bin/vendor-patches generate
command, a few things happen automatically:
- a patch file gets created in the
./patches
directory in the project root - the configuration for the
cweagans/composer-patches
plugin is added to your composer.json file
To test everything, run composer install
and your patch is applied to your code.