Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP script to install packages globally on a linux/mac box
<?php
$composer_src = "https://getcomposer.org/composer.phar";
$phpdoc_src = "http://phpdoc.org/phpDocumentor.phar";
$phpunit_src = "https://phar.phpunit.de/phpunit.phar";
$yes_values = ['', 'y', 'Y', 'yes', 'Yes'];
$bin_path = "/usr/local/bin/";
function install_package($name, $package_url)
{
global $yes_values;
global $bin_path;
// check if file exists in /usr/local/bin
$exists = file_exists($bin_path . $name);
if ($exists) {
echo "{$name} already exists, do you want to reinstall [Y/n]: ";
$stdin = trim(fgets(STDIN));
if (in_array($stdin, $yes_values)) {
download($name, $package_url);
} else {
return false;
}
} else {
$ucname = ucfirst($name);
echo "Do you want to install {$ucname} [Y/n]: ";
$stdin = trim(fgets(STDIN));
if (in_array($stdin, $yes_values))
{
download($name, $package_url);
}
}
return true;
}
function download($name, $package_url) {
global $bin_path;
echo "Installing {$name} \r\n";
$file_contents = file_get_contents($package_url);
echo "File downloaded, saving \r\n";
$filename = $bin_path . $name;
if (file_exists($filename)) {
unlink($filename);
}
/* create file for writing */
try {
file_put_contents($filename, $file_contents);
chmod($filename, 0775); // user and group RWX, other R
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Script to download composer and phpdoc globally onto the vagrant box
*
* Composer is not needed if using homestead but cannot hurt to redownload
*/
$packages = [
'composer' => $composer_src,
'phpdoc' => $phpdoc_src,
'phpunit' => $phpunit_src,
];
foreach ($packages as $name => $src) {
install_package($name, $src);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment