Implement a simple asset library and use it

This commit is contained in:
Eike Foken
2011-09-02 04:34:16 +02:00
parent 20a1ea85b3
commit 554edb1f0c
8 changed files with 472 additions and 15 deletions

View File

@@ -0,0 +1,27 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| Asset storage
|--------------------------------------------------------------------------
|
| The path to where the assets are stored, WITH trailing slash.
|
*/
$config['assets_folder'] = 'assets/';
$config['cache_folder'] = $config['assets_folder'] . 'cache/';
/*
|--------------------------------------------------------------------------
| Toggle CSSTidy/JSMin compression
|--------------------------------------------------------------------------
|
| Whether to run compression using CSSTidy or JSMin.
|
*/
$config['enable_csstidy'] = true;
$config['enable_jsmin'] = true;
/* End of file assets.php */
/* Location: ./application/config/assets.php */

View File

@@ -1,4 +1,4 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); <?php defined('BASEPATH') || exit('No direct script access allowed');
/* /*
| ------------------------------------------------------------------- | -------------------------------------------------------------------
| AUTO-LOADER | AUTO-LOADER
@@ -64,7 +64,7 @@ $autoload['libraries'] = array('session', 'lang_detect', 'database', 'access', '
| $autoload['helper'] = array('url', 'file'); | $autoload['helper'] = array('url', 'file');
*/ */
$autoload['helper'] = array('date', 'url', 'html', 'language', 'hash'); $autoload['helper'] = array('date', 'url', 'form', 'language', 'hash', 'asset');
/* /*

View File

@@ -0,0 +1,41 @@
<?php defined('BASEPATH') || exit('No direct script access allowed');
/**
* Helper functions for including assets.
*
* @author Eike Foken <kontakt@eikefoken.de>
*/
/**
* 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 */

View File

@@ -0,0 +1,101 @@
<?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,277 @@
<?php
/**
* Jsmin.php - PHP implementation of Douglas Crockford's JSMin.
*
* This is pretty much a direct port of jsmin.c to PHP with just a few
* PHP-specific performance tweaks. Also, whereas jsmin.c reads from stdin and
* outputs to stdout, this library accepts a string as input and returns another
* string as output.
*
* PHP 5 or higher is required.
*
* Permission is hereby granted to use this version of the library under the
* same terms as jsmin.c, which has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --
*
* @package CodeIgniter
* @author Ryan Grove <ryan@wonko.com>
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (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 */

View File

@@ -17,9 +17,9 @@
<?=anchor('legal', _('License'));?> <?=anchor('legal', _('License'));?>
</span> </span>
<span class="right"> <span class="right">
<?=img(array('src' => 'assets/images/iwt.gif', 'width' => 36));?> <?=asset('image', 'iwt.gif', array('width' => 36));?>
<?=img(array('src' => 'assets/images/dfg.gif', 'width' => 36));?> <?=asset('image', 'dfg.gif', array('width' => 36));?>
<?=img(array('src' => 'assets/images/uni.png', 'width' => 36));?> <?=asset('image', 'uni.png', array('width' => 36));?>
</span> </span>
</div> </div>

View File

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

10
assets/cache/index.html vendored Executable file
View File

@@ -0,0 +1,10 @@
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>