Guide to Managing Dependencies with Composer

Muhammad Noman Rauf
4 min readFeb 28, 2023

--

Composer patches are a powerful tool for managing dependencies in PHP projects. They allow developers to apply changes to upstream packages without forking them or waiting for pull requests to be merged. This can be particularly useful when working with packages that are not actively maintained or when trying to add features or fix bugs in a specific version of a package.

In this article, we’ll explore the basics of composer patches, including how to create them, apply them, and manage them in a PHP project.

What are Composer patches?

Composer patches are a way to modify the code of a dependency without directly modifying the dependency itself. This is useful in situations where you need to make changes to a package but don’t want to maintain a separate fork or wait for a pull request to be merged.

A patch is simply a file that contains the changes you want to make to the package’s code. These changes can be anything from bug fixes to new features or even just small tweaks to existing code.

Composer patches are defined in the composer.json file of a PHP project. They can be applied to any dependency that is installed via Composer, regardless of whether it is hosted on Packagist or on a private repository.

How to create a Composer patch

To create a patch, you’ll need to first clone the repository of the package you want to modify. You can do this using Git, which should be installed on your machine.

Once you have the repository cloned, make the necessary changes to the code. Once you’re happy with your changes, you must generate a patch file.

You can do this using the git diff command. For example, if you made changes to the Foo.php file, you could generate a patch file like this:

git diff > foo.diff

This will create a patch file called foo.diff in the current directory. You can then move this file to the appropriate location in your project.

How to apply a Composer patch

To apply a patch, you’ll need to define it in your project’s composer.json file.

Here’s an example of how to do this:

{
"require": {
"nrauf90/greeting-composer": "@dev",
"cweagans/composer-patches": "^1.7"
},
"extra": {
"patches": {
"nrauf90/greeting-composer": {
"update package: update function": "patches/composer/PR-1.diff"
}
}
},
"config": {
"allow-plugins": {
"cweagans/composer-patches": true
}
}
}

In this example, we’re defining a patch for the nrauf90/greeting-composer package. The patch file is located at patches/composer/PR-1.diff.

diff --git a/vendor/nrauf90/greeting-composer/src/Index.php b/src/Index.php
index 14470cd..c33e3b8 100644
--- a/vendor/nrauf90/greeting-composer/src/Index.php
+++ b/vendor/nrauf90/greeting-composer/src/Index.php
@@ -4,7 +4,7 @@ class Index {
/**
* @return string
*/
- public function greeting(){
- return "Welcome!";
+ public function greeting($name){
+ return "Welcome $name!";
}
}

Add the cweagans/composer-patches plugin to the composer.json file: composer require cweagans/composer-patches

Once you’ve defined your patch in your composer.json file, you can apply it using the composer install or composer update command.

How to manage Composer patches

Composer patches can be managed in a few different ways. Here are a few best practices to keep in mind:

  • Store your patches in a version control system like Git. This will allow you to easily track changes and share patches with other developers.
  • Use descriptive patch names and descriptions. This will make it easier to understand what each patch does and why it was created.
  • Keep your patches up to date. If you’re using a patch to fix a bug or add a feature, be sure to update the patch when new versions of the package are released.
  • Test your patches thoroughly. Before applying a patch to a production environment, be sure to test it thoroughly in a development or staging environment.
  • Consider contributing your patches back upstream. If you’ve created a patch to fix a bug or add a feature, consider contributing it back upstream to the original package. This can help ensure that your changes are maintained over time and can benefit the broader community.

Conclusion

Composer patches are a powerful tool for managing and modifying the packages used in your PHP projects. They allow you to make changes to a package without modifying the original code or maintaining a fork of the package. With Composer patches, you can fix issues or add new features to the packages you use, making them more suited to your specific project requirements.

Git Repo Source: https://github.com/nrauf90/composer-patches-testing
Composer Package: https://packagist.org/packages/nrauf90/greeting

--

--