lumenize 0.2.0-alpha.18

Laravel integration for Claviron hosts, independent of their server boot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! One launch file: the common runner options and the hosted PHP application.
use std::path::{Path, PathBuf};

use claviron::runner::RunnerOptions;
use serde::{Deserialize, Serialize};

/// Deployment options. All runtime sections come from `config_path`, not a
/// directory of separate PHP, broadcast and logging files.
#[derive(Clone, Debug)]
pub struct Options {
    pub runner: RunnerOptions,
    pub config_path: PathBuf,
    pub configuration: Configuration,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            runner: RunnerOptions::default(),
            config_path: "lumenize.yaml".into(),
            configuration: Configuration::default(),
        }
    }
}

/// Sections owned by their existing Claviron schemas. Retain their unresolved
/// values in the launch: each process resolves them against its boot snapshot.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Configuration {
    /// Explicit execution selection; omission detects Octane at launch.
    pub request_mode: Option<crate::PhpRunner>,
    pub php: serde_yaml::Value,
    pub config_parser: PhpConfigParserOptions,
    pub cache: Option<serde_yaml::Value>,
    /// Claviron's `DbFileConfig`, resolved against each process's boot env.
    pub database: Option<serde_yaml::Value>,
    pub broadcasting: Option<serde_yaml::Value>,
    pub logging: Option<serde_yaml::Value>,
    /// The selected file's identity, carried to replacement workers.
    pub config_path: Option<PathBuf>,
}

/// Safety bound for returned PHP configuration arrays, not a PHP pool policy.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PhpConfigParserOptions {
    pub max_depth: usize,
}

impl Default for PhpConfigParserOptions {
    fn default() -> Self {
        Self { max_depth: default_config_max_depth() }
    }
}

fn default_config_max_depth() -> usize {
    64
}

pub(crate) type Launch = claviron::runner::launch::Launch<Configuration>;

impl From<Launch> for Options {
    fn from(launch: Launch) -> Self {
        Self {
            runner: launch.runner,
            config_path: launch
                .own
                .config_path
                .clone()
                .unwrap_or_else(|| "lumenize.yaml".into()),
            configuration: launch.own,
        }
    }
}

impl From<Options> for Launch {
    fn from(mut options: Options) -> Self {
        options.configuration.config_path = Some(options.config_path);
        Self { runner: options.runner, own: options.configuration }
    }
}

impl Options {
    pub fn from_file(path: impl AsRef<Path>) -> crate::Result<Self> {
        let path = path.as_ref();
        let mut options: Self =
            serde_yaml::from_str::<Launch>(&std::fs::read_to_string(path)?)?
                .into();
        options.config_path = path.to_owned();
        Ok(options)
    }

    pub fn validate(&self) -> crate::Result<()> {
        self.runner.validate()?;
        if self.config_path.as_os_str().is_empty() {
            return Err("config_path must name a file".into());
        }
        if !self.configuration.php.is_null()
            && !self.configuration.php.is_mapping()
        {
            return Err("php must be a mapping".into());
        }
        if let Some(mode) = self.configuration.request_mode {
            validate_mode(mode)?;
        }
        #[cfg(not(feature = "broadcast"))]
        if self.configuration.broadcasting.is_some() {
            return Err("broadcasting requires the broadcast feature".into());
        }
        self.configuration.validate_database_features()?;
        if self.runner.tcp.is_none()
            && self.runner.uds.is_none()
            && !self.runner.orbit
        {
            return Err("select tcp, uds or orbit ingress".into());
        }
        Ok(())
    }

    /// The runner is not an edge and creates no access records.
    /// `logging: {path: ...}` is the single system-log shorthand; the full
    /// section may also declare application channels with the same file.
    pub(crate) fn system_log(
        &self
    ) -> crate::Result<Option<claviron::logging::SystemLogConfig>> {
        let Some(logging) = &self.configuration.logging else {
            return Ok(None);
        };
        let mapping =
            logging.as_mapping().ok_or("logging must be a mapping")?;
        if logging.get("path").is_none() {
            for key in mapping.keys() {
                if !matches!(key.as_str(), Some("system_log" | "channels")) {
                    return Err(format!("unknown logging field {key:?}; expected system_log or channels").into());
                }
            }
        }
        let config = if logging.get("path").is_some() {
            Some(logging)
        } else {
            logging.get("system_log").filter(|value| !value.is_null())
        };
        config
            .map(|value| {
                serde_yaml::from_value(value.clone()).map_err(Into::into)
            })
            .transpose()
    }

    #[cfg(feature = "cli")]
    pub fn from_args() -> crate::Result<Self> {
        use clap::Parser;
        // Re-exec workers receive the launch and do not reopen a default file.
        if let claviron::supervisor::ProcessEntry::Worker(worker) =
            claviron::supervisor::ProcessEntry::detect()?
        {
            return Ok(serde_json::from_slice::<Launch>(
                worker.launch().as_bytes(),
            )?
            .into());
        }
        #[derive(Parser)]
        #[command(
            about = "Serve a Laravel application through Claviron embedded PHP"
        )]
        struct Args {
            #[arg(long, alias = "config", default_value = "lumenize.yaml")]
            config_path: PathBuf,
            #[command(flatten)]
            runner: RunnerOptions,
        }
        let args = Args::parse();
        let mut written = Self::from_file(&args.config_path)?;
        written.runner = args.runner.over(written.runner);
        written.validate()?;
        Ok(written)
    }
}

impl Configuration {
    /// Resolve once, before the master serializes its launch to workers.
    pub(crate) fn prepare_php(
        &mut self,
        root: &Path,
    ) -> crate::Result<Vec<String>> {
        use crate::PhpRunner;
        use claviron::ephp::framework::ComposerPackages;

        // Explicit selection does not depend on readable Composer metadata.
        let packages = if self.request_mode.is_none() {
            Some(ComposerPackages::read(root)?)
        } else {
            ComposerPackages::read(root).ok()
        };
        let mode = *self.request_mode.get_or_insert_with(|| {
            if packages
                .as_ref()
                .is_some_and(|packages| packages.contains("laravel/octane"))
            {
                PhpRunner::Application
            } else {
                PhpRunner::Script
            }
        });
        validate_mode(mode)?;
        if self.php.is_null() {
            self.php = serde_yaml::Value::Mapping(Default::default());
        }
        let php = self.php.as_mapping_mut().ok_or("php must be a mapping")?;
        match mode {
            PhpRunner::Application => {
                if php.contains_key("script") {
                    return Err(
                        "php.script requires request_mode: script".into()
                    );
                }
                let application =
                    php.entry("application".into()).or_insert_with(|| {
                        serde_yaml::Value::Mapping(Default::default())
                    });
                let application = application
                    .as_mapping_mut()
                    .ok_or("php.application must be a mapping")?;
                if !application.contains_key("script_path") {
                    let entry = if root.join("application.php").try_exists()? {
                        PathBuf::from("application.php")
                    } else {
                        let mut args =
                            application.remove("args").unwrap_or_else(|| {
                                serde_yaml::Value::Sequence(Vec::new())
                            });
                        args.as_sequence_mut()
                            .ok_or("php.application.args must be a sequence")?
                            .insert(0, serde_yaml::to_value(root)?);
                        application.insert("args".into(), args);
                        PathBuf::from(
                            "vendor/claviron/laravel/runners/application.php",
                        )
                    };
                    application.insert(
                        "script_path".into(),
                        serde_yaml::to_value(entry)?,
                    );
                }
            }
            PhpRunner::Script => {
                if php.contains_key("application")
                    || php.contains_key("applications")
                {
                    return Err("php.application requires request_mode: application; script execution uses php.script and php.sapi".into());
                }
            }
            #[allow(unreachable_patterns)]
            _ => unreachable!("mode was validated"),
        }
        Ok(packages.map(|packages| packages.frameworks()).unwrap_or_default())
    }

    pub(crate) fn script_route(
        &self,
        state: &claviron::runtime::AppState,
    ) -> claviron::runtime::registry::Result<claviron::php::PhpConfig> {
        use claviron::runtime::registry;
        let value =
            self.php.get("script").cloned().unwrap_or_else(|| {
                serde_yaml::Value::Mapping(Default::default())
            });
        let mut route = value.as_mapping().cloned().ok_or_else(|| {
            registry::Error::msg("php.script must be a mapping")
        })?;
        route.entry("runner".into()).or_insert("script".into());
        route.entry("entry_script".into()).or_insert("index.php".into());
        let route: claviron::php::PhpConfig = expand(state, &route.into())?;
        if route.runner != crate::PhpRunner::Script {
            return Err(registry::Error::msg(
                "php.script.runner must be script",
            ));
        }
        Ok(route)
    }

    fn validate_database_features(&self) -> crate::Result<()> {
        let Some(database) = &self.database else {
            return Ok(());
        };
        if !cfg!(feature = "db") {
            return Err("database requires a database feature (postgres, mysql or sqlite)".into());
        }
        let mapping =
            database.as_mapping().ok_or("database must be a mapping")?;
        for (backend, enabled) in [
            ("postgres", cfg!(feature = "postgres")),
            ("mysql", cfg!(feature = "mysql")),
            ("sqlite", cfg!(feature = "sqlite")),
        ] {
            if mapping.contains_key(serde_yaml::Value::from(backend))
                && !enabled
            {
                return Err(format!(
                    "database.{backend} requires the {backend} feature"
                )
                .into());
            }
        }
        Ok(())
    }

    pub(crate) fn register(
        &self,
        state: &claviron::runtime::AppState,
        application: &str,
        frameworks: Vec<String>,
        icon: Option<String>,
    ) -> claviron::runtime::registry::Result<()> {
        use claviron::runtime::registry;
        self.validate_database_features()
            .map_err(|error| registry::Error::msg(error.to_string()))?;
        #[cfg(feature = "db")]
        {
            // No separate db.yaml, even when the launch declares no native
            // connections. Application dependencies decide what is required.
            let database = self
                .database
                .as_ref()
                .map(|value| expand::<claviron::db::DbFileConfig>(state, value))
                .transpose()?
                .unwrap_or_default();
            claviron::db::register_with_config(state, database)?;
        }
        let mode = self.request_mode.ok_or_else(|| {
            registry::Error::msg("PHP request mode was not prepared")
        })?;
        let mut php = self.php.clone();
        if let Some(mapping) = php.as_mapping_mut() {
            mapping.remove("script");
        }
        let yaml = serde_yaml::to_string(&php)
            .map_err(|error| registry::Error::msg(error.to_string()))?;
        let description = claviron::app::ApplicationDescription {
            frameworks,
            icon,
            ..claviron::app::ApplicationDescription::new("php", mode.as_str())
                .runner("Lumenize")
        };
        match mode {
            crate::PhpRunner::Application => {
                claviron::ephp::register_application_from_yaml(
                    state,
                    application,
                    &yaml,
                    description,
                )?
            }
            crate::PhpRunner::Script => {
                claviron::ephp::register_script_from_yaml(
                    state,
                    application,
                    &yaml,
                    description,
                )?
            }
            #[allow(unreachable_patterns)]
            _ => {
                return Err(registry::Error::msg(
                    "Lumenize supports application or script request mode",
                ));
            }
        }
        // Registration declared the process INI above; no evaluation occurs
        // until configuration preparation, after all native extensions exist.
        let root = std::env::current_dir()
            .map_err(|error| registry::Error::msg(error.to_string()))?;
        let parser = crate::congif::LaravelConfigParser::new(
            root,
            self.config_parser.max_depth,
        )
        .map_err(|error| registry::Error::msg(error.to_string()))?;
        state.config_reader().register(parser)?;

        let cache = self
            .cache
            .as_ref()
            .map(|value| expand::<claviron::cache::CacheConfig>(state, value))
            .transpose()?
            .unwrap_or_default();
        claviron::cache::register_with_config(state, cache)?;

        #[cfg(feature = "log")]
        {
            let mut logging = self.logging.clone().unwrap_or_else(|| {
                serde_yaml::Value::Mapping(Default::default())
            });
            if logging.get("path").is_some() {
                logging = serde_yaml::Value::Mapping(Default::default());
            }
            let map = logging
                .as_mapping_mut()
                .ok_or_else(|| registry::Error::msg("logging is a mapping"))?;
            map.entry("channels".into()).or_insert_with(|| {
                serde_yaml::Value::Mapping(Default::default())
            });
            let config = expand::<claviron::logging::pipeline::LoggingConfig>(
                state, &logging,
            )?;
            claviron::logging::pipeline::register_with_config(state, config)?;
        }
        #[cfg(not(feature = "log"))]
        if self
            .logging
            .as_ref()
            .is_some_and(|value| value.get("channels").is_some())
        {
            return Err(registry::Error::msg(
                "logging.channels requires the log feature",
            ));
        }

        #[cfg(feature = "broadcast")]
        {
            let mut applications = serde_yaml::Mapping::new();
            if let Some(broadcasting) = &self.broadcasting {
                let mut config =
                    broadcasting.as_mapping().cloned().ok_or_else(|| {
                        registry::Error::msg("broadcasting is a mapping")
                    })?;
                // Native publication needs no second app identity. External
                // Pusher REST clients can explicitly set their wire id.
                config.entry("id".into()).or_insert_with(|| application.into());
                applications.insert(application.into(), config.into());
            }
            let mut document = serde_yaml::Mapping::new();
            document.insert("applications".into(), applications.into());
            let config = expand::<claviron::broadcast::BroadcastConfig>(
                state,
                &document.into(),
            )?;
            claviron::broadcast::register_with_config(state, config)?;
            if self.broadcasting.is_some() {
                use crate::broadcast::LaravelBroadcastProvider;
                use claviron::runtime::Register;

                LaravelBroadcastProvider::register(state)?;
            }
        }
        Ok(())
    }
}

fn validate_mode(mode: crate::PhpRunner) -> crate::Result<()> {
    if !matches!(mode, crate::PhpRunner::Application | crate::PhpRunner::Script)
    {
        return Err(
            "Lumenize supports application or script request mode".into()
        );
    }
    Ok(())
}

fn expand<T: serde::de::DeserializeOwned>(
    state: &claviron::runtime::AppState,
    value: &serde_yaml::Value,
) -> claviron::runtime::registry::Result<T> {
    use claviron::runtime::registry;
    let raw = serde_yaml::to_string(value)
        .map_err(|error| registry::Error::msg(error.to_string()))?;
    let raw = claviron::helpers::yaml::expand_env_with(&raw, |key| {
        state.env_snapshot().get(key)
    })
    .map_err(|error| registry::Error::msg(error.to_string()))?;
    serde_yaml::from_str(&raw)
        .map_err(|error| registry::Error::msg(error.to_string()))
}

#[cfg(test)]
#[path = "../tests/options.rs"]
mod tests;