简体   繁体   English

PHP 库中的 Javascript 依赖项

[英]Javascript dependency in PHP library

I have a PHP library that depends on a Javascript repo (also my lib).我有一个依赖于 Javascript 存储库(也是我的库)的 PHP 库。 In the PHP lib, I don't want a CDN url or a minified copy.在 PHP 库中,我不想要 CDN url 或缩小的副本。 The PHP lib uses a framework (also home-brewed) that will compile the JS files together along with all the resources on my site. PHP 库使用一个框架(也是自制的),可以将 JS 文件与我网站上的所有资源一起编译。

I don't want to change anything about the JS lib, aka I don't want to make a composer.json file.我不想更改 JS 库的任何内容,也就是我不想制作composer.json文件。 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?我知道git submodule存在,但我不确定如何使用它,而且我已经读到这是处理依赖项的一种非常糟糕的方式,我猜我的子模块不会通过 composer 包含在内?

Are there any other ways to include a JS dependency in a PHP library?还有其他方法可以在 PHP 库中包含 JS 依赖项吗? (aside from copy+pasting the files) (and/or tips to make submodule a good option) (除了复制+粘贴文件)(和/或使子模块成为不错选择的提示)

Composer defaults to using the metadata from Packagist, which Packagist pulls from each repo's composer.json file. Composer 默认使用 Packagist 的元数据,Packagist 从每个 repo 的composer.json文件中提取。

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. Composer 有一些关于它的文档here,但我自己尝试过,并将在下面包含我的示例 Composer 文件。 I was able to use composer update to download a git repo which didn't contain a composer.json file.我能够使用composer update下载一个不包含composer.json文件的 git repo。

Sample Composer file for the PHP project: PHP 项目的示例 Composer 文件:

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.我加载的测试存储库只包含一个文本文件,内容为“这是版本 1”,并且在 PHP 包的require部分使用不同的版本,我能够在它们之间切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM