2022-03-26 09:42:19 +00:00
|
|
|
use super::{Context, Module, ModuleConfig};
|
2019-12-05 18:04:27 +00:00
|
|
|
|
|
|
|
use crate::configs::php::PhpConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2021-04-29 21:22:20 +00:00
|
|
|
use crate::formatter::VersionFormatter;
|
2019-12-05 18:04:27 +00:00
|
|
|
|
|
|
|
/// Creates a module with the current PHP version
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-02-21 18:52:19 +00:00
|
|
|
let mut module = context.new_module("php");
|
|
|
|
let config: PhpConfig = PhpConfig::try_load(module.config);
|
2019-12-05 18:04:27 +00:00
|
|
|
let is_php_project = context
|
|
|
|
.try_begin_scan()?
|
2021-02-21 18:52:19 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_folders(&config.detect_folders)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
2019-12-05 18:04:27 +00:00
|
|
|
.is_match();
|
|
|
|
|
|
|
|
if !is_php_project {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-01-22 17:03:18 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"version" => {
|
2021-04-29 21:22:20 +00:00
|
|
|
let php_version = context.exec_cmd(
|
2021-01-22 17:03:18 +00:00
|
|
|
"php",
|
|
|
|
&[
|
|
|
|
"-nr",
|
|
|
|
"echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;",
|
|
|
|
],
|
2021-04-29 21:22:20 +00:00
|
|
|
)?.stdout;
|
|
|
|
VersionFormatter::format_module_version(module.get_name(), &php_version, config.version_format).map(Ok)
|
2020-07-07 22:45:32 +00:00
|
|
|
}
|
2021-01-22 17:03:18 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
.parse(None, Some(context))
|
2021-01-22 17:03:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `php`:\n{}", error);
|
|
|
|
return None;
|
2019-12-05 18:04:27 +00:00
|
|
|
}
|
2021-01-22 17:03:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Some(module)
|
2019-12-05 18:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-07 19:13:12 +00:00
|
|
|
use crate::test::ModuleRenderer;
|
2020-02-21 16:52:39 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
2019-12-05 18:04:27 +00:00
|
|
|
|
2020-02-21 16:52:39 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_without_php_files() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
|
2020-02-21 16:52:39 +00:00
|
|
|
|
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-02-21 16:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_composer_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("composer.json"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
|
2020-02-21 16:52:39 +00:00
|
|
|
|
|
|
|
let expected = Some(format!(
|
2021-01-22 17:03:18 +00:00
|
|
|
"via {}",
|
|
|
|
Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
|
2020-02-21 16:52:39 +00:00
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-02-21 16:52:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 19:02:28 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_with_php_version() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join(".php-version"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
|
2020-04-03 19:02:28 +00:00
|
|
|
|
|
|
|
let expected = Some(format!(
|
2021-01-22 17:03:18 +00:00
|
|
|
"via {}",
|
|
|
|
Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
|
2020-04-03 19:02:28 +00:00
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:52:39 +00:00
|
|
|
#[test]
|
|
|
|
fn folder_with_php_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("any.php"))?.sync_all()?;
|
|
|
|
|
2020-08-07 19:13:12 +00:00
|
|
|
let actual = ModuleRenderer::new("php").path(dir.path()).collect();
|
2020-02-21 16:52:39 +00:00
|
|
|
|
|
|
|
let expected = Some(format!(
|
2021-01-22 17:03:18 +00:00
|
|
|
"via {}",
|
|
|
|
Color::Fixed(147).bold().paint("🐘 v7.3.8 ")
|
2020-02-21 16:52:39 +00:00
|
|
|
));
|
|
|
|
assert_eq!(expected, actual);
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-02-21 16:52:39 +00:00
|
|
|
}
|
2019-12-05 18:04:27 +00:00
|
|
|
}
|