kabachello/composerapi

A wrapper for Composer to call its commands programmatically using a simple object oriented API

0.3.2 2018-02-04 20:55 UTC

This package is auto-updated.

Last update: 2024-11-28 20:29:07 UTC


README

A wrapper for Composer to call it's commands from inside your code using a simple object oriented API: turns php composer.phar require monolog/monolog into $composer->require(array('monolog/monolog:*'));.

Installation

As always, the easiest (and the recommended) way to install is using composer:

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

Note the explicit requirement for composer/composer in a fixed version: this is important to prevent automatic updates of this dependency when running the update command programmatically - see "Known limitations" section below for details.

There is no simple way to install composer an the API without using composer itself. Theoretically you could include ComposerAPI.php in your code manually, but you would need to make sure an installation of "composer/composer" is available under the namespace "\Composer". The trouble is, however, that composer has lot's of dependencies itself, so you will probably end up needing the packaged version (composer.phar) anyway. If so, use the simple composer-install above.

Quick start

Here is an example, that adds the monolog library to an existing composer.json manifest and installes it with all dependencies:

<?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));
?>

Supported commands

All commands take an optional $output argument that must implement the OutputInterface of symfony console. If this argument is not passed, ComposerAPI will use StreamOutput by default.

  • install(array $options = null, OutputInterface $output = null): e.g. $composer->install(). This will probably not be used very often because the API mostly makes sense for managing existing installations and not for installing "from scratch".
  • update(array $package_names = null, array $options = null, OutputInterface $output = null): e.g. $composer->update() or $composer->update(array('monolog/monolog', 'kabachello/composerapi'))
  • require(array $package_names, array $options = null, OutputInterface $output = null): e.g. $composer->require(array('monolog/monolog:~1.16', 'slim/slim'))
  • remove(array $package_names, array $options = null, OutputInterface $output = null): e.g. $composer->remove(array('monolog/monolog'))
  • search(array $search_terms, array $options = null, OutputInterface $output = null): e.g. $composer->search(array('composerapi'))
  • show(array $options = null, OutputInterface $output = null): e.g. $composer->show() or $composer->show(array('--latest'))
  • outdated(array $options = null, OutputInterface $output = null): e.g. $composer->outdated()
  • suggests(array $package_names = null, array $options = null, OutputInterface $output = null): e.g. $composer->suggests() or $composer->suggests(array('symfony/event-dispatcher'), array('--tree'))
  • depends($package_name, $version = null, array $options = null, OutputInterface $output = null): e.g. $composer->depends('doctrine/lexer', array('--tree'))
  • prohibits($package_name, $version = null, array $options = null, OutputInterface $output = null): e.g. $composer->prohibits('symfony/symfony', '3.1', array('--tree'))
  • validate(array $options = null, OutputInterface $output = null): e.g. $composer->validate()
  • config($setting_key, array $setting_values, array $options = null, OutputInterface $output = null): e.g. $composer->config('repositories.foo', array('vcs', 'https://github.com/foo/bar'))

Known limitations

Self-update for composer not working properly

Since composer itself is a dependency of ComposerAPI, calling ComposerAPI->update() will also lead to an attempt to update composer. This won't work in most cases because the dependencies of composer will get updated one-by-one leading to inconsistencies. Solution: Pin composer to a concrete version in your root composer.json to block updating it. If you want to update composer manuall, use the packaged version (composer.phar) just like when installing ComposerAPI.

Resource consumption

Composer often needs a lot of memory and a lot of time too. Running commands like update will often take longer than PHP's max_execution_time or ecxeed the memory_limit. ComposerAPI tries to increase these limits at runtime, however this will not work if PHP runs in safe mode. Unfortunately, I do not see a simple solution for this issue. Feel free to suggest one!