Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define ( '__ABSPATH__', dirname ( __FILE__ ) . '/' );
- define ( '__CSSPATH__', 'css/' );
- define ( '__JSPATH__', 'js/' );
- define ( '__LENGTH__', 60 * 60 * 24 * 14 );
- class Resources
- {
- private $packages = array ( );
- public function __construct ( )
- {
- $xml = simplexml_load_file("package-config.xml");
- foreach ( $xml -> package as $pckg )
- {
- $type = (string) $pckg [ 'type' ];
- $name = (string) $pckg [ 'name' ];
- $minify = $pckg [ 'minify' ];
- foreach ( $pckg -> file as $file )
- {
- $this -> packages [ $type ] [ $name ] [ 'files' ] [ ] = $file [ 'url' ];
- }
- $this -> packages [ $type ] [ $name ] [ 'minify' ] = $minify;
- }
- }
- public function load_package ( $key, $cache = true )
- {
- if ( ! preg_match ( "/^(\w+\.(?:css|js))$/", $key ) )
- {
- show_404 ( );
- }
- $key_bits = explode ( ".", $key );
- $pckg = $key_bits [ 0 ];
- $type = $key_bits [ 1 ];
- if ( ! isset ( $this -> packages [ $type ] [ $pckg ] )
- ||
- ! is_array ( $this -> packages [ $type ] [ $pckg ] ) )
- {
- show_404 ( );
- }
- $content = array ( );
- foreach ( $this -> packages [ $type ] [ $pckg ] [ 'files' ] as $file )
- {
- $file_path = ( ( $type == 'css' ) ? __CSSPATH__ : __JSPATH__ ) . $file;
- if ( ! file_exists ( $file_path ) )
- {
- show_404();
- }
- $content [ ] = file_get_contents ( $file_path );
- }
- if ( $this -> packages [ $type ] [ $pckg ] [ 'minify' ] == 'true' )
- {
- switch ($type)
- {
- case 'css':
- foreach ( $content as $k => $v )
- {
- $content [ $k ] = $this -> minify_css ( $v );
- }
- break;
- case 'js':
- foreach ( $content as $k => $v )
- {
- $content [ $k ] = $this -> minify_js ( $v );
- }
- break;
- }
- }
- $this -> dump_output (
- implode ( ' ', $content ),
- get_mime_by_extension ( $type ),
- $cache
- );
- }
- function dump_output ( $output, $mime, $cache = true )
- {
- header ( 'Content-Type: ' . $mime );
- if ( $cache === true )
- {
- header ( 'Pragma: public' );
- header ( 'Cache-Control: maxage=' . __LENGTH__ );
- header ( 'Expires: ' . gmdate ( 'D, d M Y H:i:s', time ( ) + __LENGTH__ ) . ' GMT' );
- }
- die ( $output );
- }
- public function minify_css ( $css )
- {
- // Compress whitespace.
- $css = preg_replace ( '/\s+/', '', $css );
- // Remove comments.
- $css = preg_replace ( '/\/\*.*?\*\//', '', $css );
- return trim ( $css );
- }
- public function minify_js ( $js )
- {
- require_once ( "jsmin.php" );
- return ( JSMin :: minify ( $js ) );
- }
- }
- function show_404 ( )
- {
- header ( 'HTTP/1.0 404 Not Found' );
- die ( "404, Not found" );
- }
- function get_mime_by_extension ( $ext )
- {
- $mimes = array (
- 'js' => 'application/x-javascript',
- 'css' => 'text/css',
- );
- return ( ( isset ( $mimes [ $ext ] ) ) ? $mimes [ $ext ] : false );
- }
- ?>
Add Comment
Please, Sign In to add comment