Composer artifacts – an easy way to include composer packages with no repos in your code

As a Magento developer, I always prefer to add 3rd party extensions using composer. Sadly, a fair amount of vendors still provide archives with their modules instead of composer-ready repositories. Creating separate private repositories to keep each extension and then including them in my project seems like an overkill and it looks that there is a better solution for this use case – composer artifacts.

The idea is pretty simple – create an archive with your composer-ready code, add it in a directory of your main project, then simply ask composer to use it.

As an example, let’s assume we have a fictional module that has the following composer.json file:

{
"name": "paul/artifact",
"description": "this is an artifact",
"minimum-stability": "stable",
"version": "1.0",
"license": "proprietary",
"authors": [
  {
    "name": "test",
    "email": "email@example.com"
  }
]
}

The only part to keep in mind is the name of the package, paul/artifact in this case. To use it, create a zip archive of the code (including the composer.json file) then add it to your project in a subdirectory, i.e. artifacts/. The name of the zip archive is irrelevant.

In your main project, you can make composer aware of the artifacts directory by adding the following new repository type:

"repositories": [
    .....
    {
      "type": "artifact",
      "url": "artifacts"
    }
  ],

 
The artifact type is what will make composer search for artifacts as zip archives as opposed to pulling from source control repos. The “url” is the name of the directory where the zip archives are, relative to your project root.

Once this is done, you simply require the package as you always do:

"require": {
    "paul/artifact": "1.*"
  }

This brings up all composer’s features – it will install dependencies, lock versions etc. There is virtually no difference between an artifact package and any other type of package.

A small gotcha – if you want to update the package (say you zipped up a new version), don’t forget to clear composer’s cache (by running composer cleancache). Composer caches the artifacts exactly as it caches remote repos, so if you are not changing version numbers you have to intentionally clean caches so that your new version is picked up.

Hope this saves you some time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s