kabachello/composerapi

Composer 的包装器,允许通过简单的面向对象的 API 以编程方式调用其命令

0.3.2 2018-02-04 20:55 UTC

This package is auto-updated.

Last update: 2024-08-28 20:04:28 UTC


README

一个包装器,用于通过简单面向对象的 API 从代码内部调用 Composer 的命令:将 php composer.phar require monolog/monolog 转换为 $composer->require(array('monolog/monolog:*'));

安装

与往常一样,最简单(也是推荐)的安装方法是使用 composer

composer require composer/composer:1.2 kabachello/composerapi:*

请注意对 composer/composer 的明确版本要求:这很重要,可以防止在运行 update 命令时自动更新此依赖项 - 有关详细信息,请参阅下文“已知限制”部分。

没有简单的方法在不使用 composer 的情况下安装 composer API。理论上可以将 ComposerAPI.php 手动包含在代码中,但您需要确保在命名空间 "\Composer" 下有 "composer/composer" 的安装。然而,composer 本身有很多依赖项,所以您可能仍然需要使用打包版本(composer.phar)。如果是这样,请使用上述简单的 composer-install。

快速入门

以下是一个示例,它将 monolog 库添加到现有的 composer.json 清单中,并安装它及其所有依赖项

<?php
// Instantiate ComposerAPI
$composer = new \kabachello\ComposerAPI\ComposerAPI("path_to_the_folder_with_your_composer_json");
// Run the require command for monolog/monolog (latest version). The default output will be symfony's StreamOutput
$output = $composer->require(array('monolog/monolog:*'));
// Fetch the stream
$stream  = $output->getStream();
// Rewind it to get the full contents
rewind($stream);
// Print everything composer had written to the console
echo(stream_get_contents($stream));
?>

支持的命令

所有命令都接受一个可选的 $output 参数,该参数必须实现 symfony console 的 OutputInterface。如果未传递此参数,ComposerAPI 将默认使用 StreamOutput

  • install(array $options = null, OutputInterface $output = null):例如 $composer->install()。这可能不太常用,因为 API 主要适用于管理现有安装,而不是从“零开始”安装。
  • update(array $package_names = null, array $options = null, OutputInterface $output = null):例如 $composer->update()$composer->update(array('monolog/monolog', 'kabachello/composerapi'))
  • require(array $package_names, array $options = null, OutputInterface $output = null):例如 $composer->require(array('monolog/monolog:~1.16', 'slim/slim'))
  • remove(array $package_names, array $options = null, OutputInterface $output = null):例如 $composer->remove(array('monolog/monolog'))
  • search(array $search_terms, array $options = null, OutputInterface $output = null):例如 $composer->search(array('composerapi'))
  • show(array $options = null, OutputInterface $output = null):例如 $composer->show()$composer->show(array('--latest'))
  • outdated(array $options = null, OutputInterface $output = null):例如 $composer->outdated()
  • suggests(array $package_names = null, array $options = null, OutputInterface $output = null):例如 $composer->suggests()$composer->suggests(array('symfony/event-dispatcher'), array('--tree'))
  • depends($package_name, $version = null, array $options = null, OutputInterface $output = null):例如 $composer->depends('doctrine/lexer', array('--tree'))
  • prohibits($package_name, $version = null, array $options = null, OutputInterface $output = null):例如 $composer->prohibits('symfony/symfony', '3.1', array('--tree'))
  • validate(array $options = null, OutputInterface $output = null):例如 $composer->validate()
  • config($setting_key, array $setting_values, array $options = null, OutputInterface $output = null):例如 $composer->config('repositories.foo', array('vcs', 'https://github.com/foo/bar'))

已知限制

Composer 的自我更新不正常

由于Composer自身是ComposerAPI的依赖项,调用ComposerAPI->update()也会导致尝试更新Composer。这在大多数情况下都不会起作用,因为Composer的依赖项将逐个更新,导致不一致。解决方案:在您的根composer.json中将Composer锁定到具体版本以阻止更新。如果您想手动更新Composer,请使用打包版本(composer.phar),就像安装ComposerAPI时一样。

资源消耗

Composer通常需要大量的内存和大量的时间。运行像update这样的命令通常会比PHP的max_execution_time长,或者超出memory_limit。ComposerAPI会尝试在运行时增加这些限制,然而如果PHP运行在安全模式下,这将不起作用。不幸的是,我没有看到这个问题的简单解决方案。请随时提出建议!