pub trait PhpSapiAdapter {
// Required method
fn build(
self,
script_path: impl AsRef<Path>,
) -> Result<ExecutionContext, AdapterError>;
// Provided methods
fn validate_script_path(
script_path: impl AsRef<Path>,
) -> Result<PathBuf, AdapterError>
where Self: Sized { ... }
fn validate_non_empty(
field_name: &str,
value: &str,
) -> Result<(), AdapterError>
where Self: Sized { ... }
fn validate_field<T, F>(
field_name: &str,
value: &T,
predicate: F,
error_reason: &str,
) -> Result<(), AdapterError>
where Self: Sized,
T: Display,
F: FnOnce(&T) -> bool { ... }
}Expand description
Trait for types that can be converted into PHP execution contexts.
This trait provides a unified interface for building ExecutionContext instances
from different request types (Web, CLI, custom adapters). Implementors configure
their internal state through builder methods, then call build() to create
the final execution context.
§Design Goals
- Unified Interface: All adapters share the same
build()signature - Type Safety: Strong typing prevents configuration errors at compile time
- Flexibility: Adapters can have adapter-specific configuration while sharing common patterns
- Error Handling: Consistent error types across all adapters
§Examples
use ripht_php_sapi::{WebRequest, CliRequest, adapters::PhpSapiAdapter};
use std::path::Path;
let script = Path::new("/var/www/script.php");
// Web adapter
let web_ctx = WebRequest::get()
.with_uri("/api/test")
.build(script)?;
// CLI adapter
let cli_ctx = CliRequest::new()
.with_arg("--verbose")
.build(script)?;
// Both return [ExecutionContext] through the same interface§Implementation Requirements
Implementors must:
- Validate configuration before building
- Handle script path existence checking
- Return appropriate error types
- Ensure the resulting
ExecutionContextis valid for execution
§Validation Patterns
The trait provides default validation methods that implementors can use:
fn build(self, script_path: impl AsRef<Path>) -> Result<ExecutionContext, AdapterError> {
let path = Self::validate_script_path(script_path)?;
// ... adapter-specific building logic
}Required Methods§
Sourcefn build(
self,
script_path: impl AsRef<Path>,
) -> Result<ExecutionContext, AdapterError>
fn build( self, script_path: impl AsRef<Path>, ) -> Result<ExecutionContext, AdapterError>
Build an execution context from the configured adapter.
This method consumes the adapter and produces an ExecutionContext ready
for execution. The script path is validated for existence, and all adapter
configuration is converted into the appropriate server variables, environment
variables, and execution parameters.
§Arguments
script_path- Path to the PHP script to execute. Must exist and be readable.
§Returns
Ok(ExecutionContext)- Ready to executeErr(AdapterError)- Configuration or validation error
§Errors
This method will return an error if:
- The script file does not exist
- Required configuration is missing
- Configuration values are invalid
- Adapter-specific validation fails
Provided Methods§
Sourcefn validate_script_path(
script_path: impl AsRef<Path>,
) -> Result<PathBuf, AdapterError>where
Self: Sized,
fn validate_script_path(
script_path: impl AsRef<Path>,
) -> Result<PathBuf, AdapterError>where
Self: Sized,
Validate that a script path exists and is accessible.
This is a utility method that adapters can use for consistent path validation. It converts the path to an absolute path when possible and checks for existence.
§Arguments
script_path- Path to validate
§Returns
Ok(PathBuf)- Validated, absolute pathErr(AdapterError::ScriptNotFound)- Path does not exist
Sourcefn validate_non_empty(field_name: &str, value: &str) -> Result<(), AdapterError>where
Self: Sized,
fn validate_non_empty(field_name: &str, value: &str) -> Result<(), AdapterError>where
Self: Sized,
Sourcefn validate_field<T, F>(
field_name: &str,
value: &T,
predicate: F,
error_reason: &str,
) -> Result<(), AdapterError>
fn validate_field<T, F>( field_name: &str, value: &T, predicate: F, error_reason: &str, ) -> Result<(), AdapterError>
Validate a configuration field against a predicate.
Utility method for custom field validation.
§Arguments
field_name- Name of the field for error reportingvalue- Value to validatepredicate- Validation functionerror_reason- Reason for validation failure
§Returns
Ok(())- Validation passedErr(AdapterError::InvalidConfiguration)- Validation failed
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.