Skip to content

Instantly share code, notes, and snippets.

@Halleck45
Last active February 24, 2020 14:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Halleck45/5876284 to your computer and use it in GitHub Desktop.
Save Halleck45/5876284 to your computer and use it in GitHub Desktop.
This tool generate local cache of your dependencies and install satis server to use it
#!/usr/bin/php
<?php
$cacheDestination = isset($argv[1]) ?$argv[1] : null;
$satisDestination = isset($argv[2]) ?$argv[2] : null;
$composerFile = 'server-satis.json';
if (in_array($cacheDestination, array('--help', null))) {
die(sprintf('Accelerator for composer
This tool generate local cache of your dependencies,
and install satis to use it.
Usage:
1. add your composer.json file here
2. run ./%s /path/to/store/cache /path/to/install/satis
', basename(__FILE__)));
}
if (!file_exists($cacheDestination)) {
die("Destination doesn't exist");
}
$cacheDestination = realpath($cacheDestination);
$cacheDestination = rtrim($cacheDestination, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
// get composer
echo "\nGet composer";
if (!file_exists('composer.phar')) {
shell_exec('wget http://getcomposer.org/composer.phar');
}
// get used packages
echo "\nGet used packages";
exec('php composer.phar install --dev --dry-run --no-interaction', $out, $return);
if (0 !== $return) {
die(PHP_EOL . implode(PHP_EOL, $out) . PHP_EOL);
}
$lines = array_filter($out, function($v) {
return preg_match('!\- Installing !', $v);
}
);
echo "\nInstall each package in $cacheDestination";
$packages = array();
foreach ($lines as $line) {
preg_match('!Installing\s+(.*?) !', $line, $m);
list(, $name) = $m;
// search on packagist
$url = sprintf('https://packagist.org/p/%s.json', $name);
$json = json_decode(file_get_contents($url));
$p = current($json->packages);
$p = current($p);
$url = $p->source->url;
// clone
echo sprintf("\ncloning %s", $name);
$name = preg_replace('([^A-Za-z0-9])', '-', $name);
$dirname = $cacheDestination . $name . '.git';
array_push($packages, $dirname);
if (file_exists($dirname)) {
continue;
} else {
$command = sprintf('git clone %s %s', $url, $dirname);
`$command`;
}
}
// server-satis.json
$content = '{
"name":"composer local cache",
"repositories": [ ';
foreach ($packages as $n => $dir) {
$content .=
"\n\t" . ($n == 0 ? '' : ',') . '{'
. "\n\t\t" . '"type": "vcs",'
. sprintf("\n\t\t" . '"url": "%s"', $dir)
. "\n\t" . '}';
}
$content .= PHP_EOL . '] }';
file_put_contents($composerFile, $content);
// satis
echo "\n\nInstallation of Satis...";
if (!file_exists('satis')) {
$command = 'php composer.phar create-project composer/satis --stability=dev --no-interaction';
shell_exec($command);
}
// package
echo "\nCreating package and install server";
$command = sprintf('php satis/bin/satis build %s %s', $composerFile, $satisDestination);
shell_exec($command);
echo "Your satis server is installed and configured.";
echo "\n\n" . 'Now:
1. add virtualhost on ' . realpath($satisDestination) . '
2. add the following content to your composer.json:
"repositories": [
{
"type": "composer",
"url": "<your host>"
},
{
"packagist": false
}
]
';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment