Skip to content

Instantly share code, notes, and snippets.

@chris-moreton
Last active August 29, 2015 14:07
Show Gist options
  • Save chris-moreton/d2e4caffde91b91e795b to your computer and use it in GitHub Desktop.
Save chris-moreton/d2e4caffde91b91e795b to your computer and use it in GitHub Desktop.
Will inject the location of a Toran proxy server to an existing composer.json and composer.lock file.
<?php
define('TORAN_SERVER', 'toran.local');
$json = file_get_contents('composer.json');
$obj = json_decode($json);
if (!property_exists($obj, 'repositories')) {
$obj->repositories = [];
}
$repos = $obj->repositories;
$index = count($repos);
$repos[$index++] = json_decode('{"type": "composer", "url": "http://' . TORAN_SERVER . '/repo/private/"}');
$repos[$index++] = json_decode('{"type": "composer", "url": "http://' . TORAN_SERVER . '/repo/packagist/"}');
$repos[$index++] = json_decode('{"packagist": false}');
$obj->repositories = $repos;
file_put_contents('composer.json', json_encode($obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
if (!file_exists('composer.lock')) {
exit;
}
$json = file_get_contents('composer.lock');
$obj = json_decode($json);
if (!property_exists($obj, 'packages')) {
exit;
}
foreach ($obj->packages as &$package) {
if (property_exists($package, 'source')) {
$package->source->mirrors = json_decode('[
{
"url": "/%package%/%normalizedUrl%.%type%",
"preferred": true
}
]');
};
if (property_exists($package, 'dist')) {
$package->dist->mirrors = json_decode('[
{
"url": "http://' . TORAN_SERVER . '/repo/packagist/dists/%package%/%version%/%reference%.%type%",
"preferred": true
}
]');
};
}
file_put_contents('composer.lock', json_encode($obj, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment