use std::convert::Infallible;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, OnceLock};
use std::task::{Context, Poll};
use async_trait::async_trait;
use claviron::http::RouteResponse;
use claviron::runner::AppRequest;
use claviron::runtime::registry::{self, Bootable};
use claviron::runtime::{AppState, Component};
use claviron_php::embed::{
PhpApplicationAdapter, PhpApplicationService, PhpScriptAdapter,
PhpScriptService,
};
use http_body_util::BodyExt;
use tower::Service;
#[derive(Clone)]
pub(crate) struct PhpService(Arc<OnceLock<PhpAdapter>>);
enum PhpAdapter {
Application(PhpApplicationAdapter),
Script(PhpScriptAdapter),
}
impl PhpService {
pub(crate) fn new() -> Self {
Self(Arc::new(OnceLock::new()))
}
}
impl Service<AppRequest> for PhpService {
type Response = RouteResponse;
type Error = Infallible;
type Future =
Pin<Box<dyn Future<Output = Result<RouteResponse, Infallible>> + Send>>;
fn poll_ready(
&mut self,
_: &mut Context<'_>,
) -> Poll<Result<(), Infallible>> {
Poll::Ready(Ok(()))
}
fn call(
&mut self,
request: AppRequest,
) -> Self::Future {
let php = Arc::clone(&self.0);
Box::pin(async move {
let adapter = php.get().expect("PHP boots before ingress starts");
let request = request.map(|body| body.boxed_unsync());
match adapter {
PhpAdapter::Application(adapter) => {
adapter.handle(request).await
}
PhpAdapter::Script(adapter) => adapter.handle(request).await,
}
})
}
}
pub(crate) struct PhpOwner {
name: String,
php: PhpService,
script: Option<claviron_php::PhpConfig>,
}
impl PhpOwner {
pub(crate) fn new(
name: String,
php: PhpService,
script: Option<claviron_php::PhpConfig>,
) -> Self {
Self { name, php, script }
}
}
impl Component<AppState> for PhpOwner {
type Deps =
(Option<Arc<PhpApplicationService>>, Option<Arc<PhpScriptService>>);
fn name(&self) -> &'static str {
"LumenizePhpOwner"
}
fn as_bootable(&self) -> Option<&dyn Bootable<AppState, Self::Deps>> {
Some(self)
}
fn validate(
&self,
state: &AppState,
) -> registry::Result<()> {
let runtime = state
.registry_ref()
.resolve::<claviron_php::embed::PhpEmbedRuntime>()
.ok_or_else(|| {
registry::Error::msg("PHP runtime was not registered")
})?;
if self.script.is_none() && !runtime.has_application(&self.name) {
return Err(registry::Error::msg(format!(
"PHP application {:?} is not registered",
self.name
)));
}
Ok(())
}
}
#[async_trait]
impl Bootable<AppState, <Self as Component<AppState>>::Deps> for PhpOwner {
async fn boot(
&self,
(application, script): <Self as Component<AppState>>::Deps,
state: &AppState,
) -> registry::Result<()> {
if state.is_master() {
return Ok(());
}
let adapter = if let Some(config) = &self.script {
let service = script.ok_or_else(|| {
registry::Error::msg("PHP script service was not registered")
})?;
let root = std::env::current_dir()
.map_err(|error| registry::Error::msg(error.to_string()))?;
let runtime = claviron_php::PhpRuntime::from_config(
state,
config,
root.join("public"),
&[],
);
PhpAdapter::Script(PhpScriptAdapter::new(
service,
Arc::new(runtime),
))
} else {
let service = application.ok_or_else(|| {
registry::Error::msg(
"PHP application service was not registered",
)
})?;
PhpAdapter::Application(PhpApplicationAdapter::new(
service,
self.name.clone(),
))
};
self.php
.0
.set(adapter)
.map_err(|_| registry::Error::msg("PHP owner was already bound"))?;
Ok(())
}
}