Run Composer with a PHP Script in Browser

Run composer with a PHP script in browser

Yes you can run Composer with a little PHP wrapper. All of the Composer source code is available in the Phar file, so it can be extracted and then you can run it after setting up an InputInterface to replace Composer expecting the commands to be passed in via the command line.

If you setup your directory structure like this:

./project  
./project/composer.json
./project/composer.lock
./project/webroot/composerExtractor.php
./project/var/

Put the code below into composerExtractor.php and then run it from a web-browser, Composer should download all the libraries into:

./project/vendors/

As well as generating the class-loader files in that directory as well.

composerExtractor.php

<?php

define('EXTRACT_DIRECTORY', "../var/extractedComposer");

if (file_exists(EXTRACT_DIRECTORY.'/vendor/autoload.php') == true) {
echo "Extracted autoload already exists. Skipping phar extraction as presumably it's already extracted.";
}
else{
$composerPhar = new Phar("Composer.phar");
//php.ini setting phar.readonly must be set to 0
$composerPhar->extractTo(EXTRACT_DIRECTORY);
}

//This requires the phar to have been extracted successfully.
require_once (EXTRACT_DIRECTORY.'/vendor/autoload.php');

//Use the Composer classes
use Composer\Console\Application;
use Composer\Command\UpdateCommand;
use Symfony\Component\Console\Input\ArrayInput;

// change out of the webroot so that the vendors file is not created in
// a place that will be visible to the intahwebz
chdir('../');

//Create the commands
$input = new ArrayInput(array('command' => 'update'));

//Create the application and run it with the commands
$application = new Application();
$application->run($input);

?>

Although this is possible, it's not a fantastic idea but may be necessary if you can't use a host that gives you ssh access.

I'd strongly recommend at least getting a static IP address for yourself or your office and then restricting access to just your own IP, as well as probably deleting this script after it's run on the server to prevent it being accidentally run again.

Calling Composer from php

Did you try command exec("php composer.phar update") exec description?

UPDATED AFTER DISCUSSION:

The answer on code composer.phar/src/Composer/Command/StatusCommand.php [lines 43-92] - it can used for check updates of repositories

Why can't I run the composer script from PHP using exec, shell_exec, system?

I solved it by disabling the xdebug extension.

From the doc:

To improve performance when the xdebug extension is enabled, Composer
automatically restarts PHP without it.

So I guess that this "PHP restart" is an issue when calling the binary/phar from PHP.

It is be possible to use the environment variable COMPOSER_ALLOW_XDEBUG to get it working with xdebug, also disbaling a few xdebug options that could alter performances:

<?php
$result = shell_exec('COMPOSER_ALLOW_XDEBUG=1 /usr/bin/env php -d xdebug.remote_enable=0 -d xdebug.profiler_enable=0 -d xdebug.default_enable=0 composer.phar --version 2>&1');

How do I install Composer PHP packages without Composer?

The composer.json file lists the dependencies. In your example:

"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0"
},

You must then find the corresponding packages in the packagist site. Repeat the same process for each dependency: find additional dependencies in their corresponding composer.json files and search again.

When you finally have a complete list of the required packages, you only need to install them all one by one. For the most part, it's just a matter of dropping the files somewhere in your project directory. But you must also ensure that PHP can find the needed classes. Since you aren't using Composer's auto-loader, you need to add them to your own custom autoloader. You can figure out the information from the respective composer.json files, e.g.:

"autoload": {
"psr-4": { "Coinbase\\Wallet\\": "src/" }
},

If you don't use a class auto-loader you'll need to figure out the individual require_once statements. You'll probably need a lot of trial and error because most library authors won't care documenting that.

Also, and just in case there's confusion about this:

  • Composer has an official GUI installer for Windows and a copy and paste command-line installation procedure for all platforms.
  • Composer can be run locally and its output just uploaded elsewhere. You don't need SSH in your shared hosting.
  • The command needed to install a library can be copied and pasted from the package web site—even if the package maintainer didn't care to document it, packagist.org generates it by default.

Composer is not perfect and it doesn't suit all use cases but, when it comes to installing a library that relies on it, it's undoubtedly the best alternative and it's a fairly decent one.


I've checked other answers that came after mine. They mostly fall in two categories:

  1. Install a library and write a custom download script with it
  2. Use an online web based interface for Composer

Unless I'm missing something, none of them address the complaints expressed by the OP:

  • Learning curve
  • Use of third-party software
  • Possibility to develop right on the server (using SSH, I presume)
  • Potentially deep dependency tree

How can I provide a script for PHP CLI via composer (as standalone, and as dependency)

A common practice is to look into both locations for an autoload file. See for instance this snippet used by Behat:

function includeIfExists($file)
{
if (file_exists($file)) {
return include $file;
}
}
if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../autoload.php'))) {
fwrite(STDERR,
'You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL
);
exit(1);
}

Command to run html with php script

Rename file.html to file.php and check.
I think you write PHP and HTML in a single document, and you use only html extension.



Related Topics



Leave a reply



Submit