From 554edb1f0cbe260b62dc42c9bf3b81f7540169f3 Mon Sep 17 00:00:00 2001 From: Eike Foken Date: Fri, 2 Sep 2011 04:34:16 +0200 Subject: [PATCH] Implement a simple asset library and use it --- application/config/assets.php | 27 +++ application/config/autoload.php | 4 +- application/helpers/asset_helper.php | 41 ++++ application/libraries/Asset.php | 101 ++++++++++ application/libraries/Jsmin.php | 277 +++++++++++++++++++++++++++ application/views/footer.php | 6 +- application/views/header.php | 21 +- assets/cache/index.html | 10 + 8 files changed, 472 insertions(+), 15 deletions(-) create mode 100644 application/config/assets.php create mode 100644 application/helpers/asset_helper.php create mode 100644 application/libraries/Asset.php create mode 100644 application/libraries/Jsmin.php create mode 100755 assets/cache/index.html diff --git a/application/config/assets.php b/application/config/assets.php new file mode 100644 index 0000000..69fdba8 --- /dev/null +++ b/application/config/assets.php @@ -0,0 +1,27 @@ + + */ + +/** + * Helps generating links to asset files of any sort. + * + * Note: The type should be the name of the folder the asset is stored in. + * + * @param string $type + * @param string $filename + * @return string + */ +function asset_url($type, $filename) { + $CI = &get_instance(); + $CI->load->config('assets'); + + return $CI->config->item('base_url') . $CI->config->item('assets_folder') + . $type . '/' . $filename; +} + +/** + * Helps generating HTML for assets of any sort. + * + * @param string $type + * @param string $filename + * @param array $attributes + * @return string + */ +function asset($type, $filename, $attributes = array()) { + $CI = &get_instance(); + $CI->load->library('asset'); + + return $CI->asset->load($type, $filename, $attributes); +} + +/* End of file asset_helper.php */ +/* Location: ./application/helpers/asset_helper.php */ diff --git a/application/libraries/Asset.php b/application/libraries/Asset.php new file mode 100644 index 0000000..b7e1b10 --- /dev/null +++ b/application/libraries/Asset.php @@ -0,0 +1,101 @@ + + */ +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 = ' $value) { + $output .= " $key=\"$value\""; + } + + $output .= ' />'; + break; + case 'css': + $output = ' $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 = ''; + $output .= "\n"; + break; + default: + $output = ''; + } + + return $output; + } +} + +/* End of file Assets.php */ +/* Location: ./application/libraries/Assets.php */ diff --git a/application/libraries/Jsmin.php b/application/libraries/Jsmin.php new file mode 100644 index 0000000..c8dd9e6 --- /dev/null +++ b/application/libraries/Jsmin.php @@ -0,0 +1,277 @@ + + * @copyright 2002 Douglas Crockford (jsmin.c) + * @copyright 2008 Ryan Grove (PHP port) + * @license http://opensource.org/licenses/mit-license.php MIT License + * @version 1.1.1 (2008-03-02) + * @link http://code.google.com/p/jsmin-php/ + */ +class Jsmin { + + const ORD_LF = 10; + const ORD_SPACE = 32; + + protected $a = ''; + protected $b = ''; + protected $input = ''; + protected $inputIndex = 0; + protected $inputLength = 0; + protected $lookAhead = null; + protected $output = ''; + + /** + * Constructor. + */ + public function __construct() { + log_message('debug', "Jsmin Class Initialized"); + } + + /** + * Minifies javascript code. + * + * @param string $input + * @return string The minified code + */ + public function minify($input) { + $this->input = str_replace("\r\n", "\n", $input); + $this->inputLength = strlen($this->input); + return $this->min(); + } + + private function action($d) { + switch ($d) { + case 1: + $this->output .= $this->a; + case 2: + $this->a = $this->b; + + if ($this->a === "'" || $this->a === '"') { + for (;;) { + $this->output .= $this->a; + $this->a = $this->get(); + + if ($this->a === $this->b) { + break; + } + + if (ord($this->a) <= self::ORD_LF) { + log_message('error', "Unterminated string literal"); + return; + } + + if ($this->a === '\\') { + $this->output .= $this->a; + $this->a = $this->get(); + } + } + } + case 3: + $this->b = $this->next(); + + if ($this->b === '/' && ($this->a === '(' || $this->a === ',' || $this->a === '=' || $this->a === ':' || $this->a === '[' || $this->a === '!' || $this->a === '&' || $this->a === '|' || $this->a === '?')) { + $this->output .= $this->a . $this->b; + + for (;;) { + $this->a = $this->get(); + + if ($this->a === '/') { + break; + } else if ($this->a === '\\') { + $this->output .= $this->a; + $this->a = $this->get(); + } else if (ord($this->a) <= self::ORD_LF) { + log_message('error', "Unterminated regular expression literal"); + return; + } + + $this->output .= $this->a; + } + + $this->b = $this->next(); + } + } + } + + private function get() { + $c = $this->lookAhead; + $this->lookAhead = null; + + if ($c === null) { + if ($this->inputIndex < $this->inputLength) { + $c = $this->input[$this->inputIndex]; + $this->inputIndex += 1; + } else { + $c = null; + } + } + + if ($c === "\r") { + return "\n"; + } + + if ($c === null || $c === "\n" || ord($c) >= self::ORD_SPACE) { + return $c; + } + + return ' '; + } + + private function isAlphaNum($c) { + return ord($c) > 126 || $c === '\\' || preg_match('/^[\w\$]$/', $c) === 1; + } + + private function min() { + $this->a = "\n"; + $this->action(3); + + while ($this->a !== null) { + switch ($this->a) { + case ' ': + if ($this->isAlphaNum($this->b)) { + $this->action(1); + } else { + $this->action(2); + } + break; + case "\n": + switch ($this->b) { + case '{': + case '[': + case '(': + case '+': + case '-': + $this->action(1); + break; + case ' ': + $this->action(3); + break; + default: + if ($this->isAlphaNum($this->b)) { + $this->action(1); + } else { + $this->action(2); + } + } + break; + default: + switch ($this->b) { + case ' ': + if ($this->isAlphaNum($this->a)) { + $this->action(1); + break; + } + + $this->action(3); + break; + case "\n": + switch ($this->a) { + case '}': + case ']': + case ')': + case '+': + case '-': + case '"': + case "'": + $this->action(1); + break; + default: + if ($this->isAlphaNum($this->a)) { + $this->action(1); + } else { + $this->action(3); + } + } + break; + default: + $this->action(1); + break; + } + } + } + + return $this->output; + } + + private function next() { + $c = $this->get(); + + if ($c === '/') { + switch ($this->peek()) { + case '/': + for (;;) { + $c = $this->get(); + + if (ord($c) <= self::ORD_LF) { + return $c; + } + } + case '*': + $this->get(); + + for (;;) { + switch ($this->get()) { + case '*': + if ($this->peek() === '/') { + $this->get(); + return ' '; + } + break; + + case null: + throw new JSMinException('Unterminated comment.'); + } + } + default: + return $c; + } + } + + return $c; + } + + private function peek() { + $this->lookAhead = $this->get(); + return $this->lookAhead; + } +} + +/* End of file Jsmin.php */ +/* Location: ./application/libraries/Jsmin.php */ diff --git a/application/views/footer.php b/application/views/footer.php index 6775fda..50347ae 100644 --- a/application/views/footer.php +++ b/application/views/footer.php @@ -17,9 +17,9 @@ - 'assets/images/iwt.gif', 'width' => 36));?> - 'assets/images/dfg.gif', 'width' => 36));?> - 'assets/images/uni.png', 'width' => 36));?> + 36));?> + 36));?> + 36));?> diff --git a/application/views/header.php b/application/views/header.php index 8a65e7e..a82e2da 100644 --- a/application/views/header.php +++ b/application/views/header.php @@ -7,26 +7,27 @@ ScattPort - - - + + + + + + + + + - - - - - - +