Giter VIP home page Giter VIP logo

composerapi's Introduction

Latest Stable Version Total Downloads Latest Unstable Version License

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

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!

Thanks

Thanks for @kabachello for the Source Project

composerapi's People

Contributors

strausmann avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.