{"id":170606,"date":"2021-02-04T16:00:20","date_gmt":"2021-02-04T13:00:20","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/"},"modified":"2021-02-04T16:00:20","modified_gmt":"2021-02-04T13:00:20","slug":"how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/","title":{"rendered":"#How to Apply Your Own Patches to Composer Packages \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a37e525336a3\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a37e525336a3\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/#When_to_Patch_a_Package\" >When to Patch a Package<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/#Creating_a_Composer_Patch\" >Creating a Composer Patch<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/#Creating_the_Patch_File\" >Creating the Patch File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/#Automatic_Patching\" >Automatic Patching<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-your-own-patches-to-composer-packages-cloudsavvy-it\/#Removing_Patches\" >Removing Patches<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">App<\/a>ly Your Own Patches to Composer Packages \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4047\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/b835749967babe94be45e3edd2508d9c\/p\/uploads\/2017\/08\/1ac500da.png\" alt=\"PHP Logo\" width=\"1400\" height=\"600\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Using third-party packages can accelerate your project\u2019s development. Sometimes, you might need to add extra functionality or fix a critical bug. Here\u2019s how to apply patches to PHP packages installed by Composer.<\/p>\n<h2 id=\"when-to-patch-a-package\"><span class=\"ez-toc-section\" id=\"When_to_Patch_a_Package\"><\/span>When to Patch a Package<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>First consider whether it\u2019s appropriate to create an in-project patch. A patch should always be a minor change. If you need to add extensive new functionality, you should open an issue against the package or fork it yourself. This will help avoid conflicts as the package evolves.<\/p>\n<p>You should think carefully about whether the change you require is truly specific to your project. Dependency patches typically affect only a few lines of code. They address bugs and issues that would otherwise prevent you from using the package. Most patches are short-term in nature. They should be replaced later by at-source fixes within the package\u2019s codebase.<\/p>\n<p>Ultimately, you need to measure your intentions. Are you addressing an im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>te issue with a package, or extending the package\u2019s functionality to align with your codebase? If it\u2019s the latter, you should consider either contributing to the package, or wrapping it with extra classes and functions that are first-class citizens in your project.<\/p>\n<h2 id=\"creating-a-composer-patch\"><span class=\"ez-toc-section\" id=\"Creating_a_Composer_Patch\"><\/span>Creating a Composer Patch<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once you\u2019ve made the call to patch, you can start to prepare your project. Composer doesn\u2019t have built-in patch support, so we\u2019ll use the popular <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/packagist.org\/packages\/cweagans\/composer-patches\"><code>simplify\/vendor-patches<\/code> project<\/a> to add it. This provides an intuitive interface over <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/packagist.org\/packages\/symplify\/vendor-patches\"><code>cweagans\/composer-patches<\/code><\/a> to help you create new patches.<\/p>\n<pre>composer require --dev symplify\/vendor-patches<\/pre>\n<p>Make sure the package you need to patch is installed in your project:<\/p>\n<pre>composer require example\/broken-package<\/pre>\n<p>Next, open the problematic file in your code editor. You\u2019ll find it within the <code>vendor<\/code> directory. In our example, we need to edit <code>vendor\/example\/broken-package\/src\/Broken.php<\/code>:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">&lt;?php<\/span>\n\u00a0\n<span class=\"kw2\">class<\/span> Broken <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>string<span class=\"sy0\">|<\/span>int <span class=\"re0\">$foo<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"kw3\">is_string<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$foo<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n            <span class=\"kw1\">echo<\/span> <span class=\"st0\">\"Valid value!\"<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"br0\">}<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"sy1\">?&gt;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>Broken<\/code> class is expected to accept both strings and integers, using PHP 8\u2019s union types. Unfortunately, looking at the source shows it actually only accepts strings.<\/p>\n<p>In our fictitious example, the package maintainer has acknowledged the issue but hasn\u2019t yet created a new release. Let\u2019s patch our way around the problem in the meantime.<\/p>\n<p>Copy the broken file, unaltered, and add a <code>.old<\/code> suffix:<\/p>\n<pre>cp vendor\/example\/broken-project\/src\/Broken.php vendor\/example\/broken-project\/src\/Broken.php.old<\/pre>\n<p>Make sure you don\u2019t edit the <code>.old<\/code> file!<\/p>\n<p>Next, edit the <em>original<\/em> file so it functions correctly in your codebase. As you\u2019re editing in-place, the changes take effect immediately. Verify your codebase now behaves as expected.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">&lt;?php<\/span>\n\u00a0\n<span class=\"kw2\">class<\/span> Broken <span class=\"br0\">{<\/span>\n    <span class=\"kw2\">public<\/span> <span class=\"kw2\">function<\/span> __construct<span class=\"br0\">(<\/span>string<span class=\"sy0\">|<\/span>int <span class=\"re0\">$foo<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"kw3\">is_string<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$foo<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n            <span class=\"kw1\">echo<\/span> <span class=\"st0\">\"Valid value!\"<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"br0\">}<\/span>\n        <span class=\"kw1\">else<\/span> <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"kw3\">is_int<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$foo<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n            <span class=\"kw1\">echo<\/span> <span class=\"st0\">\"Also valid!\"<\/span><span class=\"sy0\">;<\/span>\n        <span class=\"br0\">}<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"sy1\">?&gt;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"creating-the-patch-file\"><span class=\"ez-toc-section\" id=\"Creating_the_Patch_File\"><\/span>Creating the Patch File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can now use the <code>symplify\/vendor-patches<\/code> project to create a patch file for your fix. The package provides a <code>vendor-patches<\/code> binary which automatically scans your <code>vendor<\/code> directory to find the changes you\u2019ve made.<\/p>\n<pre>vendor\/bin\/vendor-patches generate<\/pre>\n<p>Running the command will generate a diff for you. It\u2019ll be saved to the <code>patches<\/code> directory at the root of your project. The diff is calculated by comparing the <code>.php<\/code> and <code>.php.old<\/code> files you\u2019ve created.<\/p>\n<p>You can run the command again to discover any new patches you add. Each changed file gets its own patch within the <code>patches<\/code> directory.<\/p>\n<h2 id=\"automatic-patching\"><span class=\"ez-toc-section\" id=\"Automatic_Patching\"><\/span>Automatic Patching<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Within your <code>composer.json<\/code> file, you\u2019ll see a new section has been added:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"extra\": {&#13;\n        \"patches\": {&#13;\n            \"example\/broken-package\": [&#13;\n                \"patches\/example-broken-package-src-broken-php.patch\"&#13;\n            ]&#13;\n        }&#13;\n    }&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>patches<\/code> object maps the names of installed packages to an array of patch files to apply. These patches will be applied automatically whenever you <code>composer install<\/code>.<\/p>\n<p>An installation script is registered by <code>simplify\/vendor-patches<\/code>. It gets invoked after each dependency is installed. The script looks to see if any patches have been defined for the package. It\u2019ll automatically apply any it finds.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9328\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/a2c0210e73ea4dbc3eda87da881553dd\/p\/uploads\/2021\/01\/55f81598.png\" alt=\"\" width=\"930\" height=\"134\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You\u2019ll see lines in the <code>composer install<\/code> output indicating when patches have been applied. If an error is reported, you can use <code>composer install --verbose<\/code> to get more information on why the patch was skipped. This is often due to a package update fixing the issue you patched, making your patch file redundant.<\/p>\n<h2 id=\"removing-patches\"><span class=\"ez-toc-section\" id=\"Removing_Patches\"><\/span>Removing Patches<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You can temporarily remove a patch by deleting its line from <code>composer.json<\/code>. This will stop the patch being applied when you <code>composer install<\/code>.<\/p>\n<p>To permanently delete a patch from your project, remove its <code>composer.json<\/code> line. You can then remove its <code>.patch<\/code> file from your project\u2019s <code>patches<\/code> directory.<\/p>\n<p>If you want to revert a patch you\u2019ve applied locally, it\u2019s simplest to follow the steps above to delete or disable the patch. You can then remove the package from the <code>vendor<\/code> directory and run <code>composer install<\/code> to get back to a clean slate.\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9323\/how-to-apply-your-own-patches-to-composer-packages\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Apply Your Own Patches to Composer Packages \u2013 CloudSavvy IT&#8221; Using third-party packages can accelerate your project\u2019s development. Sometimes, you might need to add extra functionality or fix a critical bug. Here\u2019s how to apply patches to PHP packages installed by Composer. When to Patch a Package First consider whether it\u2019s appropriate to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":170607,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2017\/08\/1ac500da.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-170606","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/170606","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=170606"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/170606\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/170607"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=170606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=170606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=170606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}