Source for file optimizer.php
Documentation is available at optimizer.php
* GloryLands Optimizing Core
* GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
* Copyright (C) 2008-09 John Haralampidis <jïhnys2[at]gmail.cïm>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For any help/suggestions or troubleshooting you can see the
* project community website at <http://www.glorylands.gr>
* @license GNU/GPLv3 GNU General Public License version 3
* @author John Haralampidis <jïhnys2[at]gmail.cïm>
* @copyright Copyright (C) 2007-2009, John Haralampidis
* This class is the optimization core that porivdes data,
* file or optimization in order to make the overall execution
static private $optimizers;
* This function will look into the extensions_optimizers table
* and register all the optimizers that are defined inside.
while ($opt = $opts->next()) {
* Register an optimizer handler
* @todo The include_once must be performed by the appropriate extension
* @param string $file The filename that hosts the class to be instanced
* @param string $class The handler class name
static public function register($file, $class) {
// Register the optimizer
self::$optimizers[] = new $class();
* Quick optimization function
* This function will automatically detect if the passed parameter is a string
* or a file and will call the optimize_file or optimize_string accordingly.
* @param string $what The filename or the string to optimize
* @param string $type The passed file/string type (ex. 'image', 'template', 'script', ...)
* @param array $options You can optionally provide additional options for the operation (ex. quality in JPEG)
* @return GLDynamicPath This function returns a dynamic path that can be used either as URL or as abdsolute disk path
static public function optimize($what, $type= '', $options= array()) {
return self::optimize_file($what, $type, $options);
return self::optimize_string($what, $type, $options);
* Usually, this involves compiling of a PHP script, translating some short-hand expressions,
* @param string $what The filename or the string to optimize
* @param string|array $type The passed file type (ex. 'image', 'template', 'script', ...) It can also be a comma-separated list (ex. 'image,template'), or an array (ex. array('image','template') )
* @param array $options You can optionally provide additional options for the operation (ex. quality in JPEG)
* @return GLDynamicPath This function returns a dynamic path that can be used either as URL or as abdsolute disk path
static public function optimize_file($what, $type= '', $options= array()) {
foreach (self::$optimizers as $o) {
if ($o->is_valid($type, $what)) {
$what = $o->optimize_file($what, $options);
return $optimized? $what: false;
* Usually, this involves compressing, translating, encoding conversions etc.
* @param string $what The filename or the string to optimize
* @param string|array $type The passed file type (ex. 'image', 'template', 'script', ...) It can also be a comma-separated list (ex. 'image,template'), or an array (ex. array('image','template') )
* @param array $options You can optionally provide additional options for the operation (ex. quality in JPEG)
* @return string This function returns the new, optimized string
static public function optimize_string($what, $type= '', $options= array()) {
foreach (self::$optimizers as $o) {
if ($o->is_valid($type, $what)) {
$what = $o->optimize_file($what, $options);
return $optimized? $what: false;
* This abstract class is the base class of an optimizing module.
* Each optimizer should extend this.
public function is_valid($categories, $string) {
* Allocate a new file in cache
* @param string $prefix A group name that will be prefixed on the cache file
* @param string $uid [Optiona] A unique identifier for the file being optimized. If not specified, a new will be generated
* @return string Returns the new cache file path
$path = GLCache::$cache. '/optimizer';
if (!is_dir($path)) mkdir($path);
$file = $path . '/'. $prefix. '.'. urlencode($uid). '.opt';
* Check whether we should update or not the cached file
* @param string $file The path to the cache file
* @param mixed $check If this variable is FALSE, no previously existing file check will be performed; it will be replaced immediately. If this variable is an MD5 string, the file contents will be matched with the string provided. If this variable is a timestamp, the file's last modification date will be required to be newer than the provided.
* @return bool Returns TRUE if FALSE, depedning on when you should or should not update this cache file
if (is_file($file) && ($check !== false)) {
return ($hash != $check);
|