Javascript dependency in PHP library

Viewed 431

I have a PHP library that depends on a Javascript repo (also my lib). In the PHP lib, I don't want a CDN url or a minified copy. The PHP lib uses a framework (also home-brewed) that will compile the JS files together along with all the resources on my site.

I don't want to change anything about the JS lib, aka I don't want to make a composer.json file. I'm aware git submodule exists, though I'm not sure how to use it and I've read that it's a thoroughly bad way to handle dependencies, and I'm guessing my submodules wouldn't get included through composer?

Are there any other ways to include a JS dependency in a PHP library? (aside from copy+pasting the files) (and/or tips to make submodule a good option)

1 Answers

Composer defaults to using the metadata from Packagist, which Packagist pulls from each repo's composer.json file.

However, it is possible to just specify any file that you want to download yourself. It might be a bit cumbersome if you want to have a lot of versions though.

Composer has some documentation about it here but I tried it out myself and will include my sample composer file below. I was able to use composer update to download a git repo which didn't contain a composer.json file.

Sample Composer file for the PHP project:

It looks like you'll need a "package" section for each version you want.

{
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "testy/testyson",
                "version": "1.0.0",
                "dist": {
                    "url": "https://github.com/mickadoo/testlib/archive/1.0.0.zip",
                    "type": "zip"
                }
            }
        },
        {
            "type": "package",
            "package": {
                "name": "testy/testyson",
                "version": "2.0.0",
                "dist": {
                    "url": "https://github.com/mickadoo/testlib/archive/2.0.0.zip",
                    "type": "zip"
                }
            }
        }
    ],
    "require": {
        "testy/testyson": "2.*"
    }
}

The test repository I loaded just contains a text file with the contents "This is version 1" and using the different version in the require section of the PHP package I was able to switch between them.

Related