aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/finder/spl_file_info.rs
blob: c1d158dd3edd34095754c27c948cbffa834ad7c6 (plain)
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
//! ref: composer/vendor/symfony/finder/SplFileInfo.php

#[derive(Debug)]
pub struct SplFileInfo {
    // The path passed to the underlying \SplFileInfo constructor.
    pathname: String,
    relative_path: String,
    relative_pathname: String,
}

impl std::fmt::Display for SplFileInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.get_pathname())
    }
}

impl SplFileInfo {
    pub fn new(file: &str, relative_path: &str, relative_pathname: &str) -> Self {
        Self {
            pathname: file.to_string(),
            relative_path: relative_path.to_string(),
            relative_pathname: relative_pathname.to_string(),
        }
    }

    pub fn get_pathname(&self) -> String {
        self.pathname.clone()
    }

    pub fn get_path(&self) -> String {
        shirabe_php_shim::dirname(&self.pathname)
    }

    pub fn get_filename(&self) -> String {
        shirabe_php_shim::basename(&self.pathname)
    }

    pub fn get_basename(&self, suffix: Option<&str>) -> String {
        match suffix {
            Some(suffix) => shirabe_php_shim::basename_with_suffix(&self.pathname, suffix),
            None => shirabe_php_shim::basename(&self.pathname),
        }
    }

    pub fn get_extension(&self) -> String {
        // \SplFileInfo::getExtension() returns the extension (without the dot), or "" if none.
        let base = shirabe_php_shim::basename(&self.pathname);
        match base.rfind('.') {
            Some(index) => base[index + 1..].to_string(),
            None => String::new(),
        }
    }

    pub fn get_relative_path_name(&self) -> String {
        self.relative_pathname.clone()
    }

    pub fn get_relative_path(&self) -> String {
        self.relative_path.clone()
    }

    pub fn is_dir(&self) -> bool {
        shirabe_php_shim::is_dir(&self.pathname)
    }

    pub fn is_file(&self) -> bool {
        shirabe_php_shim::is_file(&self.pathname)
    }

    pub fn is_link(&self) -> bool {
        shirabe_php_shim::is_link(&self.pathname)
    }

    pub fn get_real_path(&self) -> Option<String> {
        // \SplFileInfo::getRealPath() returns the canonicalized absolute path, or false on failure.
        shirabe_php_shim::realpath(&self.pathname)
    }

    pub fn get_size(&self) -> i64 {
        // \SplFileInfo::getSize() returns the file size in bytes (throws on failure).
        // TODO(phase-d): PHP throws a \RuntimeException on stat failure; this returns 0 instead.
        shirabe_php_shim::filesize(&self.pathname).unwrap_or(0)
    }
}