Skip to content

Instantly share code, notes, and snippets.

@tomzx
Last active January 21, 2016 23:51
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 tomzx/f008527dac4c29a2b541 to your computer and use it in GitHub Desktop.
Save tomzx/f008527dac4c29a2b541 to your computer and use it in GitHub Desktop.
Shorten melody-like script to install composer packages for a script
<?php
$packages = [
// Your packages here
'tomzx/file-tracker: ~0.1@dev',
];
$commands = [
['require', 'packages' => $packages, '--no-update' => true],
['install'],
];
if ( ! file_exists('composer.phar')) {
copy('https://getcomposer.org/composer.phar', 'composer.phar');
}
$workingDirectory = sys_get_temp_dir() . '/' . sha1(__FILE__);
if ( ! is_dir($workingDirectory)) {
mkdir($workingDirectory, 0755, true);
}
$shouldInstall = false;
$composerJson = $workingDirectory . '/composer.json';
$composerLock = $workingDirectory . '/composer.lock';
if (is_readable($composerJson) && is_readable($composerLock)) {
$hash = md5_file($composerJson);
$lockContent = file_get_contents($composerLock);
$shouldInstall = strpos($lockContent, $hash) === false;
} else {
$shouldInstall = true;
}
if ($shouldInstall) {
require_once 'phar://composer.phar/vendor/autoload.php';
$application = new \Composer\Console\Application();
$application->setAutoExit(false);
foreach ($commands as $command) {
$command = array_merge($command, [
'--working-dir' => $workingDirectory,
]);
$application->run(new \Symfony\Component\Console\Input\ArrayInput($command), new \Symfony\Component\Console\Output\NullOutput());
}
}
require_once $workingDirectory.'/vendor/autoload.php';
// Your script here using the installed packages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment