Skip to content

Commit

Permalink
Merge pull request #12 from strarsis/sage-paths
Browse files Browse the repository at this point in the history
Add proper sage path handling.
  • Loading branch information
Log1x authored Feb 6, 2018
2 parents e764ca0 + 0ccb18c commit c41f118
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"nothingworks/blade-svg": "^0.2.1"
"nothingworks/blade-svg": "^0.2.1",
"wikimedia/relpath": "^2.1"
},
"autoload": {
"files": [
Expand Down
45 changes: 44 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 69 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,79 @@

use function App\sage;
use function App\config;
use function App\asset_path;
use \Wikimedia\RelPath;

/**
* Get Sage's dist path
*
* @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.
*
Expand All @@ -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);
}

0 comments on commit c41f118

Please sign in to comment.