Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Commit

Permalink
feat(lib/Utils): add getContents method to Asset Util (#222)
Browse files Browse the repository at this point in the history
allows getting the contents of an asset (for example an SVG file) easily using a relative path from
the theme directory, even if it's been revved
  • Loading branch information
Doğa Gürdal committed Jun 13, 2017
1 parent b2b69ac commit 0c06ba3
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions lib/Utils/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Asset
*
* @param string $asset The filename of the required asset.
*
* @return string/boolean Returns the url or false if the asset is not found.
* @return string|boolean Returns the url or false if the asset is not found.
*/
public static function requireUrl($asset)
{
Expand All @@ -38,7 +38,7 @@ public static function requireUrl($asset)
*
* @param string $asset The filename of the required asset.
*
* @return string/boolean Returns the absolute path or false if the asset is not found.
* @return string|boolean Returns the absolute path or false if the asset is not found.
*/
public static function requirePath($asset)
{
Expand Down Expand Up @@ -106,6 +106,42 @@ public static function enqueue($options)
return self::add('enqueue', $options);
}

/**
* Getter and setter for the loadFromCdn setting.
*
* @param boolean $load (optional) Value to set the parameter to.
*
* @since %%NEXT_VERSION%%
*/
public static function loadFromCdn($load = null)
{
if (!isset($load)) {
return self::$loadFromCdn;
}
self::$loadFromCdn = (bool) $load;
}

/**
* Gets the contents of an asset.
*
* Useful for loading SVGs or other files inline.
*
* @since %%NEXT_VERSION%%
*
* @param string $asset Asset path (relative to the theme directory).
* @return string|boolean Returns the file contents or false in case of failure.
*/
public static function getContents($asset)
{
$file = self::requirePath($asset);
if (file_exists($file)) {
return file_get_contents($file);
} else {
trigger_error("File not found at: ${asset}", E_USER_WARNING);
return false;
}
}

protected static function get($returnType, $asset)
{
$distPath = get_template_directory() . '/dist';
Expand Down Expand Up @@ -209,19 +245,4 @@ protected static function add($funcType, $options)

return false;
}

/**
* Getter and setter for the loadFromCdn setting.
*
* @param boolean $load (optional) Value to set the parameter to.
*
* @since %%NEXT_VERSION%%
*/
public static function loadFromCdn($load = null)
{
if (!isset($load)) {
return self::$loadFromCdn;
}
self::$loadFromCdn = (bool) $load;
}
}

0 comments on commit 0c06ba3

Please sign in to comment.