如何让 Composer 从我的本地存储库自动安装包的所有依赖项?
How do I make Composer automatically install all dependencies of a package from my local repository?
我在 Ubuntu 上使用 Composer 1.6.3 并具有以下全局 ~/.config/composer/config.json
文件:
{
"config": {},
"repositories": {
"packagist": false,
"codifier": {
"type": "composer",
"url": "file:///usr/local/codifier/library/PHP/Codifier"
}
}
}
换句话说,我禁用了 Packagist 并指向我在本地计算机上自己的存储库。这个本地存储库有一个 packages.json
定义所有可用的本地包:
{
"packages": {
"codifier/cache": {
"dev-master": {
"name": "codifier/cache",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Cache",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/cache",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Cache",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/environment": {
"dev-master": {
"name": "codifier/environment",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Environment",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/environment",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Environment",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/event": {
"dev-master": {
"name": "codifier/event",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Event",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/event",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Event",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/mime": {
"dev-master": {
"name": "codifier/mime",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Mime",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/mime",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Mime",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/net": {
"dev-master": {
"name": "codifier/net",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Net",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/net",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Net",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/number": {
"dev-master": {
"name": "codifier/number",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Number",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/number",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Number",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/persistence": {
"dev-master": {
"name": "codifier/persistence",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Persistence",
"type": "git",
"reference": "master"
}
},
"0.1.0": {
"name": "codifier/persistence",
"version": "0.1.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Persistence",
"type": "git",
"reference": "v0.1.0"
}
}
},
"codifier/process": {
"dev-master": {
"name": "codifier/process",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Process",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/process",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Process",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/socket": {
"dev-master": {
"name": "codifier/socket",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/socket",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/stdlib": {
"dev-master": {
"name": "codifier/stdlib",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/StdLib",
"type": "git",
"reference": "master"
}
},
"0.1.0": {
"name": "codifier/stdlib",
"version": "0.1.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/StdLib",
"type": "git",
"reference": "v0.1.0"
}
}
},
"codifier/text": {
"dev-master": {
"name": "codifier/text",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Text",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/text",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Text",
"type": "git",
"reference": "v1.0.0"
}
}
}
}
}
到目前为止,这部分工作正常。当我在随机文件夹中执行 $ composer show -a
时,我得到以下输出:
No composer.json found in the current directory, showing available packages from codifier
codifier/cache
codifier/environment
codifier/event
codifier/mime
codifier/net
codifier/number
codifier/persistence
codifier/process
codifier/socket
codifier/stdlib
codifier/text
所有这些包都定义了它们自己的依赖项。例如,codifier/socket
包在其根目录中有以下 composer.json
文件:
{
"name": "codifier/socket",
"authors": [ ##REDACTED## ],
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
},
"autoload": {
"psr-4": {
"Codifier\Socket\": "src"
}
}
}
现在,我认为 Composer 的全部意义在于它会自动递归安装所有必需的依赖项,而不是我必须再次手动 require
所有依赖项,但事实并非如此.
当我创建一个新的干净应用程序目录 $ mkdir Test && cd Test
然后执行 $ composer require codifier/socket
时,我得到以下输出:
Using version ^1.0 for codifier/socket
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing codifier/socket (1.0.0): Cloning v1.0.0 from cache
Writing lock file
Generating autoload files
...然后当我执行 $ composer install
时,我得到以下输出:
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
换句话说,只安装了 codifier/socket
而没有安装其依赖项。因此,Test/vendor
的内容现在是:
- codifier/
- socket/
...当我期望内容是:
- codifier/
- socket/
- event/
- net/
- process/
etc.
...因为我希望 Composer 递归地获取所有必需的依赖项。
我也尝试了 $ composer clear-cache
,但这没有什么区别,我想我也看到一些帖子建议我需要在 composer.json
中定义 "repositories"
这个应用程序,但这对我来说没有任何意义,因为我已经在我的全局 config.json
文件中指出了那些。
我在这里错过了什么?我是否错过了 Composer 的要点,因为我认为它会自动解析并自动安装所有必需的依赖项?
--- 更新 ---
我现在也尝试通过我的本地网络服务器提供包(当然,并相应地更改 json 文件中的所有 "url"
字段),因为我认为 Composer 可能不是'无法正确处理 file://
协议,但无济于事:它给出完全相同的结果:它只安装 codifier/socket
并忽略其依赖项。
我不知道我还需要做什么才能使这个本地 packages.json
存储库正常工作。
刚刚灵机一动,终于想通了怎么让它工作,但感觉很多余。我让它工作的方法是简单地将 "require"
信息从包的 composer.json
文件复制到我存储库的 packages.json
文件中。
再次以codifier/socket
包中的composer.json
文件为例:
{
"name": "codifier/socket",
"authors": [ ##REDACTED## ],
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
},
"autoload": {
"psr-4": {
"Codifier\Socket\": "src"
}
}
}
...它的相关信息在 packages.json
中将变为:
{
"packages": {
...
"codifier/socket": {
"dev-master": {
"name": "codifier/socket",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "master"
},
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
}
},
"1.0.0": {
"name": "codifier/socket",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "v1.0.0"
},
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
}
}
},
...
}
}
当我将此应用到所有包时,一切正常。
不过,我仍然不明白的是,为什么 Composer 没有从包本身的实际 composer.json
中获取此 "require"
信息。我知道文档说明 Composer doesn't recursively follow repositories,但它肯定必须递归地遵循依赖关系,不是吗?不过,也许它根本就没有,这就是它的工作方式。
这一切都意味着我将不得不做更多的 git 解析以获得包的每个单独版本的正确数据,但就这样吧。1)
这个解决方案也可能会引导我找到我遇到的自动加载问题的解决方案,其中在 composer.json
中定义的 "autoload"
命名空间都没有被选中。接下来我会尝试一下,如果也能解决这个问题,我会修改这个答案。
--- 更新 ---
正如预期的那样:当我将 "autoload"
信息也合并到 packages.json
中时,自动加载也开始工作了。
1) 我已经知道 Satis,但我不想安装和学习另一个软件。
我在 Ubuntu 上使用 Composer 1.6.3 并具有以下全局 ~/.config/composer/config.json
文件:
{
"config": {},
"repositories": {
"packagist": false,
"codifier": {
"type": "composer",
"url": "file:///usr/local/codifier/library/PHP/Codifier"
}
}
}
换句话说,我禁用了 Packagist 并指向我在本地计算机上自己的存储库。这个本地存储库有一个 packages.json
定义所有可用的本地包:
{
"packages": {
"codifier/cache": {
"dev-master": {
"name": "codifier/cache",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Cache",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/cache",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Cache",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/environment": {
"dev-master": {
"name": "codifier/environment",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Environment",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/environment",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Environment",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/event": {
"dev-master": {
"name": "codifier/event",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Event",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/event",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Event",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/mime": {
"dev-master": {
"name": "codifier/mime",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Mime",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/mime",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Mime",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/net": {
"dev-master": {
"name": "codifier/net",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Net",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/net",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Net",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/number": {
"dev-master": {
"name": "codifier/number",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Number",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/number",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Number",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/persistence": {
"dev-master": {
"name": "codifier/persistence",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Persistence",
"type": "git",
"reference": "master"
}
},
"0.1.0": {
"name": "codifier/persistence",
"version": "0.1.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Persistence",
"type": "git",
"reference": "v0.1.0"
}
}
},
"codifier/process": {
"dev-master": {
"name": "codifier/process",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Process",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/process",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Process",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/socket": {
"dev-master": {
"name": "codifier/socket",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/socket",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "v1.0.0"
}
}
},
"codifier/stdlib": {
"dev-master": {
"name": "codifier/stdlib",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/StdLib",
"type": "git",
"reference": "master"
}
},
"0.1.0": {
"name": "codifier/stdlib",
"version": "0.1.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/StdLib",
"type": "git",
"reference": "v0.1.0"
}
}
},
"codifier/text": {
"dev-master": {
"name": "codifier/text",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Text",
"type": "git",
"reference": "master"
}
},
"1.0.0": {
"name": "codifier/text",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Text",
"type": "git",
"reference": "v1.0.0"
}
}
}
}
}
到目前为止,这部分工作正常。当我在随机文件夹中执行 $ composer show -a
时,我得到以下输出:
No composer.json found in the current directory, showing available packages from codifier
codifier/cache
codifier/environment
codifier/event
codifier/mime
codifier/net
codifier/number
codifier/persistence
codifier/process
codifier/socket
codifier/stdlib
codifier/text
所有这些包都定义了它们自己的依赖项。例如,codifier/socket
包在其根目录中有以下 composer.json
文件:
{
"name": "codifier/socket",
"authors": [ ##REDACTED## ],
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
},
"autoload": {
"psr-4": {
"Codifier\Socket\": "src"
}
}
}
现在,我认为 Composer 的全部意义在于它会自动递归安装所有必需的依赖项,而不是我必须再次手动 require
所有依赖项,但事实并非如此.
当我创建一个新的干净应用程序目录 $ mkdir Test && cd Test
然后执行 $ composer require codifier/socket
时,我得到以下输出:
Using version ^1.0 for codifier/socket
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing codifier/socket (1.0.0): Cloning v1.0.0 from cache
Writing lock file
Generating autoload files
...然后当我执行 $ composer install
时,我得到以下输出:
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
换句话说,只安装了 codifier/socket
而没有安装其依赖项。因此,Test/vendor
的内容现在是:
- codifier/
- socket/
...当我期望内容是:
- codifier/
- socket/
- event/
- net/
- process/
etc.
...因为我希望 Composer 递归地获取所有必需的依赖项。
我也尝试了 $ composer clear-cache
,但这没有什么区别,我想我也看到一些帖子建议我需要在 composer.json
中定义 "repositories"
这个应用程序,但这对我来说没有任何意义,因为我已经在我的全局 config.json
文件中指出了那些。
我在这里错过了什么?我是否错过了 Composer 的要点,因为我认为它会自动解析并自动安装所有必需的依赖项?
--- 更新 ---
我现在也尝试通过我的本地网络服务器提供包(当然,并相应地更改 json 文件中的所有 "url"
字段),因为我认为 Composer 可能不是'无法正确处理 file://
协议,但无济于事:它给出完全相同的结果:它只安装 codifier/socket
并忽略其依赖项。
我不知道我还需要做什么才能使这个本地 packages.json
存储库正常工作。
刚刚灵机一动,终于想通了怎么让它工作,但感觉很多余。我让它工作的方法是简单地将 "require"
信息从包的 composer.json
文件复制到我存储库的 packages.json
文件中。
再次以codifier/socket
包中的composer.json
文件为例:
{
"name": "codifier/socket",
"authors": [ ##REDACTED## ],
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
},
"autoload": {
"psr-4": {
"Codifier\Socket\": "src"
}
}
}
...它的相关信息在 packages.json
中将变为:
{
"packages": {
...
"codifier/socket": {
"dev-master": {
"name": "codifier/socket",
"version": "dev-master",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "master"
},
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
}
},
"1.0.0": {
"name": "codifier/socket",
"version": "1.0.0",
"source": {
"url": "file:///usr/local/codifier/library/PHP/Codifier/Socket",
"type": "git",
"reference": "v1.0.0"
},
"require": {
"php": ">=7.3.0",
"codifier/event": "^1.0",
"codifier/net": "^1.0",
"codifier/process": "^1.0"
}
}
},
...
}
}
当我将此应用到所有包时,一切正常。
不过,我仍然不明白的是,为什么 Composer 没有从包本身的实际 composer.json
中获取此 "require"
信息。我知道文档说明 Composer doesn't recursively follow repositories,但它肯定必须递归地遵循依赖关系,不是吗?不过,也许它根本就没有,这就是它的工作方式。
这一切都意味着我将不得不做更多的 git 解析以获得包的每个单独版本的正确数据,但就这样吧。1)
这个解决方案也可能会引导我找到我遇到的自动加载问题的解决方案,其中在 composer.json
中定义的 "autoload"
命名空间都没有被选中。接下来我会尝试一下,如果也能解决这个问题,我会修改这个答案。
--- 更新 ---
正如预期的那样:当我将 "autoload"
信息也合并到 packages.json
中时,自动加载也开始工作了。
1) 我已经知道 Satis,但我不想安装和学习另一个软件。