aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart-autoload/src/dump.rs
blob: 103c683e51f4f176e1126841a59ec3371ba0b345 (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
//! `Composer\Autoload\AutoloadGenerator::dump` extension.
//!
//! [`mozart_core::composer::AutoloadGenerator`] is a state container in
//! `mozart-core`; the dumping algorithm itself sits here in
//! `mozart-autoload` because it pulls in the classmap scanner,
//! installed.json reader, and PHP-emission helpers. This module hangs
//! `dump()` off the generator via [`AutoloadGeneratorExt`] so callers
//! can still write `composer.autoload_generator().dump(...)`, matching
//! `$composer->getAutoloadGenerator()->dump(...)` in PHP.
//!
//! Bring [`AutoloadGeneratorExt`] into scope at the call site:
//!
//! ```ignore
//! use mozart_autoload::AutoloadGeneratorExt;
//! ```
//!
//! See `Composer\Autoload\AutoloadGenerator::dump()` (the ~500-line
//! implementation in `composer/src/Composer/Autoload/AutoloadGenerator.php`)
//! for the upstream semantics.

use std::collections::BTreeMap;
use std::path::PathBuf;

use mozart_core::composer::{
    AutoloadDumpOptions, AutoloadGenerator, InstallationManager, LocalRepository, Locker,
    PlatformRequirementFilter,
};
use mozart_core::config::Config;
use mozart_core::package::RawPackageData;

use crate::autoload::{AutoloadConfig, PlatformCheckMode, generate};

/// Mirror of `Composer\ClassMapGenerator\ClassMap` — the return value
/// of `AutoloadGenerator::dump`. PHP's class is a `Countable` carrying
/// the discovered class map plus PSR-violation and ambiguous-class
/// records; Mozart only models the slice that command handlers need to
/// branch on today (`count`, `has_psr_violations`, `has_ambiguous_classes`).
///
/// The `map` / `psr_violations` / `ambiguous_classes` fields are
/// currently populated from the existing [`generate`]'s coarse
/// summary — once `generate` is refactored to expose the full classmap
/// these fields will hold the real entries.
pub struct ClassMap {
    map: BTreeMap<String, String>,
    psr_violations: Vec<String>,
    ambiguous_classes: BTreeMap<String, Vec<String>>,
}

impl ClassMap {
    /// Mirror of `ClassMap::count`.
    pub fn count(&self) -> usize {
        self.map.len()
    }

    /// Mirror of `count($classMap->getPsrViolations()) > 0`. PHP returns
    /// the violation strings; commands typically only need the boolean.
    pub fn has_psr_violations(&self) -> bool {
        !self.psr_violations.is_empty()
    }

    /// Mirror of `count($classMap->getAmbiguousClasses($filter)) > 0`.
    /// `with_filter = true` applies PHP's default test/fixture/example
    /// path filter; `false` skips it (the `$duplicatesFilter = false`
    /// branch upstream).
    pub fn has_ambiguous_classes(&self, with_filter: bool) -> bool {
        if !with_filter {
            return !self.ambiguous_classes.is_empty();
        }
        let pattern = regex_filter_default();
        self.ambiguous_classes.values().any(|paths| {
            paths
                .iter()
                .any(|p| !pattern.is_match(&p.replace('\\', "/")))
        })
    }

    /// Read access to the underlying map (`getMap()` upstream).
    pub fn map(&self) -> &BTreeMap<String, String> {
        &self.map
    }

    /// Read access to the PSR-violation warnings.
    pub fn psr_violations(&self) -> &[String] {
        &self.psr_violations
    }

    /// Read access to the ambiguous-class records.
    pub fn ambiguous_classes(&self) -> &BTreeMap<String, Vec<String>> {
        &self.ambiguous_classes
    }
}

fn regex_filter_default() -> regex::Regex {
    use std::sync::OnceLock;
    static RE: OnceLock<regex::Regex> = OnceLock::new();
    RE.get_or_init(|| {
        // `{/(test|fixture|example|stub)s?/}i` from PHP's
        // ClassMap::getAmbiguousClasses default.
        regex::Regex::new(r"(?i)/(test|fixture|example|stub)s?/")
            .expect("default ambiguous filter compiles")
    })
    .clone()
}

/// Extension trait hanging `dump()` off
/// [`mozart_core::composer::AutoloadGenerator`]. Mirrors
/// `Composer\Autoload\AutoloadGenerator::dump()`.
///
/// Bring this trait into scope (`use mozart_autoload::AutoloadGeneratorExt;`)
/// to make the method visible.
///
/// Diverges from PHP in one place: the per-call toggles PHP fixes via
/// `setDryRun` / `setDevMode` / … on the generator are passed in here
/// as an [`AutoloadDumpOptions`] argument, because Mozart's
/// [`AutoloadGenerator`] is stateless.
pub trait AutoloadGeneratorExt {
    /// Mirror of `AutoloadGenerator::dump(Config $config,
    /// InstalledRepositoryInterface $localRepo, RootPackageInterface
    /// $rootPackage, InstallationManager $installationManager, string
    /// $targetDir, bool $scanPsrPackages = false, ?string $suffix = null,
    /// ?Locker $locker = null, bool $strictAmbiguous = false)`.
    ///
    /// Mozart-specific notes:
    /// - `options` carries the toggles PHP fixes via setters on the
    ///   generator (`setDryRun`, `setDevMode`, `setApcu`, …).
    /// - `target_dir` is currently unused (the underlying [`generate`]
    ///   always writes into `vendor_dir/composer`); the parameter is
    ///   kept on the signature so the call site mirrors PHP and we can
    ///   honour it once the writer is parameterised.
    /// - `local_repo` and `root_package` are accepted to mirror the
    ///   PHP signature, but [`generate`] currently re-reads them from
    ///   `installed.json` / `composer.json`. Refactoring to consume the
    ///   passed-in values lives in a follow-up.
    #[allow(clippy::too_many_arguments)]
    fn dump(
        &self,
        options: &AutoloadDumpOptions,
        config: &Config,
        local_repo: &LocalRepository,
        root_package: &RawPackageData,
        installation_manager: &InstallationManager,
        target_dir: &str,
        scan_psr_packages: bool,
        suffix: Option<&str>,
        locker: &Locker,
        strict_ambiguous: bool,
    ) -> anyhow::Result<ClassMap>;
}

impl AutoloadGeneratorExt for AutoloadGenerator {
    fn dump(
        &self,
        options: &AutoloadDumpOptions,
        config: &Config,
        _local_repo: &LocalRepository,
        _root_package: &RawPackageData,
        installation_manager: &InstallationManager,
        _target_dir: &str,
        scan_psr_packages: bool,
        suffix: Option<&str>,
        locker: &Locker,
        strict_ambiguous: bool,
    ) -> anyhow::Result<ClassMap> {
        // Mirrors PHP: classmap-authoritative implies PSR scanning so
        // every class gets a fixed map entry.
        let scan = scan_psr_packages || options.class_map_authoritative;

        // Mirrors PHP's `if (null === $this->devMode)` branch: read the
        // `dev` flag from `vendor/composer/installed.json` when no
        // explicit dev-mode has been set on the options.
        let dev_mode = match options.dev_mode {
            Some(m) => m,
            None => read_installed_dev_flag(installation_manager.vendor_dir()),
        };

        // Mirrors PHP's suffix resolution chain in `dump()`:
        // 1. explicit argument
        // 2. `Config::get('autoloader-suffix')`
        // 3. existing `vendor/autoload.php`'s `ComposerAutoloaderInit{X}`
        // 4. `composer.lock`'s `content-hash` (when locked)
        // 5. random hex
        let resolved_suffix = resolve_suffix(suffix, config, installation_manager, locker)?;

        // Mirrors PHP: `$basePath = realpath(getcwd())`. We don't have
        // an explicit project_dir on the generator, but `vendor_dir`'s
        // parent matches the project root for the common
        // `vendor-dir = "vendor"` layout. When the user points
        // `vendor-dir` outside the project we fall back to `.`.
        let project_dir = installation_manager
            .vendor_dir()
            .parent()
            .map(|p| p.to_path_buf())
            .unwrap_or_else(|| PathBuf::from("."));

        // Mirrors PHP's `$checkPlatform = $config->get('platform-check') !==
        // false && !($filter instanceof IgnoreAllPlatformRequirementFilter)`.
        let platform_check = if matches!(
            options.platform_requirement_filter,
            PlatformRequirementFilter::IgnoreAll
        ) {
            PlatformCheckMode::Disabled
        } else {
            platform_check_mode_from_config(&config.platform_check)
        };

        let cfg = AutoloadConfig {
            project_dir,
            vendor_dir: installation_manager.vendor_dir().to_path_buf(),
            dev_mode,
            suffix: resolved_suffix,
            classmap_authoritative: options.class_map_authoritative,
            optimize: scan,
            apcu: options.apcu,
            apcu_prefix: options.apcu_prefix.clone(),
            // `dump()` does not surface a `--strict-psr` option (that's
            // a separate command-line flag on `dump-autoload`); the
            // generator only reports violations via `ClassMap`.
            strict_psr: false,
            strict_ambiguous,
            platform_check,
            ignore_platform_reqs: matches!(
                options.platform_requirement_filter,
                PlatformRequirementFilter::IgnoreAll
            ),
        };

        if options.dry_run {
            // PHP's dry-run still scans and returns the classmap but
            // skips file writes. The current [`generate`] does not
            // expose a dry-run hook, so we return an empty ClassMap
            // for now and surface the limitation here rather than
            // silently writing files.
            return Ok(ClassMap {
                map: BTreeMap::new(),
                psr_violations: Vec::new(),
                ambiguous_classes: BTreeMap::new(),
            });
        }

        let result = generate(&cfg)?;

        // Mozart's `GenerateResult` only carries summary flags
        // (`class_count`, `has_psr_violations`, `has_ambiguous_classes`),
        // not the actual class-name / path entries that PHP's `ClassMap`
        // exposes. We project the summary onto a `ClassMap` shape so
        // command code that only branches on `count()` / `has_*()` works
        // today; refactoring `generate` to surface the full map is
        // tracked as follow-up work.
        let mut map = BTreeMap::new();
        for i in 0..result.class_count {
            map.insert(format!("__mozart_placeholder_{i}"), String::new());
        }
        let psr_violations = if result.has_psr_violations {
            vec![String::from(
                "PSR-0/4 violation detected (details not yet surfaced)",
            )]
        } else {
            Vec::new()
        };
        let mut ambiguous_classes = BTreeMap::new();
        if result.has_ambiguous_classes {
            ambiguous_classes.insert("__mozart_placeholder".to_string(), Vec::new());
        }

        Ok(ClassMap {
            map,
            psr_violations,
            ambiguous_classes,
        })
    }
}

fn read_installed_dev_flag(vendor_dir: &std::path::Path) -> bool {
    let path = vendor_dir.join("composer/installed.json");
    if !path.exists() {
        return false;
    }
    let Ok(content) = std::fs::read_to_string(&path) else {
        return false;
    };
    let Ok(value) = serde_json::from_str::<serde_json::Value>(&content) else {
        return false;
    };
    value.get("dev").and_then(|v| v.as_bool()).unwrap_or(false)
}

fn resolve_suffix(
    explicit: Option<&str>,
    config: &Config,
    installation_manager: &InstallationManager,
    locker: &Locker,
) -> anyhow::Result<String> {
    if let Some(s) = explicit
        && !s.is_empty()
    {
        return Ok(s.to_string());
    }
    if let Some(s) = config.autoloader_suffix.as_ref()
        && !s.is_empty()
    {
        return Ok(s.clone());
    }
    let vendor_path = installation_manager.vendor_dir();
    let autoload_path = vendor_path.join("autoload.php");
    if autoload_path.exists()
        && let Ok(content) = std::fs::read_to_string(&autoload_path)
        && let Some(start) = content.find("ComposerAutoloaderInit")
    {
        let rest = &content[start + "ComposerAutoloaderInit".len()..];
        if let Some(end) = rest.find("::") {
            let candidate = &rest[..end];
            if !candidate.is_empty() && candidate.chars().all(|c| c.is_ascii_hexdigit()) {
                return Ok(candidate.to_string());
            }
        }
    }
    if locker.is_locked()
        && let Some(data) = locker.lock_data()?
        && !data.content_hash.is_empty()
    {
        return Ok(data.content_hash);
    }
    // Fall back to MD5 of the current timestamp (mirrors PHP's
    // `bin2hex(random_bytes(16))` — both produce a 32-char hex token
    // that participates only in classloader naming).
    let ts = format!("{:?}", std::time::SystemTime::now());
    Ok(format!("{:x}", md5::compute(ts.as_bytes())))
}

fn platform_check_mode_from_config(platform_check: &serde_json::Value) -> PlatformCheckMode {
    match platform_check {
        serde_json::Value::Bool(false) => PlatformCheckMode::Disabled,
        serde_json::Value::Bool(true) => PlatformCheckMode::Full,
        serde_json::Value::String(s) if s == "php-only" => PlatformCheckMode::PhpOnly,
        // Anything else (including JSON null / unknown strings) falls
        // through to `Full` — the safe default that PHP also picks
        // when the value is truthy-but-not-`"php-only"`.
        _ => PlatformCheckMode::Full,
    }
}