net-tools/composer-interface

PHP shell interface to Composer when SSH is not available on your host

1.0.16 2023-09-10 12:43 UTC

This package is auto-updated.

Last update: 2024-10-10 15:03:07 UTC


README

PHP interface to Composer when SSH is not available on your host

The package contains an helper class to interface with Composer when your host (or hosting plan) does not support SSH (direct access).

With this simple PHP class, you can execute composer command through shell calls and get the output of the command in a string. A simple GUI is also provided in the demo/ folder.

Setup instructions

Download the ZIP release and upload it's content to your host.

Since you are looking for a Composer interface, we assume that you can't run Composer at the moment (no SSH access), that's why you have to manually download the library and upload it on your website.

How to use ?

In order to use the PHP interface to Composer, and after having uploaded the ZIP release to your host, you must :

  1. create a client config file in JSON format (or define a PHP associative array) ; see below for required parameters,
  2. include the src/autoload.php file (this is a custom autoload file, since Composer is not installed yet, you don't have its autoload mechanism available),
  3. in a PHP script, create a ComposerInterface object with a config object and the path of your Composer project,
  4. call one of the supported Composer command as methods of the ComposerInterface object.

The JSON client config file or the associative config array MUST define the following values :

Then create a config instance and pass it to the ComposerInterface constructor :

// include script (you may point the inclusion to the right path to src/autoload.php)
include_once "autoload.php";

// create config object
$config = \Nettools\ComposerInterface\Config::fromJSON(__DIR__ . '/composer.config.json');
// OR
// $config = \Nettools\ComposerInterface\Config::fromArray(array('composer_phpbin'=>'usr/local/php7.0/bin/php', 'composer_home'=>$_SERVER['DOCUMENT_ROOT']));

// create interface and set the composer project to be in folder LIBCOMPOSER, directly placed under the web root folder
$composer = new ComposerInterface($config, rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/libcomposer');

For example, to do the initial setup of Composer :

$output = $composer->setup();
echo "<pre>$output</pre>";

And then to require a package :

$output = $composer->package_require('net-tools/core');
echo "<pre>$output</pre>";

Composer commands have been separated in 3 groups :

  • global commands
  • commands applied to packages
  • commands applied to repositories

Global commands are called with the corresponding method name (eg $composer->diagnose()). Package commands are called with the package_ prefix (eg $composer->package_update('vendor/name')). Repositories commands are more specific :

  • to add a repository : $composer->repository_add('path', 'http://url')
  • to remove a repository : $composer->repository_remove('http://url')

If you want to send a command not supported by ComposerInterface, you may use the Command method : $composer->command('unsupportedcommand vendor/name arguments').

Demo

A simple GUI interface is provided in the demo/ subfolder.

To run the demo, just modify the provided client config file composer.config.json (in the demo folder) to point to the PHP binary on your host, and open your browser to the gui_demo.php file of the demo folder. If you don't know the path for the PHP binary, you'd better refer to your host support team.

The demo will create a libc-composerinterface folder on your web root. If you want another name or path, modify the PROJECT constant on the top lines of the demo file.

Please note that the demo comes with an empty composer project and no Composer package. The Composer software MUST be installed. To do so, you have to hit the SETUP button on the GUI (or call the setup method of ComposerInterface in a script of your own). This will download Composer and run the install script. A default composer.json will be created. THEN you can play around with require, update, etc.