Faking a Composer Repo

This is just a quick tip. How can you have a project behave like a Composer repository, but where the packages are in subdirectories of a git repository.

First, if the package is the only package in the git repository and the composer.json file for the package is in the root directory, composer can reference that git repository directly. See https://getcomposer.org/doc/05-repositories.md#vcs. I am not going to talk about that case here.

But what if the packages are in a subdirectory? The following is a quick workaround to experiment with using the Composer “path” repository type. This is not a perfect solution, but it can be a useful temporary fix.

First, check out the git repository holding the packages alongside your main project. E.g. If you main project is in /var/www/htdocs/foo, then check out the other git repo in /var/www/htdocs/bar. What we are going to do in the foo/composer.json file is reference ../bar/ as a relative path. (You can use any path you like in practice.)

Let’s say all the packages are in a “packages” subdirectory (might be app/code/Magento for Magento modules). The trick is to add a new “repository” entry of type “path” with a URL of “../bar/packages/*” (or “../bar/app/code/Magento/*”). Composer will look for a composer.json file in the top directory of each package directory matching the path. If you reference exactly the same name and version number as in the composer.json file, it will be a match and Composer will use that package. There is a “symlink” property that if true will cause a symlink to be created, if false a copy of the package will be made.

See https://getcomposer.org/doc/05-repositories.md#path for complete details.

Example

    "repositories" : [
        {
            "type": "path",
            "url": "../bar/packages/*",
            "symlink": true
        },
        . . .
    ]

4 comments

  1. Hi Alan,

    Thank you for sharing this. I’ve had experience to work with this for Magento 2 modules and it happened that if you use “path” as your repository for custom Magento 2 module and include your module to Magento 2 project via “path” and symlink the static files from custom module won’t be visible for Magento 2. The only workable solution I found is to you Modman. So far I have 15 independent repositories for single Magento 2 project. Works like a charm.

    Thanks

    1. Curious. Could you explain a bit more why? I know of two potential problems – one some web server config bans symlinks, and the other if the ../magento2ee path is out of the approved area it might be rejected – but you are talking about symlinking static files. Just want to make sure I understand what you had go wrong better to see if its fixable.

  2. Rustam Aliev · · Reply

    Hi Alan,

    Unfortunately, some of Magento 3rd party vendors (modules) doesn’t include or support composer installation method yet, like:
    composer require vendor_name/extension_name

    In most cases these vendors suggests to manually upload the module files in Magento2. Would you please advise which one of these methods would be the best approach in this case?

    1. Through composer, using Path Repository method, for example in composer.json file:
    “repositories”: [
    {
    “type”: “path”,
    “url”: “../bar/packages/*”,
    “symlink”: true
    }
    ].

    2. Through composer, using Artifact Repository method, for example in composer.json file:
    “repositories”: [
    {
    “type”: “artifact”,
    “url”: “path the to the zip archive folder”
    }
    ].

    3. Manual method (unzip/upload module files in Magento2 app/code folder).

    Thank you in advance,
    Rustam.

    1. I have not tried 1 or 2 personally so this is just suggestions. The third is “official” advice, but you don’t get any version compatibility checking. So I would be tempted to try 1 (so you can put all the code under source code control in case you need to patch it etc) and see if you can manually add dependencies rules yourself as a form of upgrade protection for later. But I would also tell extension developer “adding a composer file with just dependency info is trivial – here is a sample”. One just to say it’s only compatible with certain versions of m2 is a no brained for me

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: