M3.1: PHP detection and pinned Phpactor download
- crates/lsp toolchain: PHP from php.executable or PATH, `php -v` with a 5 s timeout and capped output, version parsing (proptest), PHP >= 8.2. - Pinned Phpactor 2026.06.25.0 (SHA-256 832510fe...59e9); newer releases need ext-posix and do not start on Windows. - Download with ureq + rustls (ring, OS roots via rustls-native-certs): HTTPS only, 32 MiB cap, SHA-256 verified before an atomic write, retry with backoff. Tests use a local HTTP server only. - Engine: settings php.executable, phpactor.path, phpactor.autoDownload (bool and string setting kinds), toolchain.status, toolchain.downloadPhpactor, toolchainChanged, notifications; worker thread off the engine lock, results via the pump. Downloads only after the host calls Engine::allow_downloads (the desktop does). - Docs: architecture Toolchain section (network access, D15), G6. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
78709dd76b
commit
50f36cce8b
23 changed files with 2577 additions and 10 deletions
118
crates/lsp/src/toolchain/mod.rs
Normal file
118
crates/lsp/src/toolchain/mod.rs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
//! The toolchain Lutra needs for PHP language features: the user's PHP
|
||||
//! and a Phpactor PHAR (owner decision D15).
|
||||
//!
|
||||
//! - [`php`] finds the PHP executable (a setting or `PATH`), runs `php -v`
|
||||
//! with a timeout and checks the version. Its output is untrusted.
|
||||
//! - [`phpactor`] knows the pinned Phpactor release (version, URL,
|
||||
//! SHA-256), where Lutra keeps it (`<data_dir>/tools/phpactor/<version>/`)
|
||||
//! and verifies a file against the pinned hash.
|
||||
//! - [`download`] fetches a file over HTTPS (pure Rust: `ureq` with
|
||||
//! `rustls`, roots from the operating system), capped in size, verified
|
||||
//! against a SHA-256 before it is written atomically, with retries and
|
||||
//! backoff. It is the only network access in Lutra; nothing in this crate
|
||||
//! starts it on its own.
|
||||
//! - [`has_php_files`] tells whether a project contains PHP code, which is
|
||||
//! when Lutra downloads Phpactor.
|
||||
//!
|
||||
//! Every function here blocks (process runs, file hashing, network), so
|
||||
//! callers run them off the engine lock.
|
||||
|
||||
pub mod download;
|
||||
pub mod php;
|
||||
pub mod phpactor;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/// Directory names [`has_php_files`] does not descend into.
|
||||
const SKIPPED_DIRS: &[&str] = &["vendor", "node_modules", ".git", ".idea", "var", "storage"];
|
||||
|
||||
/// Most directory entries [`has_php_files`] looks at.
|
||||
pub const MAX_SCAN_ENTRIES: usize = 20_000;
|
||||
|
||||
/// Deepest directory level [`has_php_files`] looks at; the root is 0.
|
||||
pub const MAX_SCAN_DEPTH: usize = 6;
|
||||
|
||||
/// Whether `root` contains PHP code: a `composer.json` at the root or a file
|
||||
/// with a PHP extension (`php`, `phtml`, `inc`) within [`MAX_SCAN_DEPTH`]
|
||||
/// levels, looking at no more than [`MAX_SCAN_ENTRIES`] entries. Hidden
|
||||
/// directories, dependency and cache directories ([`SKIPPED_DIRS`]) and
|
||||
/// symbolic links are skipped. Directories that cannot be read are skipped
|
||||
/// too, since the answer only decides whether a download is worth it.
|
||||
pub fn has_php_files(root: &Path) -> bool {
|
||||
if root.join("composer.json").is_file() {
|
||||
return true;
|
||||
}
|
||||
let mut queue = std::collections::VecDeque::from([(root.to_path_buf(), 0usize)]);
|
||||
let mut seen = 0usize;
|
||||
while let Some((dir, depth)) = queue.pop_front() {
|
||||
let Ok(entries) = std::fs::read_dir(&dir) else {
|
||||
continue;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
seen = seen.saturating_add(1);
|
||||
if seen > MAX_SCAN_ENTRIES {
|
||||
return false;
|
||||
}
|
||||
let Ok(kind) = entry.file_type() else {
|
||||
continue;
|
||||
};
|
||||
let name = entry.file_name();
|
||||
let name = name.to_string_lossy();
|
||||
if kind.is_file() && is_php_name(&name) {
|
||||
return true;
|
||||
}
|
||||
if kind.is_dir()
|
||||
&& depth < MAX_SCAN_DEPTH
|
||||
&& !name.starts_with('.')
|
||||
&& !SKIPPED_DIRS.contains(&name.as_ref())
|
||||
{
|
||||
queue.push_back((entry.path(), depth.saturating_add(1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Whether a file name has a PHP extension, ignoring case.
|
||||
fn is_php_name(name: &str) -> bool {
|
||||
let Some((_, ext)) = name.rsplit_once('.') else {
|
||||
return false;
|
||||
};
|
||||
["php", "phtml", "inc"]
|
||||
.iter()
|
||||
.any(|e| ext.eq_ignore_ascii_case(e))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn finds_php_files_and_composer_projects() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
assert!(!has_php_files(dir.path()));
|
||||
std::fs::create_dir_all(dir.path().join("vendor/x")).unwrap();
|
||||
std::fs::write(dir.path().join("vendor/x/a.php"), "<?php").unwrap();
|
||||
std::fs::write(dir.path().join("readme.md"), "hi").unwrap();
|
||||
assert!(!has_php_files(dir.path()), "vendor is skipped");
|
||||
std::fs::create_dir_all(dir.path().join("src/App")).unwrap();
|
||||
std::fs::write(dir.path().join("src/App/User.PHP"), "<?php").unwrap();
|
||||
assert!(has_php_files(dir.path()));
|
||||
|
||||
let other = tempfile::tempdir().unwrap();
|
||||
std::fs::write(other.path().join("composer.json"), "{}").unwrap();
|
||||
assert!(has_php_files(other.path()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stops_at_the_depth_cap() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut deep = dir.path().to_path_buf();
|
||||
for i in 0..=MAX_SCAN_DEPTH {
|
||||
deep.push(format!("d{i}"));
|
||||
}
|
||||
std::fs::create_dir_all(&deep).unwrap();
|
||||
std::fs::write(deep.join("a.php"), "<?php").unwrap();
|
||||
assert!(!has_php_files(dir.path()));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue