|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Softius\ResourcesResolver; |
|
4
|
|
|
|
|
5
|
|
|
class FilenameResolver implements ResolvableInterface |
|
6
|
|
|
{ |
|
7
|
|
|
const DEFAULT_DIRECTORY_SEPARATOR = DIRECTORY_SEPARATOR; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var bool |
|
11
|
|
|
*/ |
|
12
|
|
|
private $directory_separator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $dirs; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private $use_include_path; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $extension; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* FilePathResolver constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param null $directories |
|
33
|
|
|
* @param null $directory_separator |
|
34
|
|
|
*/ |
|
35
|
6 |
|
public function __construct($directories = null, $directory_separator = null) |
|
36
|
|
|
{ |
|
37
|
6 |
|
$this->directory_separator = ($directory_separator === null) ? self::DEFAULT_DIRECTORY_SEPARATOR : $directory_separator; |
|
38
|
|
|
|
|
39
|
6 |
|
$this->dirs = []; |
|
40
|
6 |
|
if (is_string($directories)) { |
|
41
|
4 |
|
$this->addDirectory($directories); |
|
42
|
6 |
|
} elseif (is_array($directories)) { |
|
43
|
1 |
|
$this->addDirectories($directories); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
6 |
|
$this->useIncludePath(false); |
|
47
|
6 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $directories |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function addDirectories($directories) |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->dirs = array_merge($this->dirs, $directories); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $directory |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function addDirectory($directory) |
|
61
|
|
|
{ |
|
62
|
4 |
|
array_push($this->dirs, $directory); |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param bool $use |
|
67
|
|
|
*/ |
|
68
|
6 |
|
public function useIncludePath($use) |
|
69
|
|
|
{ |
|
70
|
6 |
|
$this->use_include_path = $use; |
|
71
|
6 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $extension |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function setExtension($extension) |
|
77
|
|
|
{ |
|
78
|
2 |
|
$this->extension = $extension; |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $in |
|
83
|
|
|
* @return null|string |
|
84
|
|
|
* @throws \Exception |
|
85
|
|
|
*/ |
|
86
|
6 |
|
public function resolve($in) |
|
87
|
|
|
{ |
|
88
|
6 |
|
$partial_path = $in; |
|
89
|
|
|
|
|
90
|
6 |
|
if ($this->directory_separator !== self::DEFAULT_DIRECTORY_SEPARATOR) { |
|
91
|
1 |
|
$dir_name = pathinfo($partial_path, PATHINFO_DIRNAME); |
|
92
|
1 |
|
$partial_path = str_replace($dir_name, str_replace($this->directory_separator, self::DEFAULT_DIRECTORY_SEPARATOR, $dir_name), $partial_path); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
if (pathinfo($partial_path, PATHINFO_EXTENSION) == '' && !empty($this->extension)) { |
|
96
|
2 |
|
$partial_path = $partial_path.'.'.$this->extension; |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
$dirs = $this->dirs; |
|
100
|
6 |
|
if ($this->use_include_path) { |
|
101
|
1 |
|
$dirs = array_merge($dirs, explode(PATH_SEPARATOR, get_include_path())); |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
6 |
|
foreach ($dirs as $dir) { |
|
105
|
6 |
|
$path = $dir.DIRECTORY_SEPARATOR.$partial_path; |
|
106
|
6 |
|
if (file_exists($path)) { |
|
107
|
5 |
|
return realpath($path); |
|
108
|
|
|
} |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
throw new \Exception(sprintf('Could not resolve %s to a filename', $in)); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|