nystudio107 /
craft-pathtools
| 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 |
||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 9 | * @copyright Copyright (c) 2017 nystudio107 |
||||
|
0 ignored issues
–
show
|
|||||
| 10 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 11 | |||||
| 12 | namespace nystudio107\pathtools\twigextensions; |
||||
| 13 | |||||
| 14 | use Craft; |
||||
| 15 | |||||
| 16 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 17 | * @author nystudio107 |
||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||
| 18 | * @package PathTools |
||||
|
0 ignored issues
–
show
|
|||||
| 19 | * @since 1.0.0 |
||||
|
0 ignored issues
–
show
|
|||||
| 20 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | class PathToolsTwigExtension extends \Twig_Extension |
||||
|
0 ignored issues
–
show
The class
Twig_Extension has been deprecated: since Twig 2.7, use "Twig\Extension\AbstractExtension" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 22 | { |
||||
| 23 | // Public Methods |
||||
| 24 | // ========================================================================= |
||||
| 25 | |||||
| 26 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 27 | * @inheritdoc |
||||
| 28 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 29 | public function getName() |
||||
| 30 | { |
||||
| 31 | return 'PathTools'; |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 35 | * @inheritdoc |
||||
| 36 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 37 | public function getFilters() |
||||
| 38 | { |
||||
| 39 | return [ |
||||
| 40 | new \Twig_SimpleFilter('pathinfo', [$this, 'pathInfoFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 41 | new \Twig_SimpleFilter('basename', [$this, 'baseNameFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 42 | new \Twig_SimpleFilter('dirname', [$this, 'dirNameFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 43 | new \Twig_SimpleFilter('parse_url', [$this, 'parseUrlFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 44 | new \Twig_SimpleFilter('parse_string', [$this, 'parseStringFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 45 | new \Twig_SimpleFilter('swap_extension', [$this, 'swapExtensionFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 46 | new \Twig_SimpleFilter('swap_directory', [$this, 'swapDirectoryFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 47 | new \Twig_SimpleFilter('append_suffix', [$this, 'appendSuffixFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFilter has been deprecated: since Twig 2.7, use "Twig\TwigFilter" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 48 | ]; |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 52 | * @inheritdoc |
||||
| 53 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 54 | public function getFunctions() |
||||
| 55 | { |
||||
| 56 | return [ |
||||
| 57 | new \Twig_SimpleFunction('pathinfo', [$this, 'pathInfoFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 58 | new \Twig_SimpleFunction('basename', [$this, 'baseNameFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 59 | new \Twig_SimpleFunction('dirname', [$this, 'dirNameFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 60 | new \Twig_SimpleFunction('parse_url', [$this, 'parseUrlFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 61 | new \Twig_SimpleFunction('parse_string', [$this, 'parseStringFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 62 | new \Twig_SimpleFunction('swap_extension', [$this, 'swapExtensionFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 63 | new \Twig_SimpleFunction('swap_directory', [$this, 'swapDirectoryFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 64 | new \Twig_SimpleFunction('append_suffix', [$this, 'appendSuffixFilter']), |
||||
|
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated: since Twig 2.7, use "Twig\TwigFunction" instead
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 65 | ]; |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * php pathinfo() wrapper -- http://php.net/manual/en/function.pathinfo.php |
||||
|
0 ignored issues
–
show
|
|||||
| 71 | * |
||||
| 72 | * @param $path |
||||
|
0 ignored issues
–
show
|
|||||
| 73 | * @param bool $options |
||||
|
0 ignored issues
–
show
|
|||||
| 74 | * @return mixed |
||||
|
0 ignored issues
–
show
|
|||||
| 75 | */ |
||||
| 76 | public function pathInfoFilter($path, $options = false) |
||||
| 77 | { |
||||
| 78 | if ($options) { |
||||
| 79 | $output = pathinfo($path, $options); |
||||
|
0 ignored issues
–
show
$options of type true is incompatible with the type integer expected by parameter $flags of pathinfo().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 88 | * |
||||
| 89 | * @param $path |
||||
|
0 ignored issues
–
show
|
|||||
| 90 | * @param bool $suffix |
||||
|
0 ignored issues
–
show
|
|||||
| 91 | * @return string |
||||
|
0 ignored issues
–
show
|
|||||
| 92 | */ |
||||
| 93 | public function baseNameFilter($path, $suffix = false) |
||||
| 94 | { |
||||
| 95 | if ($suffix) { |
||||
| 96 | $output = basename($path, $suffix); |
||||
|
0 ignored issues
–
show
$suffix of type true is incompatible with the type string expected by parameter $suffix of basename().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 105 | * |
||||
| 106 | * @param $path |
||||
|
0 ignored issues
–
show
|
|||||
| 107 | * @return string |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 117 | * |
||||
| 118 | * @param $url |
||||
|
0 ignored issues
–
show
|
|||||
| 119 | * @param int $component |
||||
|
0 ignored issues
–
show
|
|||||
| 120 | * @return mixed |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 130 | * |
||||
| 131 | * @param $string |
||||
|
0 ignored issues
–
show
|
|||||
| 132 | * @return mixed |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 144 | * @param $extension |
||||
|
0 ignored issues
–
show
|
|||||
| 145 | * @return string |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 164 | * @param $directory |
||||
|
0 ignored issues
–
show
|
|||||
| 165 | * @return string |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 183 | * @param $suffix |
||||
|
0 ignored issues
–
show
|
|||||
| 184 | * @return string |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 206 | * @return array |
||||
|
0 ignored issues
–
show
|
|||||
| 207 | */ |
||||
| 208 | private function decomposeUrl($path_or_url) |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |