Created
February 20, 2014 16:02
-
-
Save thomascube/9117025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Distributed Composer package registration script | |
* | |
* This script registers the given composer module to the local installation registry | |
* in order to let composer.phar build autoloader files for installed modules. | |
* | |
* Usage: php add-package.php <PATH-TO-MODULE-COMPOSER.JSON> <VERSION> | |
* | |
* Requires composer.phar to be installed in the current directory | |
* which is the global php composer module installation path (e.g. /usr/share/php) | |
* | |
* @author Thomas Bruederli <[email protected]> | |
* | |
* Copyright (C) 2014, Thomas Bruederli, Bern, Switzerland | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Affero General Public License as | |
* published by the Free Software Foundation, either version 3 of the | |
* License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Affero General Public License for more details. | |
* | |
* You should have received a copy of the GNU Affero General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
ini_set('error_reporting', E_ALL & ~E_NOTICE & ~E_STRICT); | |
require_once 'phar://composer.phar/src/Composer/Json/JsonFile.php'; | |
try { | |
$instfile = new Composer\Json\JsonFile('vendor/composer/installed.json'); | |
$installed = $instfile->read(); | |
} | |
catch (Exception $e) { | |
$installed = array(); | |
} | |
$modfile = $_SERVER['argv'][1]; | |
$version = $_SERVER['argv'][2]; | |
if (empty($modfile) || empty($version)) { | |
echo "Usage: php add-package.php PATH-TO-MODULE-COMPOSER.JSON VERSION\n"; | |
exit(2); | |
} | |
//// add the module's configuration to vendor/composer/installed.json | |
try { | |
$j = new Composer\Json\JsonFile($modfile); | |
$modconf = $j->read(); | |
} | |
catch (Exception $e) { | |
echo "Failed to load module composer file: $modfile\n"; | |
exit(2); | |
} | |
$modname = $modconf['name']; | |
$modconf['version'] = $version; | |
$modconf['version_normalized'] = $version; | |
foreach ($installed as $i => $m) { | |
if ($m['name'] == $modname) { | |
echo "* replacing " . $m['name'] . ':' . $m['version']. "\n"; | |
unset($installed[$i]); | |
break; | |
} | |
} | |
echo "* adding $modname:$version\n"; | |
$installed[] = $modconf; | |
try { | |
echo "writing vendor/composer/installed.json ..."; | |
$instfile->write(array_values($installed)); | |
echo "done.\n"; | |
} | |
catch (Exception $e) { | |
echo "Failed to write vendor/composer/installed.json\n"; | |
exit(2); | |
} | |
//// add module to the composer.json registry | |
try { | |
$regfile = new Composer\Json\JsonFile('composer.json'); | |
$composer = $regfile->read(); | |
} | |
catch (Exception $e) { | |
$composer = array(); | |
} | |
$composer['require'][$modname] = $version; | |
try { | |
echo "writing composer.json ..."; | |
$regfile->write($composer); | |
echo "done.\n"; | |
} | |
catch (Exception $e) { | |
echo "Failed to write composer.json\n"; | |
exit(2); | |
} | |
// TODO: run `php composer.phar dump-autoload --optimize` right away? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment