Improve asset library

This commit is contained in:
Eike Foken
2011-09-04 23:43:32 +02:00
parent 43cf335ffc
commit 5ea117aeb8
4 changed files with 194 additions and 117 deletions

View File

@@ -16,10 +16,9 @@
*/ */
function asset_url($type, $filename) { function asset_url($type, $filename) {
$CI = &get_instance(); $CI = &get_instance();
$CI->load->config('assets'); $CI->load->library('assets');
return $CI->config->item('base_url') . $CI->config->item('assets_folder') return $CI->assets->url($type, $filename);
. $type . '/' . $filename;
} }
/** /**
@@ -27,14 +26,46 @@ function asset_url($type, $filename) {
* *
* @param string $type * @param string $type
* @param string $filename * @param string $filename
* @param array $attributes * @param mixed $attributes
* @return string * @return string
*/ */
function asset($type, $filename, $attributes = array()) { function asset($type, $filename, $attributes = '') {
$CI = &get_instance(); $CI = &get_instance();
$CI->load->library('asset'); $CI->load->library('assets');
return $CI->asset->load($type, $filename, $attributes); return $CI->assets->load($type, $filename, $attributes);
}
/**
* Inserts CSS assets.
*
* @param string $filename
* @param mixed $attributes
* @return string
*/
function css_asset($filename, $attributes = '') {
return asset('css', $filename, $attributes);
}
/**
* Inserts image assets.
*
* @param string $filename
* @param mixed $attributes
* @return string
*/
function image_asset($filename, $attributes = '') {
return asset('image', $filename, $attributes);
}
/**
* Inserts javascript assets.
*
* @param string $filename
* @return string
*/
function js_asset($filename) {
return asset('js', $filename);
} }
/* End of file asset_helper.php */ /* End of file asset_helper.php */

View File

@@ -1,101 +0,0 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/**
*
* @author Eike Foken <kontakt@eikefoken.de>
*/
class Asset {
private $assetsFolder;
private $cacheFolder;
private $CI;
/**
* Constructor.
*/
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->config('assets');
$this->assetsFolder = $this->CI->config->item('assets_folder');
$this->cacheFolder = $this->CI->config->item('cache_folder');
log_message('debug', "Asset Class Initialized");
}
/**
* Loads an asset.
*
* @param string $type
* @param string $filename
*/
public function load($type, $filename, $attributes = array()) {
// validate type parameter
if (!empty($type)) {
$type = strtolower($type);
if (!in_array($type, array('image', 'images', 'icon', 'icons', 'css', 'js'))) {
log_message('error', "Invalid asset type '" . $type . "' used.");
}
if (in_array($type, array('image', 'icon'))) {
$type .= 's';
}
}
// convert filename to array
if (!empty($filename)) {
$link = $this->CI->config->slash_item('base_url') . $this->assetsFolder . $type . '/' . $filename;
$path = FCPATH . $this->assetsFolder . $type . '/' . $filename;
}
switch ($type) {
case 'images':
case 'icons':
if (!isset($attributes['alt'])) {
$attributes['alt'] = '';
}
$output = '<img src="' . $link . '"';
foreach ($attributes as $key => $value) {
$output .= " $key=\"$value\"";
}
$output .= ' />';
break;
case 'css':
$output = '<link href="' . $link . '" rel="stylesheet" type="text/css"';
foreach ($attributes as $key => $value) {
$output .= " $key=\"$value\"";
}
$output .= " />\n";
break;
case 'js':
// $code = file_get_contents(FCPATH . $this->assetsFolder . $type . '/' . $filename);
// if ($this->CI->config->item('enable_jsmin') == true) {
// $this->CI->load->library('jsmin');
// $code = $this->CI->jsmin->minify($code);
// }
// $httpHeaders = '<' . '?php header("Content-type: text/javascript; charset: UTF-8");?' . '>';
// if ($this->CI->config->item('compress_output') == true) {
// $httpHeaders .= '';
// }
// $code = $httpHeaders . "\n" . $code;
// file_put_contents(FCPATH . $this->cacheFolder . $type . '_' . md5($code) . EXT, $code);
$output = '<script src="' . $link . '" language="javascript" type="text/javascript"></script>';
$output .= "\n";
break;
default:
$output = '';
}
return $output;
}
}
/* End of file Assets.php */
/* Location: ./application/libraries/Assets.php */

View File

@@ -0,0 +1,146 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/**
*
* @author Eike Foken <kontakt@eikefoken.de>
*/
class Assets {
private $assetsFolder;
private $cacheFolder;
private $css = array();
private $javascript = array();
private $CI;
/**
* Constructor.
*/
public function __construct($config = array()) {
log_message('debug', "Asset Class Initialized");
// set the super object to a local variable
$this->CI =& get_instance();
// set the asset path
$this->assetsPath = ($config['assets_folder'] != '') ? $config['assets_folder'] : 'assets';
if (!is_dir($this->assetsPath)) {
log_message('error', "Asset folder does not exist");
}
// set the cache path
$this->cachePath = ($config['cache_folder'] != '') ? $config['cache_folder'] : 'assets/cache';
if (!is_dir($this->cachePath) || !is_really_writable($this->cachePath)) {
$this->cacheEnable = false;
}
}
/**
* Loads an asset.
*
* @param string $type
* @param string $filename
* @param mixed $attributes
*/
public function load($type, $filename, $attributes = '') {
// validate type parameter
$type = strtolower($type);
if (!in_array($type, array('image', 'images', 'icon', 'icons', 'css', 'js'))) {
log_message('error', "Invalid asset type '" . $type . "' used.");
}
if (in_array($type, array('image', 'icon'))) {
$type .= 's';
}
// build the path strings
$filepath = $this->assetsPath . $type . '/' . $filename;
if (!file_exists($filepath)) {
log_message('error', "Unable to load requested asset: $filename");
return '';
}
switch ($type) {
case 'images':
case 'icons':
if (!isset($attributes['alt'])) {
$attributes['alt'] = '';
}
$output = '<img src="' . $this->CI->config->slash_item('base_url') . $filepath . '"' . $this->parseAttributes($attributes) . ' />';
break;
case 'css':
$output = '<link href="' . $this->CI->config->slash_item('base_url') . $filepath . '" rel="stylesheet" type="text/css"' . $this->parseAttributes($attributes) . " />\n";
break;
case 'js':
// $this->javascript[] = array('filepath' => $filepath, 'attributes' => $attributes);
// $code = file_get_contents(FCPATH . $this->assetsFolder . $type . '/' . $filename);
// if ($this->CI->config->item('enable_jsmin') == true) {
// $this->CI->load->library('jsmin');
// $code = $this->CI->jsmin->minify($code);
// }
// $httpHeaders = '<' . '?php header("Content-type: text/javascript; charset: UTF-8");?' . '>';
// if ($this->CI->config->item('compress_output') == true) {
// $httpHeaders .= '';
// }
// $code = $httpHeaders . "\n" . $code;
// file_put_contents(FCPATH . $this->cacheFolder . $type . '_' . md5($code) . EXT, $code);
$output = '<script src="' . $this->CI->config->slash_item('base_url') . $filepath . '" language="javascript" type="text/javascript"></script>';
$output .= "\n";
break;
default:
$output = '';
}
return $output;
}
/**
* Generates an URL of the given asset.
*
* @param string $type
* @param string $filename
* @return string
*/
public function url($type, $filename) {
$type = strtolower($type);
if (!in_array($type, array('image', 'images', 'icon', 'icons', 'css', 'js'))) {
log_message('error', "'" . $type . "' is not a valid asset type");
return;
}
if (in_array($type, array('image', 'icon'))) {
$type .= 's';
}
return $this->CI->config->slash_item('base_url') . $this->assetsPath . $type . '/' . $filename;
}
/**
* Parses out the attributes.
*
* @param mixed $attributes
* @return string
*/
private function parseAttributes($attributes) {
if (is_string($attributes)) {
return ($attributes != '') ? ' '.$attributes : '';
}
$output = '';
foreach ($attributes as $key => $value) {
$output .= ' ' . $key . '="' . $value . '"';
}
return $output;
}
}
/* End of file Assets.php */
/* Location: ./application/libraries/Assets.php */

View File

@@ -7,15 +7,16 @@
<title>ScattPort</title> <title>ScattPort</title>
<?=asset('css', 'style.css');?> <?=css_asset('style.css');?>
<?=asset('css', 'table.css');?> <?=css_asset('table.css');?>
<?=asset('css', 'form.css');?> <?=css_asset('form.css');?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" language="javascript" type="text/javascript"></script> <?=script_tag('https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js');?>
<?=asset('js', 'tablednd.jquery.js');?>
<?=asset('js', 'jsc3d.min.js');?> <?=js_asset('tablednd.jquery.js');?>
<?=asset('js', 'scattport.js');?> <?=js_asset('jsc3d.min.js');?>
<?=asset('js', 'jtip.js');?> <?=js_asset('scattport.js');?>
<?=js_asset('jtip.js');?>
<script type="text/javascript"> <script type="text/javascript">
var SITE_URL = '<?=site_url();?>'; var SITE_URL = '<?=site_url();?>';
@@ -27,7 +28,7 @@
<body> <body>
<div id="header"> <div id="header">
<h1><?=anchor('', asset('image', 'logo.png'));?></h1> <h1><?=anchor('', image_asset('logo.png'));?></h1>
<div class="status"> <div class="status">
<select name="activeProject"> <select name="activeProject">
<?php <?php