├── src ├── translations │ └── en │ │ └── path-tools.php ├── PathTools.php ├── icon.svg └── twigextensions │ └── PathToolsTwigExtension.php ├── CHANGELOG.md ├── composer.json ├── LICENSE.md └── README.md /src/translations/en/path-tools.php: -------------------------------------------------------------------------------- 1 | '{name} plugin loaded' 19 | ]; 20 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # PathTools Changelog 2 | 3 | ## 4.0.0-beta.1 - 2022.02.25 4 | 5 | ### Added 6 | 7 | * Initial Craft CMS 4 compatibility 8 | 9 | ## 1.0.7 - 2018.02.01 10 | ### Added 11 | * Renamed the composer package name to `craft-pathtools` 12 | 13 | ## 1.0.6 - 2017.12.06 14 | ### Changed 15 | * Updated to require craftcms/cms `^3.0.0-RC1` 16 | * Switched to `Craft::$app->view->registerTwigExtension` to register the Twig extension 17 | 18 | ## 1.0.5 - 2017.07.15 19 | ### Changed 20 | * Craft 3 beta 20 compatibility 21 | 22 | ## 1.0.4 - 2017.03.24 23 | ### Changed 24 | * `hasSettings` -> `hasCpSettings` for Craft 3 beta 8 compatibility 25 | 26 | ## 1.0.3 - 2017.03.13 27 | ### Fixed 28 | * The handle used for translations is now all lowercase 29 | 30 | ## 1.0.2 - 2017.03.12 31 | ### Added 32 | * Added `craft/cms` as a composer dependency 33 | * Added code inspection typehinting for the plugin & services 34 | 35 | ## 1.0.1 - 2017.02.17 36 | ### Changed 37 | * Code cleanup 38 | * Added a new colored icon 39 | 40 | ## 1.0.0 - 2017.02.14 41 | ### Added 42 | - Initial release 43 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nystudio107/craft-pathtools", 3 | "description": "This twig plugin for the Craft CMS brings convenient path & url manipulation functions & filters to your Twig templates.", 4 | "type": "craft-plugin", 5 | "version": "4.0.0-beta.1", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "pathtools" 12 | ], 13 | "support": { 14 | "docs": "https://nystudio107.com/docs/pathtools/", 15 | "issues": "https://nystudio107.com/plugins/pathtools/support", 16 | "source": "https://github.com/nystudio107/craft-pathtools" 17 | }, 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "nystudio107", 22 | "homepage": "https://nystudio107.com" 23 | } 24 | ], 25 | "require": { 26 | "craftcms/cms": "^4.0.0-beta.1" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "nystudio107\\pathtools\\": "src/" 31 | } 32 | }, 33 | "extra": { 34 | "class": "nystudio107\\pathtools\\PathTools", 35 | "handle": "path-tools", 36 | "name": "PathTools" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 nystudio107 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) 2 | 3 | # DEPRECATED 4 | 5 | This Craft CMS plugin is no longer supported or maintained, but it is fully functional, and you may continue to use it as you see fit. The license also allows you to fork it and make changes as needed for legacy support reasons. 6 | 7 | # PathTools plugin for Craft CMS 4.x 8 | 9 | This twig plugin for the Craft CMS brings convenient path & url manipulation functions & filters to your Twig templates. 10 | 11 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 12 | 13 | ## Requirements 14 | 15 | This plugin requires Craft CMS 4.0.0 or later. 16 | 17 | ## Installation 18 | 19 | To install PathTools, follow these steps: 20 | 21 | 1. Install with Composer via `composer require nystudio107/craft-pathtools` from your project directory 22 | 2. Install the plugin via `./craft install/plugin path-tools` via the CLI, or in the Control Panel, go to Settings → Plugins and click the “Install” button for Path Tools. 23 | 24 | You can also install PathTools via the **Plugin Store** in the Craft AdminCP. 25 | 26 | ## Documentation 27 | 28 | Click here -> [Path Tools Documentation](https://nystudio107.com/plugins/pathtools/documentation) 29 | 30 | Brought to you by [nystudio107](https://nystudio107.com) 31 | -------------------------------------------------------------------------------- /src/PathTools.php: -------------------------------------------------------------------------------- 1 | view->registerTwigExtension(new PathToolsTwigExtension()); 63 | Craft::info( 64 | Craft::t( 65 | 'path-tools', 66 | '{name} plugin loaded', 67 | ['name' => $this->name] 68 | ), 69 | __METHOD__ 70 | ); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 49 | 52 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /src/twigextensions/PathToolsTwigExtension.php: -------------------------------------------------------------------------------- 1 | decomposeUrl($path_or_url); 152 | $path_parts = pathinfo($path['path']); 153 | $new_path = $path_parts['filename'] . "." . $extension; 154 | if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== ".") { 155 | $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; 156 | $new_path = preg_replace('#/+#', '/', $new_path); 157 | } 158 | $output = $path['prefix'] . $new_path . $path['suffix']; 159 | return $output; 160 | } 161 | 162 | /** 163 | * Swap the file directory on a passed url or path 164 | * 165 | * @param $path_or_url 166 | * @param $directory 167 | * @return string 168 | */ 169 | public function swapDirectoryFilter($path_or_url, $directory) 170 | { 171 | 172 | $path = $this->decomposeUrl($path_or_url); 173 | $path_parts = pathinfo($path['path']); 174 | $new_path = $directory . DIRECTORY_SEPARATOR . $path_parts['basename']; 175 | 176 | $output = $path['prefix'] . $new_path . $path['suffix']; 177 | return $output; 178 | } 179 | 180 | 181 | /** 182 | * Append a suffix a passed url or path 183 | * 184 | * @param $path_or_url 185 | * @param $suffix 186 | * @return string 187 | */ 188 | public function appendSuffixFilter($path_or_url, $suffix) 189 | { 190 | $path = $this->decomposeUrl($path_or_url); 191 | $path_parts = pathinfo($path['path']); 192 | $new_path = $path_parts['filename'] . $suffix . "." . $path_parts['extension']; 193 | if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== ".") { 194 | $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path; 195 | $new_path = preg_replace('#/+#', '/', $new_path); 196 | } 197 | $output = $path['prefix'] . $new_path . $path['suffix']; 198 | return $output; 199 | } 200 | 201 | // Private Methods 202 | // ========================================================================= 203 | 204 | /** 205 | * Decompose a url into a prefix, path, and suffix 206 | * 207 | * @param $path_or_url 208 | * @return array 209 | */ 210 | private function decomposeUrl($path_or_url) 211 | { 212 | $result = array(); 213 | 214 | if (filter_var($path_or_url, FILTER_VALIDATE_URL)) { 215 | $url_parts = parse_url($path_or_url); 216 | $result['prefix'] = $url_parts['scheme'] . "://" . $url_parts['host']; 217 | $result['path'] = $url_parts['path']; 218 | $result['suffix'] = ""; 219 | $result['suffix'] .= (empty($url_parts['query'])) ? "" : "?" . $url_parts['query']; 220 | $result['suffix'] .= (empty($url_parts['fragment'])) ? "" : "#" . $url_parts['fragment']; 221 | } else { 222 | $result['prefix'] = ""; 223 | $result['path'] = $path_or_url; 224 | $result['suffix'] = ""; 225 | } 226 | 227 | return $result; 228 | } 229 | } 230 | --------------------------------------------------------------------------------