//! 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 (`/tools/phpactor//`) //! 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"), "