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 $merge |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
6 |
|
public function getDirectories($merge = true) |
71
|
|
|
{ |
72
|
6 |
|
$dirs = $this->dirs; |
73
|
6 |
|
if ($merge && $this->use_include_path) { |
74
|
1 |
|
$dirs = array_merge($dirs, explode(PATH_SEPARATOR, get_include_path())); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
6 |
|
return $dirs; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param bool $use |
82
|
|
|
*/ |
83
|
6 |
|
public function useIncludePath($use) |
84
|
|
|
{ |
85
|
6 |
|
$this->use_include_path = $use; |
86
|
6 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $extension |
90
|
|
|
*/ |
91
|
2 |
|
public function setExtension($extension) |
92
|
|
|
{ |
93
|
2 |
|
$this->extension = $extension; |
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $filename |
98
|
|
|
* |
99
|
|
|
* @return mixed|string |
100
|
|
|
*/ |
101
|
6 |
|
protected function normalizeFilename($filename) |
102
|
|
|
{ |
103
|
6 |
|
$normalized_filename = $filename; |
104
|
6 |
|
if ($this->directory_separator !== self::DEFAULT_DIRECTORY_SEPARATOR) { |
105
|
1 |
|
$dir_name = pathinfo($normalized_filename, PATHINFO_DIRNAME); |
106
|
1 |
|
$normalized_filename = str_replace($dir_name, str_replace($this->directory_separator, self::DEFAULT_DIRECTORY_SEPARATOR, $dir_name), $normalized_filename); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
6 |
|
if (pathinfo($normalized_filename, PATHINFO_EXTENSION) == '' && !empty($this->extension)) { |
110
|
2 |
|
$normalized_filename = $normalized_filename.'.'.$this->extension; |
111
|
2 |
|
} |
112
|
|
|
|
113
|
6 |
|
return $normalized_filename; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $in |
118
|
|
|
* |
119
|
|
|
* @return null|string |
120
|
|
|
* |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
6 |
|
public function resolve($in) |
124
|
|
|
{ |
125
|
6 |
|
$filename = $in; |
126
|
6 |
|
$filename = $this->normalizeFilename($filename); |
127
|
|
|
|
128
|
6 |
|
foreach ($this->getDirectories() as $dir) { |
129
|
6 |
|
$path = $dir.DIRECTORY_SEPARATOR.$filename; |
130
|
6 |
|
if (file_exists($path)) { |
131
|
5 |
|
return realpath($path); |
132
|
|
|
} |
133
|
2 |
|
} |
134
|
|
|
|
135
|
1 |
|
throw new \Exception(sprintf('Could not resolve %s to a filename', $in)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|