1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
use anyhow::anyhow;
use clap::Args;
use std::collections::BTreeMap;
use std::path::PathBuf;
#[derive(Args)]
pub struct ConfigArgs {
/// Setting key
pub setting_key: Option<String>,
/// Setting value(s)
pub setting_value: Vec<String>,
/// Apply to the global config file
#[arg(short, long)]
pub global: bool,
/// Open the config file in an editor
#[arg(short, long)]
pub editor: bool,
/// Affect auth config file
#[arg(short, long)]
pub auth: bool,
/// Unset the given setting key
#[arg(long)]
pub unset: bool,
/// List the current configuration variables
#[arg(short, long)]
pub list: bool,
/// Use a specific config file
#[arg(short, long)]
pub file: Option<String>,
/// Returns absolute paths when fetching *-dir config values
#[arg(long)]
pub absolute: bool,
/// JSON decode the setting value
#[arg(short, long)]
pub json: bool,
/// Merge the setting value with the current value
#[arg(short, long)]
pub merge: bool,
/// Append to existing array values
#[arg(long)]
pub append: bool,
/// Display the origin of a config setting
#[arg(long)]
pub source: bool,
}
// ─── ComposerConfig ───────────────────────────────────────────────────────────
/// Holds the effective configuration key-value pairs for a project.
/// Keys mirror Composer's `Config.php` defaults.
pub struct ComposerConfig {
pub values: BTreeMap<String, serde_json::Value>,
}
impl ComposerConfig {
/// Build a `ComposerConfig` starting from the built-in defaults.
pub fn defaults() -> Self {
let mut m: BTreeMap<String, serde_json::Value> = BTreeMap::new();
m.insert("process-timeout".to_string(), serde_json::json!(300));
m.insert("use-include-path".to_string(), serde_json::json!(false));
m.insert("preferred-install".to_string(), serde_json::json!("dist"));
m.insert("notify-on-install".to_string(), serde_json::json!(true));
m.insert(
"github-protocols".to_string(),
serde_json::json!(["https", "ssh", "git"]),
);
m.insert("vendor-dir".to_string(), serde_json::json!("vendor"));
m.insert(
"bin-dir".to_string(),
serde_json::json!("{$vendor-dir}/bin"),
);
m.insert("bin-compat".to_string(), serde_json::json!("auto"));
m.insert("cache-dir".to_string(), serde_json::json!("{$home}/cache"));
m.insert(
"cache-files-dir".to_string(),
serde_json::json!("{$cache-dir}/files"),
);
m.insert(
"cache-repo-dir".to_string(),
serde_json::json!("{$cache-dir}/repo"),
);
m.insert(
"cache-vcs-dir".to_string(),
serde_json::json!("{$cache-dir}/vcs"),
);
m.insert("cache-files-ttl".to_string(), serde_json::json!(15_552_000));
m.insert(
"cache-files-maxsize".to_string(),
serde_json::json!("300MiB"),
);
m.insert("cache-read-only".to_string(), serde_json::json!(false));
m.insert("prepend-autoloader".to_string(), serde_json::json!(true));
m.insert("autoloader-suffix".to_string(), serde_json::Value::Null);
m.insert("optimize-autoloader".to_string(), serde_json::json!(false));
m.insert("sort-packages".to_string(), serde_json::json!(false));
m.insert(
"classmap-authoritative".to_string(),
serde_json::json!(false),
);
m.insert("apcu-autoloader".to_string(), serde_json::json!(false));
m.insert("platform".to_string(), serde_json::json!({}));
m.insert("platform-check".to_string(), serde_json::json!("php-only"));
m.insert("lock".to_string(), serde_json::json!(true));
m.insert("discard-changes".to_string(), serde_json::json!(false));
m.insert("archive-format".to_string(), serde_json::json!("tar"));
m.insert("archive-dir".to_string(), serde_json::json!("."));
m.insert("htaccess-protect".to_string(), serde_json::json!(true));
m.insert("secure-http".to_string(), serde_json::json!(true));
m.insert("allow-plugins".to_string(), serde_json::json!({}));
Self { values: m }
}
/// Merge `overrides` on top of the current values.
pub fn merge(&mut self, overrides: &BTreeMap<String, serde_json::Value>) {
for (k, v) in overrides {
self.values.insert(k.clone(), v.clone());
}
}
/// Resolve `{$vendor-dir}`, `{$home}`, `{$cache-dir}` placeholders inside
/// string values. Only one pass is performed (no recursive expansion).
pub fn resolve_references(&mut self) {
// Snapshot the values we need for substitution before mutating.
let vendor_dir = self
.values
.get("vendor-dir")
.and_then(|v| v.as_str())
.unwrap_or("vendor")
.to_string();
let home = composer_home();
let cache_dir = self
.values
.get("cache-dir")
.and_then(|v| v.as_str())
.unwrap_or("{$home}/cache")
.replace("{$home}", &home);
let replacements: &[(&str, &str)] = &[
("{$vendor-dir}", &vendor_dir),
("{$home}", &home),
("{$cache-dir}", &cache_dir),
];
let keys: Vec<String> = self.values.keys().cloned().collect();
for key in keys {
if let Some(serde_json::Value::String(s)) = self.values.get(&key).cloned() {
let mut resolved = s.clone();
for (placeholder, replacement) in replacements {
resolved = resolved.replace(placeholder, replacement);
}
if resolved != s {
self.values.insert(key, serde_json::Value::String(resolved));
}
}
}
}
/// Return the effective value for a single key, or `None` if absent.
pub fn get(&self, key: &str) -> Option<&serde_json::Value> {
self.values.get(key)
}
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
/// Return the Composer home directory, respecting `COMPOSER_HOME` and
/// falling back to the platform default (`~/.config/composer` on Unix,
/// `%APPDATA%/Composer` on Windows).
fn composer_home() -> String {
if let Ok(home) = std::env::var("COMPOSER_HOME") {
return home;
}
// Platform-specific defaults
#[cfg(target_os = "windows")]
{
std::env::var("APPDATA")
.map(|p| format!("{p}/Composer"))
.unwrap_or_else(|_| "C:/ProgramData/ComposerSetup/bin".to_string())
}
#[cfg(not(target_os = "windows"))]
{
// Prefer XDG_CONFIG_HOME if set, otherwise fall back to ~/.config/composer
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
format!("{xdg}/composer")
} else {
std::env::var("HOME")
.map(|h| format!("{h}/.config/composer"))
.unwrap_or_else(|_| "/tmp/composer".to_string())
}
}
}
/// Load the `config` section from a JSON file (global `config.json` or local
/// `composer.json`). Returns an empty map when the file is absent or has no
/// `config` key.
fn load_config_section(
path: &std::path::Path,
) -> anyhow::Result<BTreeMap<String, serde_json::Value>> {
if !path.exists() {
return Ok(BTreeMap::new());
}
let content = std::fs::read_to_string(path)?;
let json: serde_json::Value = serde_json::from_str(&content)?;
match json.get("config") {
Some(serde_json::Value::Object(obj)) => {
Ok(obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
}
_ => Ok(BTreeMap::new()),
}
}
/// Build the working directory path, preferring `--working-dir` over `cwd`.
fn working_dir(cli: &super::Cli) -> anyhow::Result<PathBuf> {
match &cli.working_dir {
Some(d) => Ok(PathBuf::from(d)),
None => Ok(std::env::current_dir()?),
}
}
// ─── Value rendering ─────────────────────────────────────────────────────────
/// Render a `serde_json::Value` as a human-readable string suitable for
/// single-line display (matching Composer's behaviour).
fn render_value(v: &serde_json::Value) -> String {
match v {
serde_json::Value::Null => "NULL".to_string(),
serde_json::Value::Bool(b) => if *b { "true" } else { "false" }.to_string(),
serde_json::Value::Number(n) => n.to_string(),
serde_json::Value::String(s) => s.clone(),
serde_json::Value::Array(arr) => {
arr.iter().map(render_value).collect::<Vec<_>>().join(", ")
}
serde_json::Value::Object(obj) => {
if obj.is_empty() {
"{}".to_string()
} else {
serde_json::to_string(v).unwrap_or_else(|_| "{}".to_string())
}
}
}
}
// ─── execute() ───────────────────────────────────────────────────────────────
pub fn execute(args: &ConfigArgs, cli: &super::Cli) -> anyhow::Result<()> {
// Write-mode operations are not yet implemented.
let is_write = !args.setting_value.is_empty() || args.unset || args.editor;
if is_write {
anyhow::bail!("Write-mode config operations are not yet implemented");
}
// Build the effective config.
let mut config = ComposerConfig::defaults();
if args.global {
// Read from $COMPOSER_HOME/config.json
let global_config_path = PathBuf::from(composer_home()).join("config.json");
let overrides = load_config_section(&global_config_path)?;
config.merge(&overrides);
} else {
// Read from working_dir/composer.json (config section only).
let wd = working_dir(cli)?;
let composer_json = wd.join("composer.json");
let overrides = load_config_section(&composer_json)?;
config.merge(&overrides);
}
// Resolve {$placeholder} references in string values.
config.resolve_references();
if args.list {
// Print all key → value pairs.
for (key, value) in &config.values {
println!("[{}] {}", key, render_value(value));
}
return Ok(());
}
match &args.setting_key {
None => {
// No key and not --list: show a short usage hint (mirrors Composer).
eprintln!(
"{}",
crate::console::error(
"No command specified. Use --list to show all config values, \
or provide a setting key."
)
);
std::process::exit(1);
}
Some(key) => match config.get(key) {
Some(value) => {
println!("{}", render_value(value));
}
None => {
return Err(anyhow!("Setting \"{}\" does not exist.", key));
}
},
}
Ok(())
}
// ─── Tests ───────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
// ── defaults ───────────────────────────────────────────────────────────
#[test]
fn test_defaults_contain_expected_keys() {
let cfg = ComposerConfig::defaults();
let required_keys = [
"process-timeout",
"use-include-path",
"preferred-install",
"notify-on-install",
"github-protocols",
"vendor-dir",
"bin-dir",
"bin-compat",
"cache-dir",
"cache-files-dir",
"cache-repo-dir",
"cache-vcs-dir",
"cache-files-ttl",
"cache-files-maxsize",
"cache-read-only",
"prepend-autoloader",
"autoloader-suffix",
"optimize-autoloader",
"sort-packages",
"classmap-authoritative",
"apcu-autoloader",
"platform",
"platform-check",
"lock",
"discard-changes",
"archive-format",
"archive-dir",
"htaccess-protect",
"secure-http",
"allow-plugins",
];
for key in &required_keys {
assert!(cfg.values.contains_key(*key), "defaults missing key: {key}");
}
}
#[test]
fn test_defaults_values_correct() {
let cfg = ComposerConfig::defaults();
assert_eq!(cfg.values["process-timeout"], serde_json::json!(300));
assert_eq!(cfg.values["preferred-install"], serde_json::json!("dist"));
assert_eq!(cfg.values["vendor-dir"], serde_json::json!("vendor"));
assert_eq!(
cfg.values["github-protocols"],
serde_json::json!(["https", "ssh", "git"])
);
assert_eq!(cfg.values["secure-http"], serde_json::json!(true));
assert_eq!(cfg.values["lock"], serde_json::json!(true));
assert_eq!(cfg.values["autoloader-suffix"], serde_json::Value::Null);
}
// ── merge ──────────────────────────────────────────────────────────────
#[test]
fn test_merge_overrides_existing_key() {
let mut cfg = ComposerConfig::defaults();
let mut overrides = BTreeMap::new();
overrides.insert("vendor-dir".to_string(), serde_json::json!("packages"));
overrides.insert("sort-packages".to_string(), serde_json::json!(true));
cfg.merge(&overrides);
assert_eq!(cfg.values["vendor-dir"], serde_json::json!("packages"));
assert_eq!(cfg.values["sort-packages"], serde_json::json!(true));
}
#[test]
fn test_merge_adds_new_key() {
let mut cfg = ComposerConfig::defaults();
let mut overrides = BTreeMap::new();
overrides.insert("custom-key".to_string(), serde_json::json!("custom-value"));
cfg.merge(&overrides);
assert_eq!(cfg.values["custom-key"], serde_json::json!("custom-value"));
}
#[test]
fn test_merge_empty_overrides_leaves_defaults_intact() {
let mut cfg = ComposerConfig::defaults();
let original_vendor = cfg.values["vendor-dir"].clone();
cfg.merge(&BTreeMap::new());
assert_eq!(cfg.values["vendor-dir"], original_vendor);
}
// ── reference resolution ───────────────────────────────────────────────
#[test]
fn test_reference_resolution_bin_dir() {
let mut cfg = ComposerConfig::defaults();
// bin-dir default is "{$vendor-dir}/bin"; vendor-dir default is "vendor"
cfg.resolve_references();
assert_eq!(cfg.values["bin-dir"], serde_json::json!("vendor/bin"));
}
#[test]
fn test_reference_resolution_custom_vendor_dir() {
let mut cfg = ComposerConfig::defaults();
// Override vendor-dir before resolving
cfg.values
.insert("vendor-dir".to_string(), serde_json::json!("lib"));
cfg.resolve_references();
assert_eq!(cfg.values["bin-dir"], serde_json::json!("lib/bin"));
}
#[test]
fn test_reference_resolution_cache_dirs() {
let mut cfg = ComposerConfig::defaults();
// Inject a predictable home so the test is environment-independent.
cfg.values.insert(
"cache-dir".to_string(),
serde_json::json!("/home/user/.cache/composer"),
);
cfg.resolve_references();
assert_eq!(
cfg.values["cache-files-dir"],
serde_json::json!("/home/user/.cache/composer/files")
);
assert_eq!(
cfg.values["cache-repo-dir"],
serde_json::json!("/home/user/.cache/composer/repo")
);
assert_eq!(
cfg.values["cache-vcs-dir"],
serde_json::json!("/home/user/.cache/composer/vcs")
);
}
#[test]
fn test_reference_resolution_no_change_for_non_string() {
let mut cfg = ComposerConfig::defaults();
let before = cfg.values["process-timeout"].clone();
cfg.resolve_references();
// Numeric values should be untouched.
assert_eq!(cfg.values["process-timeout"], before);
}
// ── single key query ───────────────────────────────────────────────────
#[test]
fn test_get_existing_key() {
let cfg = ComposerConfig::defaults();
let value = cfg.get("vendor-dir");
assert!(value.is_some());
assert_eq!(value.unwrap(), &serde_json::json!("vendor"));
}
#[test]
fn test_get_nonexistent_key_returns_none() {
let cfg = ComposerConfig::defaults();
assert!(cfg.get("does-not-exist").is_none());
}
// ── render_value ───────────────────────────────────────────────────────
#[test]
fn test_render_value_string() {
assert_eq!(render_value(&serde_json::json!("hello")), "hello");
}
#[test]
fn test_render_value_bool() {
assert_eq!(render_value(&serde_json::json!(true)), "true");
assert_eq!(render_value(&serde_json::json!(false)), "false");
}
#[test]
fn test_render_value_number() {
assert_eq!(render_value(&serde_json::json!(300)), "300");
}
#[test]
fn test_render_value_null() {
assert_eq!(render_value(&serde_json::Value::Null), "NULL");
}
#[test]
fn test_render_value_array() {
let v = serde_json::json!(["https", "ssh", "git"]);
assert_eq!(render_value(&v), "https, ssh, git");
}
#[test]
fn test_render_value_empty_object() {
assert_eq!(render_value(&serde_json::json!({})), "{}");
}
// ── load_config_section ────────────────────────────────────────────────
#[test]
fn test_load_config_section_absent_file() {
let path = std::path::Path::new("/tmp/nonexistent_composer_abc123.json");
let result = load_config_section(path).unwrap();
assert!(result.is_empty());
}
#[test]
fn test_load_config_section_with_config_key() {
use std::io::Write;
use tempfile::NamedTempFile;
let mut f = NamedTempFile::new().unwrap();
write!(
f,
r#"{{"name":"test/pkg","config":{{"sort-packages":true,"vendor-dir":"packages"}}}}"#
)
.unwrap();
let result = load_config_section(f.path()).unwrap();
assert_eq!(result.get("sort-packages"), Some(&serde_json::json!(true)));
assert_eq!(
result.get("vendor-dir"),
Some(&serde_json::json!("packages"))
);
}
#[test]
fn test_load_config_section_missing_config_key() {
use std::io::Write;
use tempfile::NamedTempFile;
let mut f = NamedTempFile::new().unwrap();
write!(f, r#"{{"name":"test/pkg","require":{{}}}}"#).unwrap();
let result = load_config_section(f.path()).unwrap();
assert!(result.is_empty());
}
// ── full merge pipeline ────────────────────────────────────────────────
#[test]
fn test_full_pipeline_project_overrides_are_applied() {
use std::io::Write;
use tempfile::TempDir;
let dir = TempDir::new().unwrap();
let composer_json = dir.path().join("composer.json");
let mut f = std::fs::File::create(&composer_json).unwrap();
write!(
f,
r#"{{"name":"test/pkg","config":{{"vendor-dir":"custom_vendor","sort-packages":true}}}}"#
)
.unwrap();
let overrides = load_config_section(&composer_json).unwrap();
let mut cfg = ComposerConfig::defaults();
cfg.merge(&overrides);
cfg.resolve_references();
assert_eq!(cfg.values["vendor-dir"], serde_json::json!("custom_vendor"));
assert_eq!(cfg.values["sort-packages"], serde_json::json!(true));
// bin-dir should have resolved against the overridden vendor-dir
assert_eq!(
cfg.values["bin-dir"],
serde_json::json!("custom_vendor/bin")
);
}
}
|