Skip to content

Instantly share code, notes, and snippets.

@neoacevedo
Created November 24, 2017 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neoacevedo/63946072916c45ea3a16f44d87c74d82 to your computer and use it in GitHub Desktop.
Save neoacevedo/63946072916c45ea3a16f44d87c74d82 to your computer and use it in GitHub Desktop.
Run composer from Web Browser
<?php
// $command may have one of the following content:
// $command = ["command" => "install"]; # only for install without anything
// $command = ['command' => 'require', 'packages' => ['maatwebsite/excel:~2.1.0']]; # for install a specific package
// $command = ['command' => 'remove', 'packages' => ['maatwebsite/excel:~2.1.0']]; # for remove a specific package
// define directory for extract
define('EXTRACT_DIRECTORY', "/your/desired/directory");
//Use the Composer classes
use Composer\Console\Application;
use Composer\Command\UpdateCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\BufferedOutput;
$composerPhar = new Phar(EXTRACT_DIRECTORY . "/composer.phar");
// you must use it with null and true parameters if you wish to call manytimes this file
$composerPhar->extractTo(EXTRACT_DIRECTORY, null, true);
putenv('COMPOSER_HOME=' . EXTRACT_DIRECTORY . '/bin/composer');
//This requires the phar to have been extracted successfully.
require_once (EXTRACT_DIRECTORY . '/vendor/autoload.php');
// goto extract directory
chdir(EXTRACT_DIRECTORY);
// increase memory limit (needed if you run in an environment with less than 256MB but you have up to 256MB)
ini_set("memory_limit", "256M");
//Create the commands
$input = new ArrayInput($command);
$input->setInteractive(false);
//Create the application
$application = new Application();
// avoid calling exit;
$application->setAutoExit(false);
echo "<pre>";
// start buffering.
// some other codes invokes StreamOutput or ConsoleOutput but
// at the end, composer uses "echo 1> /dev/null what causes buffer get empty before to be used as html output
$output = new BufferedOutput();
$application->run($input, $output);
$stream_output = $output->fetch();// get buffer string
echo $stream_output;
echo "</pre><br />";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment