blog and things oh hi there!
Overriding a Composer Package

Let say you're working with a package that you pulled through Composer/Packagist and you find a bug. Of course you can create a pull request fixing this or create a fork and pull it in to your project through Composer. However, sometimes it may be more convenient to just include the fixed package into your main application repository and move along.

If you just want to override a Packagist package with your own instance, start by copying the package from vendor/ into an appropriate folder within your application. Go ahead and apply the code fix to make the bugs go away.

After that, remove the package from the require block in composer.json.

To actually override the the package, you need to know the namespace that the package uses. In most cases, this is a simple combination of vendor + package name but it's not always the case so double check the namespace.

Within composer.json's autoload > psr-4 block, place the above namespace on the left, and on the right, add the path to the fixed package within your application. This path needs to be the root of the package where the actual class with the namespace exists.

"autoload": {
    "psr-4": {
        "Sunra\\PhpSimple\\": "app/Vendor/sunra/php-simple-html-dom-parser/Src/Sunra/PhpSimple"
    },
},

At this point, just run composer update to re-generate the autoloader and that's about it!

© 2019 Kevin Jung