|
@@ -17,7 +17,7 @@ use Composer\Json\JsonFile;
|
|
|
use Composer\IO\IOInterface;
|
|
|
use Composer\Package\Archiver;
|
|
|
use Composer\Repository\RepositoryManager;
|
|
|
-use Composer\Repository\RepositoryInterface;
|
|
|
+use Composer\Repository\WritableRepositoryInterface;
|
|
|
use Composer\Util\ProcessExecutor;
|
|
|
use Composer\Util\RemoteFilesystem;
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
|
@@ -36,14 +36,12 @@ use Composer\Package\Version\VersionParser;
|
|
|
class Factory
|
|
|
{
|
|
|
/**
|
|
|
+ * @return string
|
|
|
* @throws \RuntimeException
|
|
|
- * @return Config
|
|
|
*/
|
|
|
- public static function createConfig()
|
|
|
+ protected static function getHomeDir()
|
|
|
{
|
|
|
- // determine home and cache dirs
|
|
|
$home = getenv('COMPOSER_HOME');
|
|
|
- $cacheDir = getenv('COMPOSER_CACHE_DIR');
|
|
|
if (!$home) {
|
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
|
|
if (!getenv('APPDATA')) {
|
|
@@ -57,6 +55,18 @@ class Factory
|
|
|
$home = rtrim(getenv('HOME'), '/') . '/.composer';
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return $home;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $home
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ protected static function getCacheDir($home)
|
|
|
+ {
|
|
|
+ $cacheDir = getenv('COMPOSER_CACHE_DIR');
|
|
|
if (!$cacheDir) {
|
|
|
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
|
|
|
if ($cacheDir = getenv('LOCALAPPDATA')) {
|
|
@@ -70,6 +80,21 @@ class Factory
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ return $cacheDir;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param IOInterface|null $io
|
|
|
+ * @return Config
|
|
|
+ */
|
|
|
+ public static function createConfig(IOInterface $io = null, $cwd = null)
|
|
|
+ {
|
|
|
+ $cwd = $cwd ?: getcwd();
|
|
|
+
|
|
|
+ // determine home and cache dirs
|
|
|
+ $home = self::getHomeDir();
|
|
|
+ $cacheDir = self::getCacheDir($home);
|
|
|
+
|
|
|
// Protect directory against web access. Since HOME could be
|
|
|
// the www-data's user home and be web-accessible it is a
|
|
|
// potential security risk
|
|
@@ -82,48 +107,30 @@ class Factory
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $config = new Config();
|
|
|
+ $config = new Config(true, $cwd);
|
|
|
|
|
|
// add dirs to the config
|
|
|
$config->merge(array('config' => array('home' => $home, 'cache-dir' => $cacheDir)));
|
|
|
|
|
|
+ // load global config
|
|
|
$file = new JsonFile($home.'/config.json');
|
|
|
if ($file->exists()) {
|
|
|
+ if ($io && $io->isDebug()) {
|
|
|
+ $io->write('Loading config file ' . $file->getPath());
|
|
|
+ }
|
|
|
$config->merge($file->read());
|
|
|
}
|
|
|
$config->setConfigSource(new JsonConfigSource($file));
|
|
|
|
|
|
- // move old cache dirs to the new locations
|
|
|
- $legacyPaths = array(
|
|
|
- 'cache-repo-dir' => array('/cache' => '/http*', '/cache.svn' => '/*', '/cache.github' => '/*'),
|
|
|
- 'cache-vcs-dir' => array('/cache.git' => '/*', '/cache.hg' => '/*'),
|
|
|
- 'cache-files-dir' => array('/cache.files' => '/*'),
|
|
|
- );
|
|
|
- foreach ($legacyPaths as $key => $oldPaths) {
|
|
|
- foreach ($oldPaths as $oldPath => $match) {
|
|
|
- $dir = $config->get($key);
|
|
|
- if ('/cache.github' === $oldPath) {
|
|
|
- $dir .= '/github.com';
|
|
|
- }
|
|
|
- $oldPath = $config->get('home').$oldPath;
|
|
|
- $oldPathMatch = $oldPath . $match;
|
|
|
- if (is_dir($oldPath) && $dir !== $oldPath) {
|
|
|
- if (!is_dir($dir)) {
|
|
|
- if (!@mkdir($dir, 0777, true)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- if (is_array($children = glob($oldPathMatch))) {
|
|
|
- foreach ($children as $child) {
|
|
|
- @rename($child, $dir.'/'.basename($child));
|
|
|
- }
|
|
|
- }
|
|
|
- if ($config->get('cache-dir') != $oldPath) {
|
|
|
- @rmdir($oldPath);
|
|
|
- }
|
|
|
- }
|
|
|
+ // load global auth file
|
|
|
+ $file = new JsonFile($config->get('home').'/auth.json');
|
|
|
+ if ($file->exists()) {
|
|
|
+ if ($io && $io->isDebug()) {
|
|
|
+ $io->write('Loading config file ' . $file->getPath());
|
|
|
}
|
|
|
+ $config->merge(array('config' => $file->read()));
|
|
|
}
|
|
|
+ $config->setAuthConfigSource(new JsonConfigSource($file, true));
|
|
|
|
|
|
return $config;
|
|
|
}
|
|
@@ -146,7 +153,7 @@ class Factory
|
|
|
$repos = array();
|
|
|
|
|
|
if (!$config) {
|
|
|
- $config = static::createConfig();
|
|
|
+ $config = static::createConfig($io);
|
|
|
}
|
|
|
if (!$rm) {
|
|
|
if (!$io) {
|
|
@@ -157,11 +164,14 @@ class Factory
|
|
|
}
|
|
|
|
|
|
foreach ($config->getRepositories() as $index => $repo) {
|
|
|
+ if (is_string($repo)) {
|
|
|
+ throw new \UnexpectedValueException('"repositories" should be an array of repository definitions, only a single repository was given');
|
|
|
+ }
|
|
|
if (!is_array($repo)) {
|
|
|
- throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') should be an array, '.gettype($repo).' given');
|
|
|
+ throw new \UnexpectedValueException('Repository "'.$index.'" ('.json_encode($repo).') should be an array, '.gettype($repo).' given');
|
|
|
}
|
|
|
if (!isset($repo['type'])) {
|
|
|
- throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a type defined');
|
|
|
+ throw new \UnexpectedValueException('Repository "'.$index.'" ('.json_encode($repo).') must have a type defined');
|
|
|
}
|
|
|
$name = is_int($index) && isset($repo['url']) ? preg_replace('{^https?://}i', '', $repo['url']) : $index;
|
|
|
while (isset($repos[$name])) {
|
|
@@ -176,16 +186,19 @@ class Factory
|
|
|
/**
|
|
|
* Creates a Composer instance
|
|
|
*
|
|
|
- * @param IOInterface $io IO instance
|
|
|
- * @param array|string|null $localConfig either a configuration array or a filename to read from, if null it will
|
|
|
- * read from the default filename
|
|
|
+ * @param IOInterface $io IO instance
|
|
|
+ * @param array|string|null $localConfig either a configuration array or a filename to read from, if null it will
|
|
|
+ * read from the default filename
|
|
|
* @param bool $disablePlugins Whether plugins should not be loaded
|
|
|
+ * @param bool $fullLoad Whether to initialize everything or only main project stuff (used when loading the global composer)
|
|
|
* @throws \InvalidArgumentException
|
|
|
* @throws \UnexpectedValueException
|
|
|
* @return Composer
|
|
|
*/
|
|
|
- public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false)
|
|
|
+ public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false, $cwd = null, $fullLoad = true)
|
|
|
{
|
|
|
+ $cwd = $cwd ?: getcwd();
|
|
|
+
|
|
|
// load Composer configuration
|
|
|
if (null === $localConfig) {
|
|
|
$localConfig = static::getComposerFile();
|
|
@@ -197,7 +210,7 @@ class Factory
|
|
|
|
|
|
if (!$file->exists()) {
|
|
|
if ($localConfig === './composer.json' || $localConfig === 'composer.json') {
|
|
|
- $message = 'Composer could not find a composer.json file in '.getcwd();
|
|
|
+ $message = 'Composer could not find a composer.json file in '.$cwd;
|
|
|
} else {
|
|
|
$message = 'Composer could not find the config file: '.$localConfig;
|
|
|
}
|
|
@@ -209,26 +222,45 @@ class Factory
|
|
|
$localConfig = $file->read();
|
|
|
}
|
|
|
|
|
|
- // Configuration defaults
|
|
|
- $config = static::createConfig();
|
|
|
+ // Load config and override with local config/auth config
|
|
|
+ $config = static::createConfig($io, $cwd);
|
|
|
$config->merge($localConfig);
|
|
|
- $io->loadConfiguration($config);
|
|
|
+ if (isset($composerFile)) {
|
|
|
+ if ($io && $io->isDebug()) {
|
|
|
+ $io->write('Loading config file ' . $composerFile);
|
|
|
+ }
|
|
|
+ $localAuthFile = new JsonFile(dirname(realpath($composerFile)) . '/auth.json');
|
|
|
+ if ($localAuthFile->exists()) {
|
|
|
+ if ($io && $io->isDebug()) {
|
|
|
+ $io->write('Loading config file ' . $localAuthFile->getPath());
|
|
|
+ }
|
|
|
+ $config->merge(array('config' => $localAuthFile->read()));
|
|
|
+ $config->setAuthConfigSource(new JsonConfigSource($localAuthFile, true));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
$vendorDir = $config->get('vendor-dir');
|
|
|
$binDir = $config->get('bin-dir');
|
|
|
|
|
|
- // setup process timeout
|
|
|
- ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
|
|
|
-
|
|
|
// initialize composer
|
|
|
$composer = new Composer();
|
|
|
$composer->setConfig($config);
|
|
|
|
|
|
+ if ($fullLoad) {
|
|
|
+ // load auth configs into the IO instance
|
|
|
+ $io->loadConfiguration($config);
|
|
|
+
|
|
|
+ // setup process timeout
|
|
|
+ ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
|
|
|
+ }
|
|
|
+
|
|
|
// initialize event dispatcher
|
|
|
$dispatcher = new EventDispatcher($composer, $io);
|
|
|
+ $composer->setEventDispatcher($dispatcher);
|
|
|
|
|
|
// initialize repository manager
|
|
|
$rm = $this->createRepositoryManager($io, $config, $dispatcher);
|
|
|
+ $composer->setRepositoryManager($rm);
|
|
|
|
|
|
// load local repository
|
|
|
$this->addLocalRepository($rm, $vendorDir);
|
|
@@ -237,45 +269,47 @@ class Factory
|
|
|
$parser = new VersionParser;
|
|
|
$loader = new Package\Loader\RootPackageLoader($rm, $config, $parser, new ProcessExecutor($io));
|
|
|
$package = $loader->load($localConfig);
|
|
|
+ $composer->setPackage($package);
|
|
|
|
|
|
// initialize installation manager
|
|
|
$im = $this->createInstallationManager();
|
|
|
-
|
|
|
- // Composer composition
|
|
|
- $composer->setPackage($package);
|
|
|
- $composer->setRepositoryManager($rm);
|
|
|
$composer->setInstallationManager($im);
|
|
|
|
|
|
- // initialize download manager
|
|
|
- $dm = $this->createDownloadManager($io, $config, $dispatcher);
|
|
|
-
|
|
|
- $composer->setDownloadManager($dm);
|
|
|
- $composer->setEventDispatcher($dispatcher);
|
|
|
+ if ($fullLoad) {
|
|
|
+ // initialize download manager
|
|
|
+ $dm = $this->createDownloadManager($io, $config, $dispatcher);
|
|
|
+ $composer->setDownloadManager($dm);
|
|
|
|
|
|
- // initialize autoload generator
|
|
|
- $generator = new AutoloadGenerator($dispatcher);
|
|
|
- $composer->setAutoloadGenerator($generator);
|
|
|
+ // initialize autoload generator
|
|
|
+ $generator = new AutoloadGenerator($dispatcher, $io);
|
|
|
+ $composer->setAutoloadGenerator($generator);
|
|
|
+ }
|
|
|
|
|
|
- // add installers to the manager
|
|
|
+ // add installers to the manager (must happen after download manager is created since they read it out of $composer)
|
|
|
$this->createDefaultInstallers($im, $composer, $io);
|
|
|
|
|
|
- $globalRepository = $this->createGlobalRepository($config, $vendorDir);
|
|
|
- $pm = $this->createPluginManager($composer, $io, $globalRepository);
|
|
|
- $composer->setPluginManager($pm);
|
|
|
+ if ($fullLoad) {
|
|
|
+ $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins);
|
|
|
+ $pm = $this->createPluginManager($io, $composer, $globalComposer);
|
|
|
+ $composer->setPluginManager($pm);
|
|
|
|
|
|
- if (!$disablePlugins) {
|
|
|
- $pm->loadInstalledPlugins();
|
|
|
- }
|
|
|
+ if (!$disablePlugins) {
|
|
|
+ $pm->loadInstalledPlugins();
|
|
|
+ }
|
|
|
|
|
|
- // purge packages if they have been deleted on the filesystem
|
|
|
- $this->purgePackages($rm, $im);
|
|
|
+ // once we have plugins and custom installers we can
|
|
|
+ // purge packages from local repos if they have been deleted on the filesystem
|
|
|
+ if ($rm->getLocalRepository()) {
|
|
|
+ $this->purgePackages($rm->getLocalRepository(), $im);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// init locker if possible
|
|
|
- if (isset($composerFile)) {
|
|
|
+ if ($fullLoad && isset($composerFile)) {
|
|
|
$lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION)
|
|
|
? substr($composerFile, 0, -4).'lock'
|
|
|
: $composerFile . '.lock';
|
|
|
- $locker = new Package\Locker($io, new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, $im, md5_file($composerFile));
|
|
|
+ $locker = new Package\Locker($io, new JsonFile($lockFile, new RemoteFilesystem($io, $config)), $rm, $im, md5_file($composerFile));
|
|
|
$composer->setLocker($locker);
|
|
|
}
|
|
|
|
|
@@ -313,22 +347,26 @@ class Factory
|
|
|
$rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed.json')));
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param Config $config
|
|
|
- * @param string $vendorDir
|
|
|
+ /**
|
|
|
+ * @param Config $config
|
|
|
+ * @return Composer|null
|
|
|
*/
|
|
|
- protected function createGlobalRepository(Config $config, $vendorDir)
|
|
|
+ protected function createGlobalComposer(IOInterface $io, Config $config, $disablePlugins)
|
|
|
{
|
|
|
- if ($config->get('home') == $vendorDir) {
|
|
|
- return null;
|
|
|
+ if (realpath($config->get('home')) === getcwd()) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- $path = $config->get('home').'/vendor/composer/installed.json';
|
|
|
- if (!file_exists($path)) {
|
|
|
- return null;
|
|
|
+ $composer = null;
|
|
|
+ try {
|
|
|
+ $composer = self::createComposer($io, $config->get('home') . '/composer.json', $disablePlugins, $config->get('home'), false);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ if ($io->isDebug()) {
|
|
|
+ $io->write('Failed to initialize global composer: '.$e->getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return new Repository\InstalledFilesystemRepository(new JsonFile($path));
|
|
|
+ return $composer;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -344,7 +382,7 @@ class Factory
|
|
|
$cache = new Cache($io, $config->get('cache-files-dir'), 'a-z0-9_./');
|
|
|
}
|
|
|
|
|
|
- $dm = new Downloader\DownloadManager();
|
|
|
+ $dm = new Downloader\DownloadManager($io);
|
|
|
switch ($config->get('preferred-install')) {
|
|
|
case 'dist':
|
|
|
$dm->setPreferDist(true);
|
|
@@ -365,6 +403,7 @@ class Factory
|
|
|
$dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $eventDispatcher, $cache));
|
|
|
$dm->setDownloader('rar', new Downloader\RarDownloader($io, $config, $eventDispatcher, $cache));
|
|
|
$dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $eventDispatcher, $cache));
|
|
|
+ $dm->setDownloader('gzip', new Downloader\GzipDownloader($io, $config, $eventDispatcher, $cache));
|
|
|
$dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $eventDispatcher, $cache));
|
|
|
$dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $eventDispatcher, $cache));
|
|
|
|
|
@@ -392,11 +431,14 @@ class Factory
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * @param IOInterface $io
|
|
|
+ * @param Composer $composer
|
|
|
+ * @param Composer $globalComposer
|
|
|
* @return Plugin\PluginManager
|
|
|
*/
|
|
|
- protected function createPluginManager(Composer $composer, IOInterface $io, RepositoryInterface $globalRepository = null)
|
|
|
+ protected function createPluginManager(IOInterface $io, Composer $composer, Composer $globalComposer = null)
|
|
|
{
|
|
|
- return new Plugin\PluginManager($composer, $io, $globalRepository);
|
|
|
+ return new Plugin\PluginManager($io, $composer, $globalComposer);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -421,12 +463,11 @@ class Factory
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param Repository\RepositoryManager $rm
|
|
|
- * @param Installer\InstallationManager $im
|
|
|
+ * @param WritableRepositoryInterface $repo repository to purge packages from
|
|
|
+ * @param Installer\InstallationManager $im manager to check whether packages are still installed
|
|
|
*/
|
|
|
- protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im)
|
|
|
+ protected function purgePackages(WritableRepositoryInterface $repo, Installer\InstallationManager $im)
|
|
|
{
|
|
|
- $repo = $rm->getLocalRepository();
|
|
|
foreach ($repo->getPackages() as $package) {
|
|
|
if (!$im->isPackageInstalled($repo, $package)) {
|
|
|
$repo->removePackage($package);
|
|
@@ -435,10 +476,10 @@ class Factory
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param IOInterface $io IO instance
|
|
|
- * @param mixed $config either a configuration array or a filename to read from, if null it will read from
|
|
|
- * the default filename
|
|
|
- * @param bool $disablePlugins Whether plugins should not be loaded
|
|
|
+ * @param IOInterface $io IO instance
|
|
|
+ * @param mixed $config either a configuration array or a filename to read from, if null it will read from
|
|
|
+ * the default filename
|
|
|
+ * @param bool $disablePlugins Whether plugins should not be loaded
|
|
|
* @return Composer
|
|
|
*/
|
|
|
public static function create(IOInterface $io, $config = null, $disablePlugins = false)
|