phpDocumentor GloryLands
Common
[ class tree: GloryLands ] [ index: GloryLands ] [ all elements ]

Source for file optimizer.php

Documentation is available at optimizer.php

  1. <?php
  2. /**
  3.   * GloryLands Optimizing Core
  4.   *
  5.   * <pre>
  6.   * GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
  7.   * Copyright (C) 2008-09  John Haralampidis <jïhnys2[at]gmail.cïm>
  8.   *
  9.   * This program is free software: you can redistribute it and/or modify
  10.   * it under the terms of the GNU General Public License as published by
  11.   * the Free Software Foundation, either version 3 of the License, or
  12.   * (at your option) any later version.
  13.   *
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   * GNU General Public License for more details.
  18.   *
  19.   * You should have received a copy of the GNU General Public License
  20.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.   *
  22.   * For any help/suggestions or troubleshooting you can see the
  23.   * project community website at <http://www.glorylands.gr>
  24.   * </pre>
  25.   *
  26.   * @license GNU/GPLv3 GNU General Public License version 3
  27.   * @package GloryLands
  28.   * @subpackage Common
  29.   * @author John Haralampidis <jïhnys2[at]gmail.cïm>
  30.   * @copyright Copyright (C) 2007-2009, John Haralampidis
  31.   * @version 1.0
  32.   */
  33.  
  34. /**
  35.   * File/Data optimizer
  36.   *
  37.   * This class is the optimization core that porivdes data,
  38.   * file or optimization in order to make the overall execution
  39.   * fast and light.
  40.   *
  41.   * @subpackage Common
  42.   * @version 1.0
  43.   */
  44. class GLOptimizer {
  45.     
  46.     static private $optimizers;
  47.     
  48.     /**
  49.       * Initialize optimizer
  50.       *
  51.       * This function will look into the extensions_optimizers table
  52.       * and register all the optimizers that are defined inside.
  53.       */
  54.     static public function initialize({                
  55.         $opts new GLDBOrderedConfig('extensions_optimizers');
  56.         while ($opt $opts->next()) {
  57.             GLOptimizer::register(GLConfig::$root->file($opt->file)$opt->class);
  58.             
  59.         }
  60.     }
  61.  
  62.     /**
  63.       * Register an optimizer handler
  64.       *
  65.       * @todo The include_once must be performed by the appropriate extension
  66.       *
  67.       * @param    string    $file    The filename that hosts the class to be instanced
  68.       * @param    string    $class    The handler class name
  69.       */
  70.     static public function register($file$class{
  71.         include_once $file;
  72.         
  73.         // Register the optimizer
  74.         self::$optimizers[new $class();
  75.     }
  76.     
  77.     /**
  78.       * Quick optimization function
  79.       * This function will automatically detect if the passed parameter is a string
  80.       * or a file and will call the optimize_file or optimize_string accordingly.
  81.       *
  82.       * @param    string    $what        The filename or the string to optimize
  83.       * @param    string    $type        The passed file/string type (ex. 'image', 'template', 'script', ...)
  84.       * @param    array    $options    You can optionally provide additional options for the operation (ex. quality in JPEG)
  85.       * @return    GLDynamicPath        This function returns a dynamic path that can be used either as URL or as abdsolute disk path
  86.       */
  87.     static public function optimize($what$type=''$options=array()) {
  88.         if (is_file($what)) {
  89.             return self::optimize_file($what$type$options);
  90.         else {
  91.             return self::optimize_string($what$type$options);
  92.         }
  93.     }
  94.     
  95.     /**
  96.       * Optimize a file
  97.       * Usually, this involves compiling of a PHP script, translating some short-hand expressions,
  98.       * compressing, or
  99.       *
  100.       * @param    string            $what        The filename or the string to optimize
  101.       * @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') )
  102.       * @param    array            $options    You can optionally provide additional options for the operation (ex. quality in JPEG)
  103.       * @return    GLDynamicPath    This function returns a dynamic path that can be used either as URL or as abdsolute disk path
  104.       */
  105.     static public function optimize_file($what$type=''$options=array()) {
  106.         if (is_string($type)) $type=explode(',',$type);
  107.         $optimized false;
  108.         foreach (self::$optimizers as $o{
  109.             if ($o->is_valid($type$what)) {            
  110.                 $what $o->optimize_file($what$options);
  111.                 $optimized true;
  112.             }
  113.         }
  114.         return $optimized?$what:false;
  115.     }
  116.  
  117.     /**
  118.       * Optimize a string
  119.       * Usually, this involves compressing, translating, encoding conversions etc.
  120.       *
  121.       * @param    string            $what        The filename or the string to optimize
  122.       * @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') )
  123.       * @param    array            $options    You can optionally provide additional options for the operation (ex. quality in JPEG)
  124.       * @return    string            This function returns the new, optimized string
  125.       */
  126.     static public function optimize_string($what$type=''$options=array()) {
  127.         if (is_string($type)) $type=explode(',',$type);
  128.         $optimized false;
  129.         foreach (self::$optimizers as $o{
  130.             if ($o->is_valid($type$what)) {            
  131.                 $what $o->optimize_file($what$options);
  132.                 $optimized true;
  133.             }
  134.         }
  135.         return $optimized?$what:false;
  136.     }        
  137.     
  138. }
  139.  
  140.  
  141. /**
  142.   * Optimizer Module
  143.   *
  144.   * This abstract class is the base class of an optimizing module.
  145.   * Each optimizer should extend this.
  146.   *
  147.   * @subpackage Common
  148.   * @version 1.0
  149.   */
  150.  
  151.     public function is_valid($categories$string{
  152.         return false;
  153.     }
  154.  
  155.     public function optimize_string($string$options{
  156.         return $string;
  157.     }
  158.     
  159.     public function optimize_file($file$options{
  160.         return $file;
  161.     }
  162.  
  163.     /**
  164.       * Allocate a new file in cache
  165.       *
  166.       * @param    string        $prefix        A group name that will be prefixed on the cache file
  167.       * @param    string        $uid        [Optiona] A unique identifier for the file being optimized. If not specified, a new will be generated
  168.       * @return    string        Returns the new cache file path
  169.       */
  170.     function allocate_cache($prefix$uid=false{
  171.         $path GLCache::$cache.'/optimizer';
  172.         if (!is_dir($path)) mkdir($path);
  173.         
  174.         if (!$uid$uid md5(rand(0,100000date('YmdHis'microtime(rand(0,100000));
  175.         $file $path .'/'$prefix.'.'.urlencode($uid).'.opt';
  176.         
  177.         return $file;
  178.     }    
  179.     
  180.     /**
  181.       * Check whether we should update or not the cached file
  182.       *
  183.       * @param    string        $file        The path to the cache file
  184.       * @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.
  185.       * @return    bool        Returns TRUE if FALSE, depedning on when you should or should not update this cache file
  186.       */
  187.     function should_update($file$check=false{
  188.         if (is_file($file&& ($check !== false)) {
  189.             if (is_numeric($check)) {
  190.                 $c filemtime($file);
  191.                 return ($check $c);
  192.             elseif (is_string($check)) {
  193.                 $hash md5_file($file);
  194.                 return ($hash != $check);
  195.             }
  196.         else {
  197.             return true;
  198.         }    
  199.     }
  200. }
  201.  
  202. ?>

Documentation generated on Tue, 13 Oct 2009 23:49:14 +0300 by phpDocumentor 1.4.1