diff --git a/composer.json b/composer.json index bd51bf5..c7f02cb 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ } ], "require": { - "nothingworks/blade-svg": "^0.2.1" + "nothingworks/blade-svg": "^0.2.1", + "wikimedia/relpath": "^2.1" }, "autoload": { "files": [ diff --git a/composer.lock b/composer.lock index 97b51ba..07e3188 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "2dbd5c7f4dc1a63ee5f289521a7f7c01", + "content-hash": "4a38c143372d165b5f3b3e2c29faa8a7", "packages": [ { "name": "doctrine/inflector", @@ -361,6 +361,49 @@ "description": "Symfony Finder Component", "homepage": "https://symfony.com", "time": "2017-06-01T21:01:25+00:00" + }, + { + "name": "wikimedia/relpath", + "version": "2.1.1", + "source": { + "type": "git", + "url": "https://github.com/wikimedia/RelPath.git", + "reference": "35e701ff16abf461bb8676a9d9177f86fa0b2c94" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/wikimedia/RelPath/zipball/35e701ff16abf461bb8676a9d9177f86fa0b2c94", + "reference": "35e701ff16abf461bb8676a9d9177f86fa0b2c94", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "require-dev": { + "jakub-onderka/php-parallel-lint": "^0.9.0.0", + "mediawiki/mediawiki-codesniffer": "15.0.0", + "phpunit/phpunit": "^4.8.9.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/RelPath/RelPath.php", + "src/Wikimedia/RelPath.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ori Livneh", + "email": "ori@wikimedia.org" + } + ], + "description": "Compute a relative filepath between two paths.", + "homepage": "https://www.mediawiki.org/wiki/RelPath", + "time": "2018-01-18T21:23:40+00:00" } ], "packages-dev": [], diff --git a/src/helpers.php b/src/helpers.php index cfcefe0..31ff544 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,6 +4,8 @@ use function App\sage; use function App\config; +use function App\asset_path; +use \Wikimedia\RelPath; /** * Get Sage's dist path @@ -11,11 +13,70 @@ * @param string $path * @return string */ -function get_dist_path($path) +function get_dist_path($path = '') { - return trailingslashit(config('theme.dir')) . trailingslashit('dist') . $path; + $dist_path = trailingslashit(config('theme.dir')); + + if(!empty($path)) { + $dist_path .= trailingslashit('dist') . $path; + } else { + $dist_path .= 'dist'; + } + + return $dist_path; +} + + +/** + * Returns path to svg dir + * + * @param string $icon + * @return string + */ +function get_svg_path() { + return apply_filters('bladesvg_image_path', get_dist_path('images/svg/icons')); +} + +/** + * Returns absolute path to icon + * + * @param string $icon + * @return string + */ +function get_icon_path_abs($icon) { + return trailingslashit(get_svg_path()) . $icon . '.svg'; } +/** + * Returns path to icon in a sage theme + * + * @param string $icon + * @return string + */ +function get_icon_sage_path($icon) +{ + // 1. Absolute path to icon + $icon_abs_path = get_icon_path_abs($icon); + + // 2. Icon path relative to sage dist dir + $icon_rel_dist = RelPath::getRelativePath($icon_abs_path, get_dist_path()); + + // 3. Icon path with proper hash relative to svg dir (itself being relative to dist dir) + $icon_asset_path = sage('assets')->get($icon_rel_dist); + + // 4. Absolute path to icon with proper hash + $icon_path_abs = RelPath::joinPath(get_dist_path(), $icon_asset_path); + + // 5. Icon path relative to dist dir + $icon_path_rel = RelPath::getRelativePath($icon_path_abs, get_svg_path()); + + // 6. Final icon path without extension expected + $icon_imgpath_noext = pathinfo($icon_path_rel, PATHINFO_FILENAME); + + return $icon_imgpath_noext; +} + + /** * Simple helper for our directive. * @@ -26,5 +87,10 @@ function get_dist_path($path) */ function svg_image($icon, $class = '', $attrs = []) { - return sage(\BladeSvg\SvgFactory::class)->svg(sage('assets')->get($icon), $class, $attrs); + // sage asset expectes relative path (production builds) + // see https://discourse.roots.io/t/asset-hashes/10261 + // see https://github.com/roots/sage-lib/blob/d7789609eae857e910812cf62ae55355b836ad58/Assets/JsonManifest.php#L33 + $icon_sage_path = get_icon_sage_path($icon); + + return sage(\BladeSvg\SvgFactory::class)->svg($icon_sage_path, $class, $attrs); }