From 9c109dfea15eeca4bda5ecddb177d0ca060c317b Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Fri, 13 Oct 2017 22:38:05 -0600 Subject: [PATCH 1/5] Add default config source to Config class --- src/Composer/Config.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index a7fca2988..aaad6574c 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -84,6 +84,7 @@ class Config private $configSource; /** @var ConfigSourceInterface */ private $authConfigSource; + private $defaultsConfigSource; private $useEnvironment; private $warnedHosts = array(); @@ -120,6 +121,16 @@ class Config return $this->authConfigSource; } + public function setDefaultsConfigSource(ConfigSourceInterface $source) + { + $this->defaultsConfigSource = $source; + } + + public function getDefaultsConfigSource() + { + return $this->defaultsConfigSource; + } + /** * Merges new config values with the existing ones (overriding) * From 6ce11697ecc83ce67af91f50134c542b6c294d6c Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Fri, 13 Oct 2017 22:38:35 -0600 Subject: [PATCH 2/5] Load default config in with other configs --- src/Composer/Factory.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 23eaaab09..fe5102b81 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -200,6 +200,16 @@ class Factory } $config->setAuthConfigSource(new JsonConfigSource($file, true)); + // load global auth file + $file = new JsonFile($config->get('home').'/defaults.json'); + if ($file->exists()) { + if ($io && $io->isDebug()) { + $io->writeError('Loading config file ' . $file->getPath()); + } + $config->merge(array('config' => $file->read())); + } + $config->setDefaultsConfigSource(new JsonConfigSource($file, true)); + // load COMPOSER_AUTH environment variable if set if ($composerAuthEnv = getenv('COMPOSER_AUTH')) { $authData = json_decode($composerAuthEnv, true); From 9ed0a2d35f8d341dbcf5cee3e9859140b33373ce Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Fri, 13 Oct 2017 22:39:08 -0600 Subject: [PATCH 3/5] Use default configs, where applicable, in init --- src/Composer/Command/InitCommand.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index aa7213060..0772e2de4 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -154,11 +154,12 @@ EOT $git = $this->getGitConfig(); $io = $this->getIO(); $formatter = $this->getHelperSet()->get('formatter'); - + $config = Factory::createConfig($io); + $defaults = $config->get('defaults'); + // initialize repos if configured $repositories = $input->getOption('repository'); if ($repositories) { - $config = Factory::createConfig($io); $repos = array(new PlatformRepository); foreach ($repositories as $repo) { $repos[] = RepositoryFactory::fromString($io, $config, $repo); @@ -191,7 +192,9 @@ EOT $name = basename($cwd); $name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name); $name = strtolower($name); - if (isset($git['github.user'])) { + if ( isset($defaults['vendor']) ) { + $name = $defaults['vendor'] . '/' . $name; + } elseif (isset($git['github.user'])) { $name = $git['github.user'] . '/' . $name; } elseif (!empty($_SERVER['USERNAME'])) { $name = $_SERVER['USERNAME'] . '/' . $name; @@ -240,7 +243,9 @@ EOT $input->setOption('description', $description); if (null === $author = $input->getOption('author')) { - if (isset($git['user.name']) && isset($git['user.email'])) { + if ( isset($defaults['author']) && isset($defaults['author']['name']) && isset($defaults['author']['email']) ) { + $author = sprintf('%s <%s>', $defaults['author']['name'], $defaults['author']['email']); + } elseif (isset($git['user.name']) && isset($git['user.email'])) { $author = sprintf('%s <%s>', $git['user.name'], $git['user.email']); } } @@ -291,7 +296,12 @@ EOT ); $input->setOption('type', $type); - $license = $input->getOption('license') ?: false; + if (null === $license = $input->getOption('license')) { + if ( isset($defaults['license']) ) { + $license = $defaults['license']; + } + } + $license = $io->ask( 'License ['.$license.']: ', $license From 8d6f8a6f421e45960d354136ee519fa4a0b368d6 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Mon, 13 Nov 2017 12:58:07 -0700 Subject: [PATCH 4/5] Reverse new config-file changes --- src/Composer/Config.php | 11 ----------- src/Composer/Factory.php | 10 ---------- 2 files changed, 21 deletions(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index aaad6574c..a7fca2988 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -84,7 +84,6 @@ class Config private $configSource; /** @var ConfigSourceInterface */ private $authConfigSource; - private $defaultsConfigSource; private $useEnvironment; private $warnedHosts = array(); @@ -121,16 +120,6 @@ class Config return $this->authConfigSource; } - public function setDefaultsConfigSource(ConfigSourceInterface $source) - { - $this->defaultsConfigSource = $source; - } - - public function getDefaultsConfigSource() - { - return $this->defaultsConfigSource; - } - /** * Merges new config values with the existing ones (overriding) * diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index fe5102b81..23eaaab09 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -200,16 +200,6 @@ class Factory } $config->setAuthConfigSource(new JsonConfigSource($file, true)); - // load global auth file - $file = new JsonFile($config->get('home').'/defaults.json'); - if ($file->exists()) { - if ($io && $io->isDebug()) { - $io->writeError('Loading config file ' . $file->getPath()); - } - $config->merge(array('config' => $file->read())); - } - $config->setDefaultsConfigSource(new JsonConfigSource($file, true)); - // load COMPOSER_AUTH environment variable if set if ($composerAuthEnv = getenv('COMPOSER_AUTH')) { $authData = json_decode($composerAuthEnv, true); From a59f7399bf2e30132169b9efde94d2b7a4a39aa4 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Mon, 13 Nov 2017 13:14:09 -0700 Subject: [PATCH 5/5] Use environment variables to define default values instead --- src/Composer/Command/InitCommand.php | 31 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 0772e2de4..fe921e965 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -154,12 +154,11 @@ EOT $git = $this->getGitConfig(); $io = $this->getIO(); $formatter = $this->getHelperSet()->get('formatter'); - $config = Factory::createConfig($io); - $defaults = $config->get('defaults'); - + // initialize repos if configured $repositories = $input->getOption('repository'); if ($repositories) { + $config = Factory::createConfig($io); $repos = array(new PlatformRepository); foreach ($repositories as $repo) { $repos[] = RepositoryFactory::fromString($io, $config, $repo); @@ -192,8 +191,8 @@ EOT $name = basename($cwd); $name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name); $name = strtolower($name); - if ( isset($defaults['vendor']) ) { - $name = $defaults['vendor'] . '/' . $name; + if ( !empty($_SERVER['COMPOSER_DEFAULT_VENDOR']) ) { + $name = $_SERVER['COMPOSER_DEFAULT_VENDOR'] . '/' . $name; } elseif (isset($git['github.user'])) { $name = $git['github.user'] . '/' . $name; } elseif (!empty($_SERVER['USERNAME'])) { @@ -243,10 +242,20 @@ EOT $input->setOption('description', $description); if (null === $author = $input->getOption('author')) { - if ( isset($defaults['author']) && isset($defaults['author']['name']) && isset($defaults['author']['email']) ) { - $author = sprintf('%s <%s>', $defaults['author']['name'], $defaults['author']['email']); - } elseif (isset($git['user.name']) && isset($git['user.email'])) { - $author = sprintf('%s <%s>', $git['user.name'], $git['user.email']); + if ( !empty($_SERVER['COMPOSER_DEFAULT_AUTHOR']) ) { + $author_name = $_SERVER['COMPOSER_DEFAULT_AUTHOR']; + } elseif ( isset($git['user.name']) ) { + $author_name = $git['user.name']; + } + + if ( !empty($_SERVER['COMPOSER_DEFAULT_EMAIL']) ) { + $author_email = $_SERVER['COMPOSER_DEFAULT_EMAIL']; + } elseif ( isset($git['user.email']) ) { + $author_email = $git['user.email']; + } + + if (isset($author_name) && isset($author_email)) { + $author = sprintf('%s <%s>', $author_name, $author_email); } } @@ -297,8 +306,8 @@ EOT $input->setOption('type', $type); if (null === $license = $input->getOption('license')) { - if ( isset($defaults['license']) ) { - $license = $defaults['license']; + if ( !empty($_SERVER['COMPOSER_DEFAULT_LICENSE']) ) { + $license = $_SERVER['COMPOSER_DEFAULT_LICENSE']; } }