vivacity 0.26.3

Fast, Composer-compatible installer for PHP projects: install, update, require, remove, dump-autoload
Documentation
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
//! `update`, `require` and `remove` with `--no-install`: the end of
//! `Installer::run` when nothing is installed (docs/plans/no-install-listeners.md).
//!
//! `Installer::run` with `setInstall(false)` skips the dump
//! (Installer.php:305-307) and returns from `doUpdate` once the lock is
//! written (:684-689), but it still prints the funding count (:412-436) and
//! dispatches `POST_UPDATE_CMD` (:438-441) — `--no-scripts` only silences
//! composer.json's scripts (EventDispatcher.php:585); `--dry-run` turns it
//! off (:292-298). The listeners are those of the plugins
//! `Factory::createComposer` loaded from installed.json
//! (`loadInstalledPlugins`, Factory.php:448), and every emulated one reads
//! `getLocalRepository()` — installed.json as it was, purged of the
//! packages whose directory is gone (`purgePackages`, :457), dev packages
//! included whatever `--no-dev` says, without the lock's inline aliases
//! (only an install re-adds them), each branch alias loaded before its
//! package (`ArrayRepository::addPackage`, ArrayRepository.php:215-231).
//! A plugin the command adds is not loaded; one it removes still is.

use super::*;

/// The local repository `POST_UPDATE_CMD`'s listeners read without an
/// install, and the order their plugins were activated in.
pub(crate) struct LocalRepository {
    /// `loadInstalledPlugins`: installed.json as written (project, then
    /// `COMPOSER_HOME`), sorted by `PackageSorter` — no transaction follows.
    pub(crate) listeners: Vec<String>,
    /// installed.json purged, as a lock whose `packages` hold everything.
    pub(crate) lock: vivacity_core::lock::Lock,
    /// The same entries, as read.
    pub(crate) entries: Vec<serde_json::Value>,
}

pub(crate) fn local_repository(
    project: &std::path::Path,
    manifest: &serde_json::Value,
    plugins_enabled: bool,
) -> LocalRepository {
    let composer_dir = composer_dir_of(project, manifest);
    let raw = plugin_order::installed_json_packages(&composer_dir);
    let listeners = if plugins_enabled {
        plugin_order::loaded_plugins(
            &raw,
            &composer_dir,
            plugin_order::global_composer_dir().as_deref(),
        )
    } else {
        Vec::new()
    };
    let entries = purge_missing(&composer_dir, raw);
    let lock = vivacity_core::lock::Lock::from_value(
        &serde_json::json!({"packages": entries, "packages-dev": []}),
    );
    LocalRepository {
        listeners,
        lock,
        entries,
    }
}

fn allowed(manifest: &serde_json::Value, plugin: &str) -> bool {
    matches!(
        vivacity_core::layout::plugin_allowed(manifest, plugin),
        vivacity_core::layout::PluginVerdict::Allowed
    )
}

/// The install paths of the local repository, as the listeners ask
/// `InstallationManager::getInstallPath` for them (the current manifest's
/// installers and directories).
fn local_layout(
    project: &std::path::Path,
    repo: &LocalRepository,
    manifest: &serde_json::Value,
) -> Result<vivacity_core::layout::Layout, Vec<String>> {
    vivacity_core::layout::Layout::resolve(project, &repo.lock, manifest, true, true)
}

/// `plugin`'s listener runs: Composer loaded the plugin from installed.json
/// (`repo.listeners`) and `allow-plugins` allows it. The one rule of the
/// layout check before resolving and of the listeners themselves.
fn listening(repo: &LocalRepository, manifest: &serde_json::Value, plugin: &str) -> bool {
    repo.listeners.iter().any(|p| p == plugin) && allowed(manifest, plugin)
}

/// Decided before resolving (`resolution_fallback`): a loaded, allowed
/// listener that needs install paths (dealerdirect, the extension
/// installers) on a local repository whose layout vivacity cannot compute
/// goes to Composer, named.
pub(crate) fn layout_issue(
    project: &std::path::Path,
    manifest: &serde_json::Value,
) -> Option<vivacity_core::scope::ScopeIssue> {
    let repo = local_repository(project, manifest, true);
    if !vivacity_core::scope::POST_UPDATE_FILE_LISTENERS
        .iter()
        .any(|p| listening(&repo, manifest, p))
    {
        return None;
    }
    local_layout(project, &repo, manifest).err().map(|issues| {
        vivacity_core::scope::ScopeIssue::Layout(format!(
            "the install paths the post-update-cmd listeners read cannot be computed: {}",
            issues.join("; ")
        ))
    })
}

/// php-http/discovery loaded and allowed: its `postUpdate`
/// (Plugin.php:145-240 at 1.20.0) asks the local repository — not the new
/// lock — for an implementation of every abstraction it looks after, and
/// may then move a package to `require`, add one, edit composer.json and
/// run a cloned `Installer` again; measured, with the implementation absent
/// from the local repository, and with a `project` root whose only
/// implementation is a dev package: never converging under `--no-install`.
/// When it would act ([`crate::discovery::post_update_issue`]) the command
/// goes to Composer. Decided on the manifest the command resolves
/// (`require`'s edit included), before the lock is written.
pub(crate) fn discovery_issue(
    project: &std::path::Path,
    manifest: &serde_json::Value,
) -> Option<vivacity_core::scope::ScopeIssue> {
    use crate::discovery::DISCOVERY;
    let repo = local_repository(project, manifest, true);
    if !repo.listeners.iter().any(|p| p == DISCOVERY) || !allowed(manifest, DISCOVERY) {
        return None;
    }
    let version = repo
        .lock
        .packages
        .iter()
        .find(|p| p.name() == DISCOVERY)
        .map(|p| p.version().to_owned());
    let packages: Vec<crate::discovery::RepoPackage> = repo
        .entries
        .iter()
        .flat_map(crate::discovery::RepoPackage::from_entry)
        .collect();
    // `InstalledFilesystemRepository::getDevPackageNames`: installed.json's
    // `dev-package-names`.
    let dev_names =
        vivacity_core::jsonfile::read(&composer_dir_of(project, manifest).join("installed.json"))
            .and_then(|v| v.get("dev-package-names").cloned())
            .and_then(|v| serde_json::from_value::<Vec<String>>(v).ok())
            .unwrap_or_default()
            .into_iter()
            .map(|n| n.to_ascii_lowercase())
            .collect();
    crate::discovery::post_update_issue(
        version.as_deref(),
        &packages,
        &dev_names,
        manifest,
        || vivacity_core::scope::discovery_watches(manifest, &repo.entries),
        "not emulated without an install",
    )
}

/// Decided before resolving, for a command that dispatches
/// `POST_UPDATE_CMD` without an install (`resolution_fallback`):
///
/// - an emulated `POST_UPDATE_CMD` listener loaded from `COMPOSER_HOME`
///   (`PluginManager::loadInstalledPlugins` loads the global repository
///   too, PluginManager.php:109-111): vivacity runs the project's
///   plugins only;
/// - an active plugin that is neither emulated nor benign (what the
///   install side hands over: composer-patches removes the packages to
///   re-patch at `PRE_UPDATE_CMD`, an installers extender moves the
///   install paths the listeners write) — inert for the lock, not for
///   the run;
/// - a local repository whose install paths vivacity cannot compute when
///   a listener that writes them is loaded.
pub(crate) fn issues_before_resolution(
    project: &std::path::Path,
    manifest: &serde_json::Value,
    with_dev: bool,
) -> Vec<vivacity_core::scope::ScopeIssue> {
    use vivacity_core::scope::{ScopeIssue, BENIGN_PLUGINS, EMULATED_PLUGINS, RESOLUTION_INERT};
    let mut issues = Vec::new();
    let listeners = [
        vivacity_core::phpcs_installer::PLUGIN_NAME,
        extension_installers::PHPSTAN,
        extension_installers::RECTOR,
        extension_installers::INFECTION,
        vivacity_core::c3_plugin::PLUGIN_NAME,
    ];
    if let Some(global) = plugin_order::global_composer_dir() {
        let packages = plugin_order::installed_json_packages(&global);
        for p in &packages {
            let Some(name) = p.get("name").and_then(serde_json::Value::as_str) else {
                continue;
            };
            if listeners.contains(&name)
                && p.get("type").and_then(serde_json::Value::as_str) == Some("composer-plugin")
            {
                issues.push(ScopeIssue::ResolutionPlugin(
                    name.to_owned(),
                    "is installed globally: its post-update-cmd listener runs on this project (not emulated)".to_owned(),
                ));
            }
        }
    }
    // (A plugin outside every list is already handed over by
    // `resolution_issues`: only the resolution-inert ones are left.)
    for name in vivacity_core::scope::active_plugins(project, manifest, true) {
        if RESOLUTION_INERT.contains(&name.as_str())
            && !EMULATED_PLUGINS.contains(&name.as_str())
            && !BENIGN_PLUGINS.contains(&name.as_str())
        {
            issues.push(ScopeIssue::ResolutionPlugin(
                name,
                "runs listeners during an update without install that vivacity does not emulate"
                    .to_owned(),
            ));
        }
    }
    issues.extend(layout_issue(project, manifest));
    // The second round of emulated plugins (docs/plans/plugins-round2.md):
    // dg/composer-cleaner, liborm85/composer-vendor-cleaner and a
    // configured laminas-skeleton-installer act at `POST_UPDATE_CMD`.
    let installed = vivacity_core::layout::installed_packages_of(project, manifest);
    let global = crate::effect_plugins::global_packages();
    let ctx = vivacity_core::effect_plugins::Context {
        project,
        manifest,
        local: installed
            .iter()
            .filter_map(serde_json::Value::as_object)
            .collect(),
        installed: &installed,
        dev_mode: with_dev,
        command: vivacity_core::effect_plugins::Command::UpdateNoInstall,
        interactive: false,
        no_autoloader: false,
        plugins_enabled: true,
        global: &global,
        aliases: false,
        // No dump without an install: drupol/composer-packages is inert.
        lock_data: None,
    };
    issues.extend(
        vivacity_core::effect_plugins::issues(&ctx)
            .into_iter()
            .map(ScopeIssue::Layout),
    );
    issues
}

/// yiisoft/yii2-composer's `Plugin::activate` (Plugin.php:46-56) creates
/// `<vendor-dir>/yiisoft/extensions.php` when it is missing, on every
/// command that loads it — without an install, and on a dry run too. The
/// plugin loaded from installed.json and allowed.
pub(crate) fn activate_yii2_composer(
    project: &std::path::Path,
    manifest: &serde_json::Value,
    plugins_enabled: bool,
) -> anyhow::Result<()> {
    use vivacity_core::yii2_composer as yii;
    let repo = local_repository(project, manifest, plugins_enabled);
    if repo.listeners.iter().any(|p| p == yii::PLUGIN_NAME) && allowed(manifest, yii::PLUGIN_NAME) {
        let vendor = vivacity_core::dirs::Dirs::resolve(manifest)
            .unwrap_or_default()
            .vendor_dir(project);
        yii::ensure_extensions_file(&vendor)?;
    }
    Ok(())
}

/// `POST_UPDATE_CMD` without an install: the emulated listeners, in their
/// plugins' activation order, on the local repository. `manifest` is the
/// root package the listeners read (`extra`, `allow-plugins` as loaded);
/// `flex` prints symfony/flex's output at its rank (already decided by the
/// resolution, `print_flex_post_update`).
pub(crate) fn post_update_listeners(
    project: &std::path::Path,
    manifest: &serde_json::Value,
    plugins_enabled: bool,
    flex: Option<&dyn Fn() -> anyhow::Result<()>>,
) -> anyhow::Result<()> {
    let repo = local_repository(project, manifest, plugins_enabled);
    let emulated = [
        vivacity_core::phpcs_installer::PLUGIN_NAME,
        extension_installers::PHPSTAN,
        extension_installers::RECTOR,
        extension_installers::INFECTION,
        vivacity_core::c3_plugin::PLUGIN_NAME,
        "symfony/flex",
    ];
    let mut layout: Option<vivacity_core::layout::Layout> = None;
    let mut flex_printed = false;
    // Every plugin Composer loaded, in activation order: under -v its
    // listener's line (`EventDispatcher::doDispatch`, written right before
    // the call), then what the emulated listener does — the two
    // interleaved, as Composer writes them.
    for plugin in repo.listeners.iter().map(String::as_str) {
        crate::verbose_listeners::announce(
            plugin,
            crate::verbose_listeners::POST_UPDATE_CMD,
            manifest,
        );
        if !emulated.contains(&plugin) {
            continue;
        }
        if plugin == "symfony/flex" {
            if let Some(print) = flex {
                print()?;
                flex_printed = true;
            }
            continue;
        }
        if !listening(&repo, manifest, plugin) {
            continue;
        }
        if plugin == vivacity_core::c3_plugin::PLUGIN_NAME {
            // `askForUpdateV2`: the installed c3's own c3.php.
            emulate_c3(project, &repo.lock, manifest, true, true, plugins_enabled)?;
            continue;
        }
        if layout.is_none() {
            // Refused before resolving when it cannot be computed
            // (`layout_issue`).
            layout = Some(local_layout(project, &repo, manifest).map_err(|issues| {
                anyhow::anyhow!(
                    "internal: the local repository's layout was not checked: {}",
                    issues.join("; ")
                )
            })?);
        }
        let Some(layout) = layout.as_ref() else {
            continue;
        };
        match plugin {
            vivacity_core::phpcs_installer::PLUGIN_NAME => {
                emulate_phpcs(project, layout, &repo.lock, manifest, true)?;
            }
            extension_installers::PHPSTAN => {
                extension_installers::phpstan(
                    layout,
                    &repo.lock,
                    &[],
                    manifest,
                    true,
                    plugins_enabled,
                )?;
            }
            extension_installers::RECTOR => {
                extension_installers::rector(layout, &repo.lock, manifest, true, plugins_enabled)?;
            }
            extension_installers::INFECTION => {
                // The local repository as loaded: installed.json's order.
                let order = installed_order(project, manifest);
                let order: Vec<&str> = order.iter().map(String::as_str).collect();
                extension_installers::infection(
                    layout,
                    &repo.lock,
                    &order,
                    manifest,
                    true,
                    plugins_enabled,
                )?;
            }
            _ => {}
        }
    }
    // symfony/flex active for the resolution but not loadable from
    // installed.json (its directory gone): printed where it always was.
    if let (Some(print), false) = (flex, flex_printed) {
        print()?;
    }
    Ok(())
}

/// The names of installed.json, in its order.
fn installed_order(project: &std::path::Path, manifest: &serde_json::Value) -> Vec<String> {
    plugin_order::installed_json_packages(&composer_dir_of(project, manifest))
        .iter()
        .filter_map(|p| p.get("name").and_then(serde_json::Value::as_str))
        .map(str::to_owned)
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    /// A project whose installed.json lists `packages`, each laid out
    /// under vendor/ unless its name is in `gone`.
    fn project(packages: serde_json::Value, gone: &[&str]) -> tempfile::TempDir {
        let tmp = tempfile::tempdir().expect("tmp");
        let composer = tmp.path().join("vendor/composer");
        std::fs::create_dir_all(&composer).expect("mkdir");
        let mut list = Vec::new();
        for p in packages.as_array().expect("array") {
            let name = p["name"].as_str().expect("name");
            if !gone.contains(&name) {
                std::fs::create_dir_all(tmp.path().join("vendor").join(name)).expect("pkg");
            }
            let mut p = p.clone();
            p["install-path"] = json!(format!("../{name}"));
            list.push(p);
        }
        std::fs::write(
            composer.join("installed.json"),
            json!({"packages": list, "dev": true, "dev-package-names": []}).to_string(),
        )
        .expect("installed.json");
        tmp
    }

    #[test]
    fn the_local_repository_is_installed_json_purged() {
        let tmp = project(
            json!([
                {"name": "phpstan/extension-installer", "version": "1.4.3", "type": "composer-plugin"},
                {"name": "a/here", "version": "1.0.0"},
                {"name": "a/gone", "version": "1.0.0"},
            ]),
            &["a/gone"],
        );
        let manifest = json!({"config": {"allow-plugins": {"phpstan/extension-installer": true}}});
        let repo = local_repository(tmp.path(), &manifest, true);
        let names: Vec<&str> = repo.lock.packages.iter().map(|p| p.name()).collect();
        assert_eq!(names, ["phpstan/extension-installer", "a/here"]);
        assert!(repo
            .listeners
            .iter()
            .any(|p| p == "phpstan/extension-installer"));
        // `--no-plugins`: nothing loaded, the repository unchanged.
        assert!(local_repository(tmp.path(), &manifest, false)
            .listeners
            .is_empty());
    }

    #[test]
    fn discovery_hands_over_when_it_looks_after_an_abstraction() {
        let allowed = json!({"config": {"allow-plugins": {"php-http/discovery": true}}});
        let plugin = |version: &str| json!({"name": "php-http/discovery", "version": version, "type": "composer-plugin"});
        let sdk = json!({"name": "acme/sdk", "version": "1.0.0",
            "require": {"php-http/discovery": "^1.15", "psr/http-client-implementation": "*"}});
        // Something to look after: handed over.
        let watched = project(json!([plugin("1.20.0"), sdk.clone()]), &[]);
        assert!(discovery_issue(watched.path(), &allowed,).is_some());
        // Not allowed: never loaded.
        assert!(discovery_issue(
            watched.path(),
            &json!({"config": {"allow-plugins": {"php-http/discovery": false}}})
        )
        .is_none());
        // Nothing to look after: native.
        let idle = project(
            json!([plugin("1.20.0"), {"name": "psr/http-client", "version": "1.0.3"}]),
            &[],
        );
        assert!(discovery_issue(idle.path(), &allowed).is_none());
        // A version not read: handed over, whatever it looks after.
        let newer = project(json!([plugin("1.21.0")]), &[]);
        let issue = discovery_issue(newer.path(), &allowed)
            .map(|i| i.to_string())
            .unwrap_or_default();
        assert!(
            issue.contains("1.21.0 listens to post-update-cmd in a version not read"),
            "{issue}"
        );
        // Its directory gone: its classes are not there, not loaded.
        let gone = project(json!([plugin("1.20.0"), sdk]), &["php-http/discovery"]);
        assert!(discovery_issue(gone.path(), &allowed).is_none());
    }

    #[test]
    fn a_plugin_neither_emulated_nor_benign_is_handed_over() {
        let tmp = project(
            json!([{"name": "cweagans/composer-patches", "version": "1.7.3", "type": "composer-plugin"}]),
            &[],
        );
        let manifest = json!({"config": {"allow-plugins": {"cweagans/composer-patches": true}}});
        let issues: Vec<String> = issues_before_resolution(tmp.path(), &manifest, true)
            .iter()
            .map(ToString::to_string)
            .collect();
        assert!(
            issues
                .iter()
                .any(|i| i.starts_with("plugin cweagans/composer-patches runs listeners")),
            "{issues:?}"
        );
    }

    #[test]
    fn a_plugin_allowed_but_not_installed_runs_nothing() {
        // Allowed in `allow-plugins`, absent from installed.json: Composer
        // never loads it, so no listener runs — not even the layout of the
        // local repository is computed (an absolute vendor-dir, which
        // `Layout::resolve` refuses, would otherwise fail the run after the
        // lock was written).
        let tmp = project(json!([{"name": "a/lib", "version": "1.0.0"}]), &[]);
        let manifest = json!({"config": {"vendor-dir": "/an/absolute/vendor",
            "allow-plugins": {"phpstan/extension-installer": true,
                "dealerdirect/phpcodesniffer-composer-installer": true}}});
        assert!(post_update_listeners(tmp.path(), &manifest, true, None).is_ok());
        assert!(post_update_listeners(tmp.path(), &manifest, false, None).is_ok());
    }
}