Created
December 6, 2010 16:25
-
-
Save lukemorton/730518 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Static singleton class for handling Assets | |
* | |
* @usage | |
* Asset::css('myscript.css'); | |
* | |
* Asset::css(array( | |
* 'my-script.css', | |
* 'another.css', | |
* )); | |
* | |
* Asset::js('myjsfile.js'); | |
* | |
* Asset::js('another.js'); | |
* | |
* // Compile a js file from multiple files | |
* Asset::js_group('app.js', array( | |
* 'app.main.js', | |
* 'app.additional.js', | |
* 'app.helpers.js', | |
* )); | |
* | |
* // Echo <script> and <link> | |
* echo Asset::css_includes(); | |
* echo Asset::js_includes(); | |
*/ | |
class Asset { | |
protected static $_css = array(); | |
protected static $_css_groups = array(); | |
protected static $_js = array(); | |
protected static $_js_groups = array(); | |
public static function css($file) | |
{ | |
// Add single css | |
if (is_string($file)) | |
{ | |
Asset::$_css[] = $file; | |
return; | |
} | |
// Merge array | |
Asset::$_css += $file; | |
} | |
public static function css_group($group, $file) | |
{ | |
// Add single css | |
if (is_string($file)) | |
{ | |
Asset::$_css_groups[$group][] = $file; | |
return; | |
} | |
// If not set | |
if ( ! isset(Asset::$_css_groups[$group])) | |
{ | |
Asset::$_css_groups[$group] = array(); | |
} | |
// Merge array | |
Asset::$_css_groups[$group] += $file; | |
} | |
public static function js($file) | |
{ | |
// Add single css | |
if (is_string($file)) | |
{ | |
Asset::$_js[] = $file; | |
return; | |
} | |
// Merge array | |
Asset::$_js += $file; | |
} | |
public static function js_group($group, $file) | |
{ | |
// Add single css | |
if (is_string($file)) | |
{ | |
Asset::$_js_groups[$group][] = $file; | |
return; | |
} | |
// If not set | |
if ( ! isset(Asset::$_js_groups[$group])) | |
{ | |
Asset::$_js_groups[$group] = array(); | |
} | |
// Merge array | |
Asset::$js_groups[$group] += $file; | |
} | |
public static function css_includes() | |
{ | |
// Compile groups | |
Assets::compile_groups(); | |
// Compile sass if neccessary | |
Asset::compile_less(); | |
// Output | |
$css = array(); | |
// | |
foreach (Asset::$_css as $_css) | |
{ | |
$css[] = '<link rel="stylesheet" href="'.$_css.'">'; | |
} | |
// Return css includes | |
return implode("\n", $css); | |
} | |
public static function js_includes() | |
{ | |
// Output | |
$js = array(); | |
// Compile groups | |
Assets::compile_groups(); | |
// | |
foreach (Asset::$_js as $_js) | |
{ | |
$js[] = '<script src="'.$_js.'"></script>'; | |
} | |
// Return css includes | |
return implode("\n", $js); | |
} | |
public static function compile_groups($force = FALSE) | |
{ | |
// compile time | |
} | |
public static function compile_less($force = FALSE) | |
{ | |
foreach (Asset::$css as $_k => $_css) | |
{ | |
if (strpos($_css, '.less') === FALSE) | |
{ | |
continue; | |
} | |
// New file | |
$css_output_file = str_replace('.less', '.css', $_css); | |
// Compile | |
require_once('lessc.inc.php'); | |
try | |
{ | |
lessc::ccompile($_css, $css_output_file); | |
} | |
catch (Exception $e) | |
{ | |
die($e); | |
} | |
// Replace .less for .css | |
Asset::$css[$_k] = $css_output_file; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kodizga tushinmadim