composer引用私有git

php开发中,会遇到需要引用公司内部开发的组件。内部组件放在私有git上,用以下方式引用:
在项目composer.json中添加包配置。

  • git库在本地
{
    "repositories": {
        "0": {
            "type": "package",
            "package": {
                "name": "hwnet/tecomp",
                "version": "1.0.1",
                "source": {
                    "url": "file:///media/sf_git/tecomp",
                    "type": "git",
                    "reference": "bda1d03"
                }
            }
        }
    },
    "require": {
        "hwnet/tecomp": "1.0.1"
    }
}
  • git库在局域网,如果url是http,不是https,还需要在config里定义 "secure-http": false
{
    "repositories": {
        "0": {
            "type": "package",
            "package": {
                "name": "hwnet/tecomp",
                "version": "1.0.1",
                "source": {
                    "url": "http://192.168.0.1/git/tecomp",
                    "type": "git",
                    "reference": "bda1d03"
                }
            }
        }
    },
    "require": {
        "hwnet/tecomp": "1.0.1"
    },
    "config": {
        "secure-http": false
    }
}

如果该git是public的,则不需要任何处理,如果git是需要登录的,在项目的根目录下(和composer.json平级的目录)增加一个auth.json

{    
    "http-basic":{    
        "http://192.168.0.1/git/tecomp":{    
            "username":"",    
            "password":""   
        }    
    }    
}   

git出来的库在vendor目录下。如果在执行composer后,需要删除拉下来的.git目录,可以在composer.json中加入钩子。

    "scripts": {
        "post-install-cmd": [
            "find vendor -type d -name .git -exec rm -rf '{}' \\;"
        ],
        "post-update-cmd": [
            "find vendor -type d -name .git -exec rm -rf '{}' \\;"
        ]
    }
  • 另外,还可以用文件的形式把组件分发出来。composer.json可以像下面这样写。下载的文件会放在vendor目录下,zip文件会自动解开。
{
    "repositories": {
        "0": {
            "type": "package",
            "package": {
                "name": "hwnet/asset01",
                "version": "1.0.0",
                "dist": {
                    "url": "http://192.168.10.53/smb.txt",
                    "type": "file"
                }
            }
        },
        "1": {
            "type": "package",
            "package": {
                "name": "hwnet/asset02",
                "version": "1.0.0",
                "dist": {
                    "url": "http://192.168.10.53/W.zip",
                    "type": "zip"
                }
            }
        }
    },
    "require": {
        "hwnet/asset01": "1.0.0",
        "hwnet/asset02": "1.0.0"
    },
    "config": {
        "secure-http": false
    }
}

你可能感兴趣的:(composer引用私有git)