aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/command/base_command.rs
blob: c6b5550ba26d56e6a2bcebae61cc9fade682b237 (plain)
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! ref: composer/src/Composer/Command/BaseCommand.php
//! ref: composer/vendor/symfony/console/Command/Command.php

use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::symfony::console::Terminal;
use shirabe_external_packages::symfony::console::command::command::{
    Command, CommandData, SetDefinitionArg,
};
use shirabe_external_packages::symfony::console::helper::Table;
use shirabe_external_packages::symfony::console::helper::TableSeparator;
use shirabe_external_packages::symfony::console::input::InputInterface;
use shirabe_external_packages::symfony::console::output::OutputInterface;
use shirabe_php_shim::{
    AsAny, InvalidArgumentException, LogicException, PhpMixed, RuntimeException,
    UnexpectedValueException, count, explode, in_array, is_string, max,
};
use std::cell::RefCell;
use std::rc::Rc;

use crate::advisory::AuditConfig;
use crate::advisory::Auditor;
use crate::composer::PartialComposerHandle;
use crate::config::Config;
use crate::console::Application;
use crate::console::input::InputArgument;
use crate::console::input::InputDefinitionItem;
use crate::console::input::InputOption;
use crate::factory::Factory;
use crate::filter::platform_requirement_filter::PlatformRequirementFilterFactory;
use crate::filter::platform_requirement_filter::PlatformRequirementFilterInterface;
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::io::NullIO;
use crate::package::version::VersionParser;
use crate::plugin::PluginEvents;
use crate::plugin::PreCommandRunEvent;
use crate::util::Platform;

pub const SUCCESS: i64 = 0;
pub const FAILURE: i64 = 1;
pub const INVALID: i64 = 2;

/// The base-class state shared by all Composer commands: the embedded Symfony command
/// state plus the lazily-resolved Composer instance and IO.
#[derive(Debug)]
pub struct BaseCommandData {
    inner: CommandData,
    pub(crate) composer: Option<PartialComposerHandle>,
    pub(crate) io: Option<Rc<RefCell<dyn IOInterface>>>,
}

impl BaseCommandData {
    pub fn new(name: Option<String>) -> Self {
        BaseCommandData {
            inner: CommandData::new(name),
            composer: None,
            io: None,
        }
    }

    /// Mutable access to the embedded Symfony command state, used by the Composer-typed definition
    /// builders to forward to `CommandData`'s Symfony-typed entry points.
    pub(crate) fn command_data_mut(&mut self) -> &mut CommandData {
        &mut self.inner
    }
}

/// \Composer\Composer\Command\BaseCommand — the Composer additions on top of the Symfony
/// `Command` trait. The Symfony state methods are inherited from the [`Command`] supertrait;
/// only the Composer-specific behavior and the Composer-typed definition builders live here.
pub trait BaseCommand: Command {
    /// Mutable access to the embedded Symfony command state. Each command returns
    /// `self.base_command_data.command_data_mut()`; this lets the Composer-typed definition
    /// builders below forward to `CommandData`'s Symfony-typed entry points.
    fn command_data_mut(&mut self) -> &mut CommandData;

    /// Sets the definition from Composer-typed argument/option instances.
    fn set_definition(&mut self, definition: &[InputDefinitionItem]) -> &mut Self
    where
        Self: Sized,
    {
        let items = definition
            .iter()
            .map(|item| item.to_definition_item())
            .collect();
        self.command_data_mut()
            .set_definition(SetDefinitionArg::Array(items));
        self
    }

    fn add_argument(
        &mut self,
        name: &str,
        mode: Option<i64>,
        description: &str,
        default: PhpMixed,
    ) -> &mut Self
    where
        Self: Sized,
    {
        self.command_data_mut()
            .add_argument(name, mode, description, default)
            .expect("command argument definitions in configure() are statically valid");
        self
    }

    fn add_option(
        &mut self,
        name: &str,
        shortcut: Option<&str>,
        mode: Option<i64>,
        description: &str,
        default: PhpMixed,
    ) -> &mut Self
    where
        Self: Sized,
    {
        let shortcut = shortcut
            .map(|s| PhpMixed::from(s.to_string()))
            .unwrap_or(PhpMixed::Null);
        self.command_data_mut()
            .add_option(name, shortcut, mode, description, default)
            .expect("command option definitions in configure() are statically valid");
        self
    }

    /// Whether this command is the self-update command (disables plugins/scripts).
    fn is_self_update_command(&self) -> bool {
        false
    }

    /// Whether or not this command is meant to call another command.
    fn is_proxy_command(&self) -> bool {
        false
    }

    /// Retrieves the default Composer\Composer instance or throws
    fn require_composer(
        &mut self,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle>;

    /// Retrieves the default Composer\Composer instance or null
    fn try_composer(
        &mut self,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Option<PartialComposerHandle>;

    fn set_composer(&mut self, composer: PartialComposerHandle);

    /// Removes the cached composer instance
    fn reset_composer(&mut self) -> Result<()>;

    fn get_io(&mut self) -> Rc<RefCell<dyn IOInterface>>;

    fn set_io(&mut self, io: Rc<RefCell<dyn IOInterface>>);

    /// Calls {@see Factory::create()} with the given arguments, taking into account flags and default states for disabling scripts and plugins
    fn create_composer_instance(
        &self,
        input: Rc<RefCell<dyn InputInterface>>,
        io: Rc<RefCell<dyn IOInterface>>,
        config: Option<IndexMap<String, PhpMixed>>,
        disable_plugins: bool,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle>;

    /// Returns preferSource and preferDist values based on the configuration.
    fn get_preferred_install_options(
        &self,
        config: &Config,
        input: Rc<RefCell<dyn InputInterface>>,
        keep_vcs_requires_prefer_source: bool,
    ) -> Result<(bool, bool)>;

    fn get_platform_requirement_filter(
        &self,
        input: Rc<RefCell<dyn InputInterface>>,
    ) -> Result<Rc<dyn PlatformRequirementFilterInterface>>;

    /// @param array<string> $requirements
    ///
    /// @return array<string, string>
    fn format_requirements(&self, requirements: Vec<String>) -> Result<IndexMap<String, String>>;

    /// @param array<string> $requirements
    ///
    /// @return list<array{name: string, version?: string}>
    fn normalize_requirements(
        &self,
        requirements: Vec<String>,
    ) -> Result<Vec<IndexMap<String, String>>>;

    /// @param array<TableSeparator|mixed[]> $table
    fn render_table(&self, table: Vec<PhpMixed>, output: Rc<RefCell<dyn OutputInterface>>);

    fn get_terminal_width(&self) -> i64;

    /// @internal
    /// @param 'format'|'audit-format' $optName
    /// @return Auditor::FORMAT_*
    fn get_audit_format(
        &self,
        input: Rc<RefCell<dyn InputInterface>>,
        opt_name: &str,
    ) -> Result<String>;

    /// Creates an AuditConfig from the Config object, optionally overriding security blocking based on input options
    fn create_audit_config(
        &self,
        config: &mut Config,
        input: Rc<RefCell<dyn InputInterface>>,
    ) -> Result<AuditConfig>;
}

/// Forwards every Composer-specific `BaseCommand` method that no command overrides to an
/// embedded field. Each command invokes this once inside its `impl BaseCommand` block,
/// alongside its hand-written `command_data_mut` (and any overridden behavior hooks). The
/// single argument names the field to forward to (always `base_command_data`).
#[macro_export]
macro_rules! delegate_base_command_trait_impls_to_inner {
    ($field:ident) => {
        shirabe_external_packages::delegate_to_inner!($field, fn require_composer(&mut self, disable_plugins: Option<bool>, disable_scripts: Option<bool>) -> anyhow::Result<$crate::composer::PartialComposerHandle>);
        shirabe_external_packages::delegate_to_inner!($field, fn try_composer(&mut self, disable_plugins: Option<bool>, disable_scripts: Option<bool>) -> Option<$crate::composer::PartialComposerHandle>);
        shirabe_external_packages::delegate_to_inner!($field, fn set_composer(&mut self, composer: $crate::composer::PartialComposerHandle));
        shirabe_external_packages::delegate_to_inner!($field, fn reset_composer(&mut self) -> anyhow::Result<()>);
        shirabe_external_packages::delegate_to_inner!($field, fn get_io(&mut self) -> std::rc::Rc<std::cell::RefCell<dyn $crate::io::IOInterface>>);
        shirabe_external_packages::delegate_to_inner!($field, fn set_io(&mut self, io: std::rc::Rc<std::cell::RefCell<dyn $crate::io::IOInterface>>));
        shirabe_external_packages::delegate_to_inner!($field, fn create_composer_instance(&self, input: std::rc::Rc<std::cell::RefCell<dyn shirabe_external_packages::symfony::console::input::InputInterface>>, io: std::rc::Rc<std::cell::RefCell<dyn $crate::io::IOInterface>>, config: Option<indexmap::IndexMap<String, shirabe_php_shim::PhpMixed>>, disable_plugins: bool, disable_scripts: Option<bool>) -> anyhow::Result<$crate::composer::PartialComposerHandle>);
        shirabe_external_packages::delegate_to_inner!($field, fn get_preferred_install_options(&self, config: &$crate::config::Config, input: std::rc::Rc<std::cell::RefCell<dyn shirabe_external_packages::symfony::console::input::InputInterface>>, keep_vcs_requires_prefer_source: bool) -> anyhow::Result<(bool, bool)>);
        shirabe_external_packages::delegate_to_inner!($field, fn get_platform_requirement_filter(&self, input: std::rc::Rc<std::cell::RefCell<dyn shirabe_external_packages::symfony::console::input::InputInterface>>) -> anyhow::Result<std::rc::Rc<dyn $crate::filter::platform_requirement_filter::PlatformRequirementFilterInterface>>);
        shirabe_external_packages::delegate_to_inner!($field, fn format_requirements(&self, requirements: Vec<String>) -> anyhow::Result<indexmap::IndexMap<String, String>>);
        shirabe_external_packages::delegate_to_inner!($field, fn normalize_requirements(&self, requirements: Vec<String>) -> anyhow::Result<Vec<indexmap::IndexMap<String, String>>>);
        shirabe_external_packages::delegate_to_inner!($field, fn render_table(&self, table: Vec<shirabe_php_shim::PhpMixed>, output: std::rc::Rc<std::cell::RefCell<dyn shirabe_external_packages::symfony::console::output::OutputInterface>>));
        shirabe_external_packages::delegate_to_inner!($field, fn get_terminal_width(&self) -> i64);
        shirabe_external_packages::delegate_to_inner!($field, fn get_audit_format(&self, input: std::rc::Rc<std::cell::RefCell<dyn shirabe_external_packages::symfony::console::input::InputInterface>>, opt_name: &str) -> anyhow::Result<String>);
        shirabe_external_packages::delegate_to_inner!($field, fn create_audit_config(&self, config: &mut $crate::config::Config, input: std::rc::Rc<std::cell::RefCell<dyn shirabe_external_packages::symfony::console::input::InputInterface>>) -> anyhow::Result<$crate::advisory::AuditConfig>);
    };
}

impl Command for BaseCommandData {
    shirabe_external_packages::delegate_command_trait_impls_to_inner!(inner);
}

impl BaseCommand for BaseCommandData {
    fn command_data_mut(&mut self) -> &mut CommandData {
        &mut self.inner
    }

    fn require_composer(
        &mut self,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle> {
        if self.composer.is_none() {
            let application = self.get_application();
            let Some(application) = application else {
                return Err(RuntimeException {
                    message: "Could not create a Composer\\Composer instance, you must inject one if this command is not used with a Composer\\Console\\Application instance".to_string(),
                    code: 0,
                }
                .into());
            };
            let composer = {
                let mut app_ref = application.borrow_mut();
                let app_dyn: &mut dyn shirabe_external_packages::symfony::console::application::Application = &mut *app_ref;
                let app = app_dyn
                    .as_any_mut()
                    .downcast_mut::<Application>()
                    .expect("a Composer command's application is a shirabe Application");
                app.get_composer(true, disable_plugins, disable_scripts)?
            };
            self.composer = composer;
        }

        Ok(self
            .composer
            .clone()
            .expect("requireComposer always yields a Composer or errors"))
    }

    fn try_composer(
        &mut self,
        disable_plugins: Option<bool>,
        disable_scripts: Option<bool>,
    ) -> Option<PartialComposerHandle> {
        if self.composer.is_none()
            && let Some(application) = self.get_application()
        {
            let result = {
                let mut app_ref = application.borrow_mut();
                let app_dyn: &mut dyn shirabe_external_packages::symfony::console::application::Application = &mut *app_ref;
                let app = app_dyn
                    .as_any_mut()
                    .downcast_mut::<Application>()
                    .expect("a Composer command's application is a shirabe Application");
                app.get_composer(false, disable_plugins, disable_scripts)
            };
            if let Ok(composer) = result {
                self.composer = composer;
            }
        }

        self.composer.clone()
    }

    fn set_composer(&mut self, composer: PartialComposerHandle) {
        self.composer = Some(composer);
    }

    fn reset_composer(&mut self) -> Result<()> {
        self.composer = None;
        if let Some(application) = self.get_application() {
            let mut app_ref = application.borrow_mut();
            let app_dyn: &mut dyn shirabe_external_packages::symfony::console::application::Application = &mut *app_ref;
            let app = app_dyn
                .as_any_mut()
                .downcast_mut::<Application>()
                .expect("a Composer command's application is a shirabe Application");
            app.reset_composer();
        }
        Ok(())
    }

    fn get_io(&mut self) -> Rc<RefCell<dyn IOInterface>> {
        if self.io.is_none() {
            match self.get_application() {
                Some(application) => {
                    let io = {
                        let app_ref = application.borrow();
                        let app_dyn: &dyn shirabe_external_packages::symfony::console::application::Application = &*app_ref;
                        let app = app_dyn
                            .as_any()
                            .downcast_ref::<Application>()
                            .expect("a Composer command's application is a shirabe Application");
                        app.get_io()
                    };
                    self.io = Some(io);
                }
                None => {
                    self.io = Some(Rc::new(RefCell::new(NullIO::new())));
                }
            }
        }

        self.io.clone().unwrap()
    }

    fn set_io(&mut self, io: Rc<RefCell<dyn IOInterface>>) {
        self.io = Some(io);
    }

    fn create_composer_instance(
        &self,
        input: Rc<RefCell<dyn InputInterface>>,
        io: Rc<RefCell<dyn IOInterface>>,
        config: Option<IndexMap<String, PhpMixed>>,
        disable_plugins: bool,
        disable_scripts: Option<bool>,
    ) -> Result<PartialComposerHandle> {
        let disable_plugins = disable_plugins
            || input
                .borrow()
                .has_parameter_option(PhpMixed::from(vec!["--no-plugins"]), false);
        let disable_scripts = disable_scripts.unwrap_or(false)
            || input
                .borrow()
                .has_parameter_option(PhpMixed::from(vec!["--no-scripts"]), false);

        // PHP: if ($app instanceof Application && $app->getDisablePluginsByDefault()) $disablePlugins = true;
        //      (same for getDisableScriptsByDefault()).
        // TODO(phase-c): these application-default overrides need a shared Application handle
        // (deferred), so only the passed/flag values apply.
        let disable_plugins_kind = if disable_plugins {
            crate::factory::DisablePlugins::All
        } else {
            crate::factory::DisablePlugins::None
        };
        let config = config.map(crate::factory::LocalConfigInput::Data);
        Factory::create(io, config, disable_plugins_kind, disable_scripts).map(|c| c.upcast())
    }

    fn get_preferred_install_options(
        &self,
        config: &Config,
        input: Rc<RefCell<dyn InputInterface>>,
        keep_vcs_requires_prefer_source: bool,
    ) -> Result<(bool, bool)> {
        let mut prefer_source = false;
        let mut prefer_dist = false;

        match config.get("preferred-install").as_string().unwrap_or("") {
            "source" => {
                prefer_source = true;
            }
            "dist" => {
                prefer_dist = true;
            }
            _ => {
                // noop
            }
        }

        if !input.borrow().has_option("prefer-dist") || !input.borrow().has_option("prefer-source")
        {
            return Ok((prefer_source, prefer_dist));
        }

        if input.borrow().has_option("prefer-install")
            && is_string(&input.borrow().get_option("prefer-install")?)
        {
            if input
                .borrow()
                .get_option("prefer-source")?
                .as_bool()
                .unwrap_or(false)
            {
                return Err(InvalidArgumentException {
                    message: "--prefer-source can not be used together with --prefer-install"
                        .to_string(),
                    code: 0,
                }
                .into());
            }
            if input
                .borrow()
                .get_option("prefer-dist")?
                .as_bool()
                .unwrap_or(false)
            {
                return Err(InvalidArgumentException {
                    message: "--prefer-dist can not be used together with --prefer-install"
                        .to_string(),
                    code: 0,
                }
                .into());
            }
            let prefer_install = input.borrow().get_option("prefer-install")?;
            match prefer_install.as_string().unwrap_or("") {
                "dist" => {
                    input
                        .borrow_mut()
                        .set_option("prefer-dist", PhpMixed::Bool(true))?;
                }
                "source" => {
                    input
                        .borrow_mut()
                        .set_option("prefer-source", PhpMixed::Bool(true))?;
                }
                "auto" => {
                    prefer_dist = false;
                    prefer_source = false;
                }
                other => {
                    return Err(UnexpectedValueException {
                        message: format!(
                            "--prefer-install accepts one of \"dist\", \"source\" or \"auto\", got {}",
                            other
                        ),
                        code: 0,
                    }
                    .into());
                }
            }
        }

        if input
            .borrow()
            .get_option("prefer-source")?
            .as_bool()
            .unwrap_or(false)
            || input
                .borrow()
                .get_option("prefer-dist")?
                .as_bool()
                .unwrap_or(false)
            || (keep_vcs_requires_prefer_source
                && input.borrow().has_option("keep-vcs")
                && input
                    .borrow()
                    .get_option("keep-vcs")?
                    .as_bool()
                    .unwrap_or(false))
        {
            prefer_source = input
                .borrow()
                .get_option("prefer-source")?
                .as_bool()
                .unwrap_or(false)
                || (keep_vcs_requires_prefer_source
                    && input.borrow().has_option("keep-vcs")
                    && input
                        .borrow()
                        .get_option("keep-vcs")?
                        .as_bool()
                        .unwrap_or(false));
            prefer_dist = input
                .borrow()
                .get_option("prefer-dist")?
                .as_bool()
                .unwrap_or(false);
        }

        Ok((prefer_source, prefer_dist))
    }

    fn get_platform_requirement_filter(
        &self,
        input: Rc<RefCell<dyn InputInterface>>,
    ) -> Result<Rc<dyn PlatformRequirementFilterInterface>> {
        if !input.borrow().has_option("ignore-platform-reqs")
            || !input.borrow().has_option("ignore-platform-req")
        {
            return Err(LogicException {
                message:
                    "Calling getPlatformRequirementFilter from a command which does not define the --ignore-platform-req[s] flags is not permitted."
                        .to_string(),
                code: 0,
            }
            .into());
        }

        if input
            .borrow()
            .get_option("ignore-platform-reqs")?
            .as_bool()
            .unwrap_or(false)
        {
            return Ok(PlatformRequirementFilterFactory::ignore_all());
        }

        let ignores = input.borrow().get_option("ignore-platform-req")?;
        if count(&ignores) > 0 {
            return PlatformRequirementFilterFactory::from_bool_or_list(ignores);
        }

        Ok(PlatformRequirementFilterFactory::ignore_nothing())
    }

    fn format_requirements(&self, requirements: Vec<String>) -> Result<IndexMap<String, String>> {
        let mut requires: IndexMap<String, String> = IndexMap::new();
        let requirements = self.normalize_requirements(requirements)?;
        for requirement in requirements {
            if !requirement.contains_key("version") {
                return Err(UnexpectedValueException {
                    message: format!(
                        "Option {} is missing a version constraint, use e.g. {}:^1.0",
                        requirement.get("name").map(|s| s.as_str()).unwrap_or(""),
                        requirement.get("name").map(|s| s.as_str()).unwrap_or(""),
                    ),
                    code: 0,
                }
                .into());
            }
            requires.insert(
                requirement.get("name").cloned().unwrap_or_default(),
                requirement.get("version").cloned().unwrap_or_default(),
            );
        }

        Ok(requires)
    }

    fn normalize_requirements(
        &self,
        requirements: Vec<String>,
    ) -> Result<Vec<IndexMap<String, String>>> {
        let parser: VersionParser = VersionParser::new();

        parser.parse_name_version_pairs(requirements)
    }

    fn render_table(&self, table: Vec<PhpMixed>, output: Rc<RefCell<dyn OutputInterface>>) {
        let mut renderer = Table::new(output);
        renderer
            .set_style(PhpMixed::from("compact"))
            .expect("Table::set_style I/O cannot fail")
            .expect("'compact' is a built-in table style");
        renderer.set_rows(table).render();
        let _ = TableSeparator::new();
    }

    fn get_terminal_width(&self) -> i64 {
        let terminal = Terminal::new();
        let mut width = terminal.get_width();

        if Platform::is_windows() {
            width -= 1;
        } else {
            width = max(80, width);
        }

        width
    }

    fn get_audit_format(
        &self,
        input: Rc<RefCell<dyn InputInterface>>,
        opt_name: &str,
    ) -> Result<String> {
        if !input.borrow().has_option(opt_name) {
            return Err(LogicException {
                message: format!(
                    "This should not be called on a Command which has no {} option defined.",
                    opt_name
                ),
                code: 0,
            }
            .into());
        }

        let val = input.borrow().get_option(opt_name)?;
        let formats: Vec<Box<PhpMixed>> = Auditor::FORMATS
            .iter()
            .map(|s| Box::new(PhpMixed::String(s.to_string())))
            .collect();
        if !in_array(val.clone(), &PhpMixed::List(formats), true) {
            return Err(InvalidArgumentException {
                message: format!(
                    "--{} must be one of {}.",
                    opt_name,
                    Auditor::FORMATS.join(", ")
                ),
                code: 0,
            }
            .into());
        }

        Ok(val.as_string().unwrap_or("").to_string())
    }

    fn create_audit_config(
        &self,
        config: &mut Config,
        input: Rc<RefCell<dyn InputInterface>>,
    ) -> Result<AuditConfig> {
        // Handle both --audit and --no-audit flags
        let audit = if input.borrow().has_option("audit") {
            input
                .borrow()
                .get_option("audit")?
                .as_bool()
                .unwrap_or(false)
        } else {
            !(input.borrow().has_option("no-audit")
                && input
                    .borrow()
                    .get_option("no-audit")?
                    .as_bool()
                    .unwrap_or(false))
        };
        let audit_format = if input.borrow().has_option("audit-format") {
            self.get_audit_format(input.clone(), "audit-format")?
        } else {
            Auditor::FORMAT_SUMMARY.to_string()
        };

        let audit_config = AuditConfig::from_config(config, audit, &audit_format)?;

        if Platform::get_env("COMPOSER_NO_SECURITY_BLOCKING")
            .is_some_and(|s| !s.is_empty() && s != "0")
            || (input.borrow().has_option("no-security-blocking")
                && input
                    .borrow()
                    .get_option("no-security-blocking")?
                    .as_bool()
                    .unwrap_or(false))
        {
            let audit_config = AuditConfig::new(
                audit_config.audit,
                audit_config.audit_format,
                audit_config.audit_abandoned,
                false, // blockInsecure
                audit_config.block_abandoned,
                audit_config.ignore_unreachable,
                audit_config.ignore_list_for_audit,
                audit_config.ignore_list_for_blocking,
                audit_config.ignore_severity_for_audit,
                audit_config.ignore_severity_for_blocking,
                audit_config.ignore_abandoned_for_audit,
                audit_config.ignore_abandoned_for_blocking,
            );
            return Ok(audit_config);
        }

        Ok(audit_config)
    }
}

/// \Composer\Command\BaseCommand::initialize — runs for every Composer command after the
/// input is bound. Shared via a free function because Rust has no inheritance; each command's
/// `Command::initialize` forwards here so the leaf's `is_self_update_command()` override (and
/// future overrides) bind correctly.
pub fn base_command_initialize(
    cmd: &mut dyn BaseCommand,
    input: Rc<RefCell<dyn InputInterface>>,
    _output: Rc<RefCell<dyn OutputInterface>>,
) -> Result<()> {
    // initialize a plugin-enabled Composer instance, either local or global
    // PHP also ORs in $this->getApplication()->getDisablePluginsByDefault() /
    // getDisableScriptsByDefault().
    // TODO(phase-c): the application-default OR-terms need a shared Application handle
    // (deferred), so only the input flags are honoured here.
    let mut disable_plugins = input
        .borrow()
        .has_parameter_option(PhpMixed::from(vec!["--no-plugins"]), false);
    let mut disable_scripts = input
        .borrow()
        .has_parameter_option(PhpMixed::from(vec!["--no-scripts"]), false);

    if cmd.is_self_update_command() {
        disable_plugins = true;
        disable_scripts = true;
    }

    let composer = cmd.try_composer(Some(disable_plugins), Some(disable_scripts));
    let io = cmd.get_io();

    let disable_plugins_kind = if disable_plugins {
        crate::factory::DisablePlugins::All
    } else {
        crate::factory::DisablePlugins::None
    };
    let composer = if composer.is_none() {
        Factory::create_global(io.clone(), disable_plugins_kind, disable_scripts)
    } else {
        composer
    };
    if let Some(composer) = composer.as_ref() {
        let command_name = cmd.get_name().unwrap_or_default();
        let mut pre_command_run_event = PreCommandRunEvent::new(
            PluginEvents::PRE_COMMAND_RUN.to_string(),
            input.clone(),
            command_name,
        );
        let pre_command_run_event_name = pre_command_run_event.get_name().to_string();
        let dispatcher = composer.borrow_partial().get_event_dispatcher();
        dispatcher.borrow_mut().dispatch(
            Some(&pre_command_run_event_name),
            Some(&mut pre_command_run_event),
        )?;
    }

    if input
        .borrow()
        .has_parameter_option(PhpMixed::from(vec!["--no-ansi"]), false)
        && input.borrow().has_option("no-progress")
    {
        input
            .borrow_mut()
            .set_option("no-progress", PhpMixed::Bool(true));
    }

    let env_options: IndexMap<&str, Vec<&str>> = [
        ("COMPOSER_NO_AUDIT", vec!["no-audit"]),
        ("COMPOSER_NO_DEV", vec!["no-dev", "update-no-dev"]),
        ("COMPOSER_PREFER_STABLE", vec!["prefer-stable"]),
        ("COMPOSER_PREFER_LOWEST", vec!["prefer-lowest"]),
        ("COMPOSER_MINIMAL_CHANGES", vec!["minimal-changes"]),
        ("COMPOSER_WITH_DEPENDENCIES", vec!["with-dependencies"]),
        (
            "COMPOSER_WITH_ALL_DEPENDENCIES",
            vec!["with-all-dependencies"],
        ),
        (
            "COMPOSER_NO_SECURITY_BLOCKING",
            vec!["no-security-blocking"],
        ),
    ]
    .into_iter()
    .collect();
    for (env_name, option_names) in &env_options {
        for option_name in option_names {
            if input.borrow().has_option(option_name)
                && !input
                    .borrow()
                    .get_option(option_name)?
                    .as_bool()
                    .unwrap_or(false)
                && Platform::get_env(env_name).is_some_and(|s| !s.is_empty() && s != "0")
            {
                input
                    .borrow_mut()
                    .set_option(option_name, PhpMixed::Bool(true));
            }
        }
    }

    if input.borrow().has_option("ignore-platform-reqs")
        && !input
            .borrow()
            .get_option("ignore-platform-reqs")?
            .as_bool()
            .unwrap_or(false)
        && Platform::get_env("COMPOSER_IGNORE_PLATFORM_REQS")
            .is_some_and(|s| !s.is_empty() && s != "0")
    {
        input
            .borrow_mut()
            .set_option("ignore-platform-reqs", PhpMixed::Bool(true));

        io.write_error("<warning>COMPOSER_IGNORE_PLATFORM_REQS is set. You may experience unexpected errors.</warning>");
    }

    if input.borrow().has_option("ignore-platform-req")
        && (!input.borrow().has_option("ignore-platform-reqs")
            || !input
                .borrow()
                .get_option("ignore-platform-reqs")?
                .as_bool()
                .unwrap_or(false))
    {
        let ignore_platform_req_env = Platform::get_env("COMPOSER_IGNORE_PLATFORM_REQ");
        let ignore_str = ignore_platform_req_env.clone().unwrap_or_default();
        if 0 == count(&input.borrow().get_option("ignore-platform-req")?)
            && ignore_platform_req_env.is_some()
            && !ignore_str.is_empty()
        {
            input.borrow_mut().set_option(
                "ignore-platform-req",
                PhpMixed::List(
                    explode(",", &ignore_str)
                        .into_iter()
                        .map(|s| Box::new(PhpMixed::String(s)))
                        .collect(),
                ),
            );

            io.write_error(&format!(
                "<warning>COMPOSER_IGNORE_PLATFORM_REQ is set to ignore {}. You may experience unexpected errors.</warning>",
                ignore_str
            ));
        }
    }

    Ok(())
}