PhpSapiAdapter

Trait PhpSapiAdapter 

Source
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:

  1. Validate configuration before building
  2. Handle script path existence checking
  3. Return appropriate error types
  4. Ensure the resulting ExecutionContext is 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§

Source

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 execute
  • Err(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§

Source

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 path
  • Err(AdapterError::ScriptNotFound) - Path does not exist
Source

fn validate_non_empty(field_name: &str, value: &str) -> Result<(), AdapterError>
where Self: Sized,

Validate a configuration field is not empty.

Utility method for validating string configuration fields.

§Arguments
  • field_name - Name of the field for error reporting
  • value - Value to validate
§Returns
  • Ok(()) - Value is non-empty
  • Err(AdapterError::MissingConfiguration) - Value is empty
Source

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,

Validate a configuration field against a predicate.

Utility method for custom field validation.

§Arguments
  • field_name - Name of the field for error reporting
  • value - Value to validate
  • predicate - Validation function
  • error_reason - Reason for validation failure
§Returns
  • Ok(()) - Validation passed
  • Err(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.

Implementors§