Last active
August 19, 2016 06:42
-
-
Save kosmiq/f98f419206d8413c874258ec3f5a69dd to your computer and use it in GitHub Desktop.
Roots Sage 8.4.2 PHP 5.3 assets loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Roots Sage 8.4.2 Assets loader for PHP 5.3 in the off case that you have to be compatible with such old versions. | |
* Remember to replace [] arrays with array() in other files. And change short echo <?= to <?php echo. | |
* Other than that Sage is compatible with PHP 5.3 | |
*/ | |
namespace Roots\Sage\Assets; | |
/** | |
* Get paths for assets | |
*/ | |
class JsonManifest { | |
private $manifest; | |
public function __construct($manifest_path) { | |
if (file_exists($manifest_path)) { | |
$this->manifest = json_decode(file_get_contents($manifest_path), true); | |
} else { | |
$this->manifest = array(); | |
} | |
} | |
public function get() { | |
return $this->manifest; | |
} | |
public function getPath($key = '', $default = null) { | |
$collection = $this->manifest; | |
if (is_null($key)) { | |
return $collection; | |
} | |
if (isset($collection[$key])) { | |
return $collection[$key]; | |
} | |
foreach (explode('.', $key) as $segment) { | |
if (!isset($collection[$segment])) { | |
return $default; | |
} else { | |
$collection = $collection[$segment]; | |
} | |
} | |
return $collection; | |
} | |
} | |
function asset_path($filename) { | |
$dist_path = get_template_directory_uri() . '/dist/'; | |
$directory = dirname($filename) . '/'; | |
$file = basename($filename); | |
static $manifest; | |
if (empty($manifest)) { | |
$manifest_path = get_template_directory() . '/dist/' . 'assets.json'; | |
$manifest = new JsonManifest($manifest_path); | |
} | |
if (array_key_exists($file, $manifest->get())) { | |
$tmp = $manifest->get(); | |
return $dist_path . $directory . $tmp[$file]; | |
} else { | |
return $dist_path . $directory . $file; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment