strausmann/composerapi

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

0.3.5 2018-02-27 13:41 UTC

This package is auto-updated.

Last update: 2024-09-06 23:01:48 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

ComposerAPI

一个包装器,用于通过简单的面向对象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运行在安全模式下,这不会起作用。不幸的是,我看不到这个问题的简单解决方案。请随意提出建议!

感谢

感谢@kabachello对源项目的贡献