1
0
Fork 0

Add "use-parent-dir" config to configure prompt when no composer.json is present in current dir (#10307)

Fixes #10299

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
pull/10334/head
Tizian Schmidlin 2021-12-04 15:00:28 +01:00 committed by GitHub
parent 1cfdaf43f1
commit 4f789a5f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 3 deletions

View File

@ -63,6 +63,19 @@ optionally be a hash of patterns for more granular install preferences.
> configuration in global and package configurations the string notation > configuration in global and package configurations the string notation
> is translated to a `*` package pattern. > is translated to a `*` package pattern.
## use-parent-dir
When running Composer in a directory where there is no composer.json, if there
is one present in a directory above Composer will by default ask you whether
you want to use that directory's composer.json instead.
If you always want to answer yes to this prompt, you can set this config value
to `true`. To never be prompted set it to `false`. The default is `"prompt"`.
> **Note:** This config must be set in your global user-wide config for it
> to work. Use for example `php composer.phar config --global use-parent-dir true`
> to set it.
## store-auths ## store-auths
What to do after prompting for authentication, one of: `true` (always store), What to do after prompting for authentication, one of: `true` (always store),

View File

@ -296,6 +296,10 @@
"type": "boolean", "type": "boolean",
"description": "If true, the Composer autoloader will also look for classes in the PHP include path." "description": "If true, the Composer autoloader will also look for classes in the PHP include path."
}, },
"use-parent-dir": {
"type": ["string", "boolean"],
"description": "When running Composer in a directory where there is no composer.json, if there is one present in a directory above Composer will by default ask you whether you want to use that directory's composer.json instead. One of: true (always use parent if needed), false (never ask or use it) or \"prompt\" (ask every time), defaults to prompt."
},
"preferred-install": { "preferred-install": {
"type": ["string", "object"], "type": ["string", "object"],
"description": "The install method Composer will prefer to use, defaults to auto and can be any of source, dist, auto, or a hash of {\"pattern\": \"preference\"}." "description": "The install method Composer will prefer to use, defaults to auto and can be any of source, dist, auto, or a hash of {\"pattern\": \"preference\"}."

View File

@ -454,6 +454,18 @@ EOT
return 'php-only'; return 'php-only';
} }
return $val !== 'false' && (bool)$val;
},
),
'use-parent-dir' => array(
function ($val) {
return in_array($val, array('true', 'false', 'prompt'), true);
},
function ($val) {
if ('prompt' === $val) {
return 'prompt';
}
return $val !== 'false' && (bool) $val; return $val !== 'false' && (bool) $val;
}, },
), ),

View File

@ -33,6 +33,7 @@ class Config
public static $defaultConfig = array( public static $defaultConfig = array(
'process-timeout' => 300, 'process-timeout' => 300,
'use-include-path' => false, 'use-include-path' => false,
'use-parent-dir' => 'prompt',
'preferred-install' => 'dist', 'preferred-install' => 'dist',
'notify-on-install' => true, 'notify-on-install' => true,
'github-protocols' => array('https', 'ssh', 'git'), 'github-protocols' => array('https', 'ssh', 'git'),

View File

@ -172,14 +172,19 @@ class Application extends BaseApplication
} }
// prompt user for dir change if no composer.json is present in current dir // prompt user for dir change if no composer.json is present in current dir
if ($io->isInteractive() && !$newWorkDir && !in_array($commandName, array('', 'list', 'init', 'about', 'help', 'diagnose', 'self-update', 'global', 'create-project', 'outdated'), true) && !file_exists(Factory::getComposerFile())) { if ($io->isInteractive() && !$newWorkDir && !in_array($commandName, array('', 'list', 'init', 'about', 'help', 'diagnose', 'self-update', 'global', 'create-project', 'outdated'), true) && !file_exists(Factory::getComposerFile()) && ($useParentDirIfNoJsonAvailable = $this->getUseParentDirConfigValue()) !== false) {
$dir = dirname(getcwd()); $dir = dirname(getcwd());
$home = realpath(Platform::getEnv('HOME') ?: Platform::getEnv('USERPROFILE') ?: '/'); $home = realpath(Platform::getEnv('HOME') ?: Platform::getEnv('USERPROFILE') ?: '/');
// abort when we reach the home dir or top of the filesystem // abort when we reach the home dir or top of the filesystem
while (dirname($dir) !== $dir && $dir !== $home) { while (dirname($dir) !== $dir && $dir !== $home) {
if (file_exists($dir.'/'.Factory::getComposerFile())) { if (file_exists($dir.'/'.Factory::getComposerFile())) {
if ($io->askConfirmation('<info>No composer.json in current directory, do you want to use the one at '.$dir.'?</info> [<comment>Y,n</comment>]? ')) { if ($useParentDirIfNoJsonAvailable === true || $io->askConfirmation('<info>No composer.json in current directory, do you want to use the one at '.$dir.'?</info> [<comment>Y,n</comment>]? ')) {
if ($useParentDirIfNoJsonAvailable === true) {
$io->writeError('<info>No composer.json in current directory, changing working directory to '.$dir.'</info>');
} else {
$io->writeError('<info>Always want to use the parent dir? Use "composer config --global use-parent-dir true" to change the default.</info>');
}
$oldWorkingDir = getcwd(); $oldWorkingDir = getcwd();
chdir($dir); chdir($dir);
} }
@ -593,4 +598,14 @@ class Application extends BaseApplication
{ {
return $this->initialWorkingDirectory; return $this->initialWorkingDirectory;
} }
/**
* @return 'prompt'|bool
*/
private function getUseParentDirConfigValue()
{
$config = Factory::createConfig($this->io);
return $config->get('use-parent-dir');
}
} }