diff options
Diffstat (limited to 'crates/shirabe/src/platform')
| -rw-r--r-- | crates/shirabe/src/platform/hhvm_detector.rs | 14 | ||||
| -rw-r--r-- | crates/shirabe/src/platform/runtime.rs | 58 |
2 files changed, 50 insertions, 22 deletions
diff --git a/crates/shirabe/src/platform/hhvm_detector.rs b/crates/shirabe/src/platform/hhvm_detector.rs index da062d5..69871f1 100644 --- a/crates/shirabe/src/platform/hhvm_detector.rs +++ b/crates/shirabe/src/platform/hhvm_detector.rs @@ -9,6 +9,14 @@ use std::sync::Mutex; // None = null (uninitialized), Some(None) = false (not found), Some(Some(v)) = version static HHVM_VERSION_CACHE: Mutex<Option<Option<String>>> = Mutex::new(None); +/// Seam over HHVM detection so PlatformRepository can be tested with a mocked version. +/// PHP mocks the concrete `Composer\Platform\HhvmDetector` directly; the trait is +/// introduced here to keep the consumer dependent only on trait methods. +pub trait HhvmDetectorInterface: std::fmt::Debug { + fn reset(&self); + fn get_version(&mut self) -> Option<String>; +} + #[derive(Debug)] pub struct HhvmDetector { executable_finder: Option<ExecutableFinder>, @@ -25,12 +33,14 @@ impl HhvmDetector { process_executor, } } +} - pub fn reset(&self) { +impl HhvmDetectorInterface for HhvmDetector { + fn reset(&self) { *HHVM_VERSION_CACHE.lock().unwrap() = None; } - pub fn get_version(&mut self) -> Option<String> { + fn get_version(&mut self) -> Option<String> { let cached = HHVM_VERSION_CACHE.lock().unwrap().clone(); if cached.is_some() { return cached.flatten(); diff --git a/crates/shirabe/src/platform/runtime.rs b/crates/shirabe/src/platform/runtime.rs index aa2f4d7..fcee930 100644 --- a/crates/shirabe/src/platform/runtime.rs +++ b/crates/shirabe/src/platform/runtime.rs @@ -8,41 +8,53 @@ use shirabe_php_shim::{ html_entity_decode, implode, instantiate_class, ltrim, phpversion, strip_tags, trim, }; +/// Seam over the PHP runtime so PlatformRepository can be tested against mocked +/// extension/constant/function probes. PHP has no such interface (the test mocks the +/// concrete `Composer\Platform\Runtime` directly); it is introduced here to keep the +/// consumer dependent only on trait methods. +pub trait RuntimeInterface: std::fmt::Debug { + fn has_constant(&self, constant_name: &str, class: Option<String>) -> bool; + fn get_constant(&self, constant_name: &str, class: Option<String>) -> PhpMixed; + /// `callable` carries the PHP callable spec (a function name string or a + /// `[class, method]` list), matching PHP `invoke($callable, $arguments)`. + fn invoke(&self, callable: PhpMixed, arguments: Vec<PhpMixed>) -> PhpMixed; + fn has_class(&self, class: &str) -> bool; + fn construct(&self, class: &str, arguments: Vec<PhpMixed>) -> Result<PhpMixed>; + fn get_extensions(&self) -> Vec<String>; + fn get_extension_version(&self, extension: &str) -> String; + fn get_extension_info(&self, extension: &str) -> Result<String>; +} + #[derive(Debug)] pub struct Runtime; -impl Runtime { - pub fn has_constant(&self, constant_name: &str, class: Option<&str>) -> bool { +impl RuntimeInterface for Runtime { + fn has_constant(&self, constant_name: &str, class: Option<String>) -> bool { defined(<rim( - &format!("{}::{}", class.unwrap_or(""), constant_name), + &format!("{}::{}", class.as_deref().unwrap_or(""), constant_name), Some(":"), )) } - pub fn get_constant(&self, constant_name: &str, class: Option<&str>) -> PhpMixed { + fn get_constant(&self, constant_name: &str, class: Option<String>) -> PhpMixed { constant(<rim( - &format!("{}::{}", class.unwrap_or(""), constant_name), + &format!("{}::{}", class.as_deref().unwrap_or(""), constant_name), Some(":"), )) } - pub fn has_function(&self, f: &str) -> bool { - function_exists(f) - } - - pub fn invoke( - &self, - callable: Box<dyn Fn(Vec<PhpMixed>) -> PhpMixed>, - arguments: Vec<PhpMixed>, - ) -> PhpMixed { - callable(arguments) + fn invoke(&self, callable: PhpMixed, arguments: Vec<PhpMixed>) -> PhpMixed { + // PHP: return $callable(...$arguments); + // Dispatching an arbitrary PHP callable needs a PHP runtime; no shim exists. + let _ = (callable, arguments); + todo!() } - pub fn has_class(&self, class: &str) -> bool { + fn has_class(&self, class: &str) -> bool { class_exists(class) } - pub fn construct(&self, class: &str, arguments: Vec<PhpMixed>) -> Result<PhpMixed> { + fn construct(&self, class: &str, arguments: Vec<PhpMixed>) -> Result<PhpMixed> { if arguments.is_empty() { Ok(instantiate_class(class, vec![])) } else { @@ -50,20 +62,26 @@ impl Runtime { } } - pub fn get_extensions(&self) -> Vec<String> { + fn get_extensions(&self) -> Vec<String> { get_loaded_extensions() } - pub fn get_extension_version(&self, extension: &str) -> String { + fn get_extension_version(&self, extension: &str) -> String { let version = phpversion(extension); version.unwrap_or_else(|| "0".to_string()) } - pub fn get_extension_info(&self, extension: &str) -> Result<String> { + fn get_extension_info(&self, extension: &str) -> Result<String> { // Depends on \ReflectionExtension::info() and output buffering; no shim equivalent exists. let _ = extension; todo!() } +} + +impl Runtime { + pub fn has_function(&self, f: &str) -> bool { + function_exists(f) + } pub fn parse_html_extension_info(html: &str) -> String { let mut result: Vec<String> = vec![]; |
