1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PathTools plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* This twig plugin for the Craft CMS brings convenient path & url manipulation functions & filters to your |
6
|
|
|
* Twig templates. |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\pathtools\twigextensions; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
|
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @author nystudio107 |
|
|
|
|
18
|
|
|
* @package PathTools |
|
|
|
|
19
|
|
|
* @since 1.0.0 |
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
class PathToolsTwigExtension extends \Twig_Extension |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
// Public Methods |
24
|
|
|
// ========================================================================= |
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
public function getName() |
30
|
|
|
{ |
31
|
|
|
return 'PathTools'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
public function getFilters() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
|
|
new \Twig_SimpleFilter('pathinfo', [$this, 'pathInfoFilter']), |
|
|
|
|
41
|
|
|
new \Twig_SimpleFilter('basename', [$this, 'baseNameFilter']), |
|
|
|
|
42
|
|
|
new \Twig_SimpleFilter('dirname', [$this, 'dirNameFilter']), |
|
|
|
|
43
|
|
|
new \Twig_SimpleFilter('parse_url', [$this, 'parseUrlFilter']), |
|
|
|
|
44
|
|
|
new \Twig_SimpleFilter('parse_string', [$this, 'parseStringFilter']), |
|
|
|
|
45
|
|
|
new \Twig_SimpleFilter('swap_extension', [$this, 'swapExtensionFilter']), |
|
|
|
|
46
|
|
|
new \Twig_SimpleFilter('swap_directory', [$this, 'swapDirectoryFilter']), |
|
|
|
|
47
|
|
|
new \Twig_SimpleFilter('append_suffix', [$this, 'appendSuffixFilter']), |
|
|
|
|
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
|
|
|
|
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
|
|
|
|
54
|
|
|
public function getFunctions() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
new \Twig_SimpleFunction('pathinfo', [$this, 'pathInfoFilter']), |
|
|
|
|
58
|
|
|
new \Twig_SimpleFunction('basename', [$this, 'baseNameFilter']), |
|
|
|
|
59
|
|
|
new \Twig_SimpleFunction('dirname', [$this, 'dirNameFilter']), |
|
|
|
|
60
|
|
|
new \Twig_SimpleFunction('parse_url', [$this, 'parseUrlFilter']), |
|
|
|
|
61
|
|
|
new \Twig_SimpleFunction('parse_string', [$this, 'parseStringFilter']), |
|
|
|
|
62
|
|
|
new \Twig_SimpleFunction('swap_extension', [$this, 'swapExtensionFilter']), |
|
|
|
|
63
|
|
|
new \Twig_SimpleFunction('swap_directory', [$this, 'swapDirectoryFilter']), |
|
|
|
|
64
|
|
|
new \Twig_SimpleFunction('append_suffix', [$this, 'appendSuffixFilter']), |
|
|
|
|
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* php pathinfo() wrapper -- http://php.net/manual/en/function.pathinfo.php |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @param $path |
|
|
|
|
73
|
|
|
* @param bool $options |
|
|
|
|
74
|
|
|
* @return mixed |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function pathInfoFilter($path, $options = false) |
77
|
|
|
{ |
78
|
|
|
if ($options) { |
79
|
|
|
$output = pathinfo($path, $options); |
|
|
|
|
80
|
|
|
} else { |
81
|
|
|
$output = pathinfo($path); |
82
|
|
|
} |
83
|
|
|
return $output; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* php basename() wrapper -- http://php.net/manual/en/function.basename.php |
|
|
|
|
88
|
|
|
* |
89
|
|
|
* @param $path |
|
|
|
|
90
|
|
|
* @param bool $suffix |
|
|
|
|
91
|
|
|
* @return string |
|
|
|
|
92
|
|
|
*/ |
93
|
|
|
public function baseNameFilter($path, $suffix = false) |
94
|
|
|
{ |
95
|
|
|
if ($suffix) { |
96
|
|
|
$output = basename($path, $suffix); |
|
|
|
|
97
|
|
|
} else { |
98
|
|
|
$output = basename($path); |
99
|
|
|
} |
100
|
|
|
return $output; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* php dirname() wrapper -- http://php.net/manual/en/function.dirname.php |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @param $path |
|
|
|
|
107
|
|
|
* @return string |
|
|
|
|
108
|
|
|
*/ |
109
|
|
|
public function dirNameFilter($path) |
110
|
|
|
{ |
111
|
|
|
$output = dirname($path); |
112
|
|
|
return $output; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* php parse_url() wrapper -- http://php.net/manual/en/function.parse-url.php |
|
|
|
|
117
|
|
|
* |
118
|
|
|
* @param $url |
|
|
|
|
119
|
|
|
* @param int $component |
|
|
|
|
120
|
|
|
* @return mixed |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
public function parseUrlFilter($url, $component = -1) |
123
|
|
|
{ |
124
|
|
|
$output = parse_url($url, $component); |
125
|
|
|
return $output; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* php parse_str() wrapper -- http://php.net/manual/en/function.parse-str.php |
|
|
|
|
130
|
|
|
* |
131
|
|
|
* @param $string |
|
|
|
|
132
|
|
|
* @return mixed |
|
|
|
|
133
|
|
|
*/ |
134
|
|
|
public function parseStringFilter($string) |
135
|
|
|
{ |
136
|
|
|
parse_str($string, $output); |
137
|
|
|
return $output; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Swap the file extension on a passed url or path |
142
|
|
|
* |
143
|
|
|
* @param $path_or_url |
|
|
|
|
144
|
|
|
* @param $extension |
|
|
|
|
145
|
|
|
* @return string |
|
|
|
|
146
|
|
|
*/ |
147
|
|
|
public function swapExtensionFilter($path_or_url, $extension) |
148
|
|
|
{ |
149
|
|
|
$path = $this->decomposeUrl($path_or_url); |
150
|
|
|
$path_parts = pathinfo($path['path']); |
151
|
|
|
$new_path = $path_parts['filename'] . "." . $extension; |
152
|
|
|
if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== ".") { |
153
|
|
|
$new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; |
154
|
|
|
$new_path = preg_replace('#/+#', '/', $new_path); |
155
|
|
|
} |
156
|
|
|
$output = $path['prefix'] . $new_path . $path['suffix']; |
157
|
|
|
return $output; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Swap the file directory on a passed url or path |
162
|
|
|
* |
163
|
|
|
* @param $path_or_url |
|
|
|
|
164
|
|
|
* @param $directory |
|
|
|
|
165
|
|
|
* @return string |
|
|
|
|
166
|
|
|
*/ |
167
|
|
|
public function swapDirectoryFilter($path_or_url, $directory) |
168
|
|
|
{ |
169
|
|
|
|
170
|
|
|
$path = $this->decomposeUrl($path_or_url); |
171
|
|
|
$path_parts = pathinfo($path['path']); |
172
|
|
|
$new_path = $directory . DIRECTORY_SEPARATOR . $path_parts['basename']; |
173
|
|
|
|
174
|
|
|
$output = $path['prefix'] . $new_path . $path['suffix']; |
175
|
|
|
return $output; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Append a suffix a passed url or path |
181
|
|
|
* |
182
|
|
|
* @param $path_or_url |
|
|
|
|
183
|
|
|
* @param $suffix |
|
|
|
|
184
|
|
|
* @return string |
|
|
|
|
185
|
|
|
*/ |
186
|
|
|
public function appendSuffixFilter($path_or_url, $suffix) |
187
|
|
|
{ |
188
|
|
|
$path = $this->decomposeUrl($path_or_url); |
189
|
|
|
$path_parts = pathinfo($path['path']); |
190
|
|
|
$new_path = $path_parts['filename'] . $suffix . "." . $path_parts['extension']; |
191
|
|
|
if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== ".") { |
192
|
|
|
$new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; |
193
|
|
|
$new_path = preg_replace('#/+#', '/', $new_path); |
194
|
|
|
} |
195
|
|
|
$output = $path['prefix'] . $new_path . $path['suffix']; |
196
|
|
|
return $output; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Private Methods |
200
|
|
|
// ========================================================================= |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Decompose a url into a prefix, path, and suffix |
204
|
|
|
* |
205
|
|
|
* @param $path_or_url |
|
|
|
|
206
|
|
|
* @return array |
|
|
|
|
207
|
|
|
*/ |
208
|
|
|
private function decomposeUrl($path_or_url) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
$result = array(); |
211
|
|
|
|
212
|
|
|
if (filter_var($path_or_url, FILTER_VALIDATE_URL)) { |
213
|
|
|
$url_parts = parse_url($path_or_url); |
214
|
|
|
$result['prefix'] = $url_parts['scheme'] . "://" . $url_parts['host']; |
215
|
|
|
$result['path'] = $url_parts['path']; |
216
|
|
|
$result['suffix'] = ""; |
217
|
|
|
$result['suffix'] .= (empty($url_parts['query'])) ? "" : "?" . $url_parts['query']; |
218
|
|
|
$result['suffix'] .= (empty($url_parts['fragment'])) ? "" : "#" . $url_parts['fragment']; |
219
|
|
|
} else { |
220
|
|
|
$result['prefix'] = ""; |
221
|
|
|
$result['path'] = $path_or_url; |
222
|
|
|
$result['suffix'] = ""; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $result; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|