4

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
  • & I'm aware I should create a CDN release & a minified copy of the javascript for folks who want to use the js library, but that's not the point I'm at right now and not what I'm looking for. Commented Aug 30, 2020 at 17:22

1 Answer 1

4
+150

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you much! Though if somebody is using my library, they'll have to include the repositories{} list, won't they? I remember reading awhile ago that those have to be in the root-level composer.json for them to work. Still gives me a "copy+paste this into your composer" solution instead of something more complex.
Yep, I didn't realize from the question that you wanted something different. I think anyone maintaining a modern PHP app today should be comfortable with composer. Adding a second system for managing dependencies on top of that would seem (to me anyway) like a bit of a headache. What did you have in mind?
That's a good point, keeping it in the composer-family would be approachable for most. After mulling over for a bit, I'm wondering about using a custom bash build script to git pull the dependent repo then delete it's .git folder, so that the JS dependency saves to version control without any extra steps for folks using my lib. But then if I develop 3 different PHP libraries that all use that same JS lib, I'll have multiple copies & that would be problematic.
I'll probably go with what you've suggested. Wouldn't be hard to say "copy+paste this into your composer file". I could also write a command using composer config so I could tell end-users to composer require blah blah; composer config stuff-and-things; & keep it all in one-line / one command.
I think your Jenkins idea sounds good, or even just a fork of your own that you keep up-to-date manually. I guess having Jenkins automated will make it a bit less hassle for you in the long term. TBH I think including a composer.json in the main JS lib is totally fine - like look at the bootstrap library on Github - not at all related to PHP but still has a composer.json. Still, I can understand if you don't want to add yet another file to the root directory :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.