2017 © Pedro Peláez
 

library composerapi

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

image

kabachello/composerapi

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

  • Sunday, February 4, 2018
  • by Kabachnik
  • Repository
  • 3 Watchers
  • 12 Stars
  • 266 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 9 Versions
  • 3 % Grown

The README.md

ComposerAPI

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:*'));., (*1)

Installation

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

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., (*3)

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., (*4)

Quick start

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

<?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., (*6)

  • 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., (*7)

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!, (*8)

The Versions

04/02 2018

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

04/02 2018

0.2.3

0.2.3.0

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

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

04/02 2018

0.3.2

0.3.2.0

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

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

14/10 2016

0.3.1

0.3.1.0

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

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

19/07 2016

0.3

0.3.0.0

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

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

18/07 2016

0.2.2

0.2.2.0

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

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

18/07 2016

0.2.1

0.2.1.0

A wrapper for Composer to call it's commands programmatically using a simple intuitive API

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

18/07 2016

0.2

0.2.0.0

A wrapper for Composer to call it's commands programmatically using a simple intuitive API

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik

16/07 2016

0.1

0.1.0.0

A wrapper for Composer to call it's commands programmatically using a simple intuitive API

  Sources   Download

MIT

The Requires

 

by Andrej Kabachnik