<?php
/**
* file: jmoo.php
* author: Mark Fabrizio, Jr.
* date: 1.20.2007
*

*/
define'DS'DIRECTORY_SEPARATOR );

/**
* jmoo function takes optional parameters to create, and optionally cache, a compressed
* mootools library.
*
* optional parameters should be passed as an associative array with the following key/values:
*
*    base          string        the path to the mootools library (defaults to js/mootools)
*    ascii_encoding  int         the ascii_encoding to use with compression (defaults to 62)
*    fast_decode     boolean        flag for fast decode in compression (default false)
*    special_char     boolean        flag for special_char in compressiona (default false)
*    compress    boolean        flag for compressing files (default true)
*    caching        boolean        flag for caching files (default false)
*     cachedir    string        the directory to store cached files
*    files        string        comma seperated list of files to combine for output (default false)
*
* example of how to use with defaults for directories and caching
*
*    $js = jmoo( array( 'files' => 'Fx.Styles,Dom' ) );
*    // this would evaluate to the compressed javascript of Fx.Styles and Dom plus all their dependecies in the proper order.
*
*    you can also use wildcards for directories to include all the files there with their dependecies
*    $js = jmoo( array( 'files' => 'Effects/*' ) );
*
*
* @param params array
* @return javascript
*
*/
function jmoo($params){
    
    
//$base = isset( $params['base'] ) ? $params['base'] : 'js/mootools';
    
$base_path jmoo_getVar'base''js/mootools'$params );
    
$ascii_encoding jmoo_getVar'ascii_encoding'62$params );
    
$fast_decode jmoo_getVar'fast_decode'false$params );
    
$special_char jmoo_getVar'special_char'false$params );
    
$compress jmoo_getVar'compress'true$params );
    
$caching jmoo_getVar'caching'false$params );
    
$cachedir jmoo_getVar'cachedir''jmoo_cache'$params );
    
$files jmoo_getVar'files'false$params );
    
    
$output '';
    
    if( !
$base_path || !$files){
        return;
    }
    
    if( !
$files ){
        return;
    }
    
    
$files explode','$files );
    
$includes = array();
    for( 
$i=0$i count($files); $i++ ){
        
$file trim$files[$i] );
        
$file explode'/'$file );
        if( 
count$file ) == 2){
            
jmoo_includeScript$file[0], $file[1], $includes$base_path );
        }
        else if( 
count$file ) == ){
            
jmoo_includeScriptfalse$file[0], $includes$base_path );
        }
    }
    
uksort$includes'jmoo_sortOrder' );
    
$last_mod jmoo_getLastModified$includes$base_path );
    
$cachefile ='';
    if( 
$caching ){
        if( !
is_dir$cachedir ) ){
            @
mkdir$cachedir );
        }
        
$cachefile md5implode(',',$includes) . $last_mod $ascii_encoding $fast_decode $special_char ) . '.js';
        
    }
        
    if( 
$caching && file_exists$cachedir DS $cachefile ) ){
        
$output file_get_contents$cachedir DS $cachefile );
    }
    else{
        
$js '';
        
$header '/* following files are encoded: 'implode',' $includes ) . ": ascii-encoding: $ascii_encoding, fast_decode " . ($fast_decode 'on' 'off') . ', special_char: ' . ($special_char 'on' 'off') . ' */';
        foreach( 
$includes as $k => $include ){
            
$js .= file_get_contents$base_path DS $include ) . "\n";
        }
        if( 
$compress ){
            require_once( 
'jscompress' DS 'class.JavaScriptPacker.php' );
            if( 
class_exists'JavaScriptPacker' ) ){
                
$packer = new JavaScriptPacker($js$ascii_encoding$fast_decode$special_char);
                
//$packer = new JavaScriptPacker($js );
                
$js $packer->pack$js );
            }
        }
        if( 
$caching && is_writable$cachedir DS ) ){
            if( 
$handle fopen$cachedir DS $cachefile'w' ) ){
                
$towrite $header "\n" $js;
                
fwrite$handle$towrite );
            }
            
fclose$handle );
        }
        
$output $header "\n" $js;
                    
    }
    return 
$output;
}
function 
jmoo_includeScript($folder,$file,&$includes,$base_path){
    static 
$mooScripts false;
    if( 
$mooScripts === false ){
        require_once( 
'json/JSON.php' );
        
$json = new Services_JSON();
        
$mooScripts $json->decodefile_get_contents$base_path DS 'scripts.json' ) );

    }
    
    if( 
$folder === false ){
        
$script jmoo_getScriptFromName$file$base_path );
        if( !
$script ){
            return;
        }
        
$folder explodeDS$script);
        
$folder $folder[0];
    }
    
    if( !isset( 
$mooScripts->$folder ) ){
        return;
    }
    if( 
$file != '*' && !isset( $mooScripts->$folder->$file ) ){
        return;
    }
    if( 
$file != '*' ){
        
$deps $mooScripts->$folder->$file->deps;
        foreach( 
$deps as $dep ){
            if( !isset( 
$includes[$dep] ) ){
                
$includes[$dep] = jmoo_getScriptFromName$dep$base_path );
            }
        }
        if( !isset( 
$includes[$file] ) ){
            
$includes[$file] = $folder DS $file '.js';
        }
    }else{
        
$js jmoo_getScriptArray$base_path );
        if( isset( 
$js[$folder] ) ){
            foreach( 
$js[$folder] as $f ){
                
jmoo_includeScript$folder$f$includes$base_path );
            }
        }
    }
    
}

function 
jmoo_getScriptArray$dir ){
    static 
$moo false;
    if( 
$moo === false ){
        if( 
$handle opendir$dir ) ){
            while( 
false !== ($folder readdir$handle ) ) ){
                if( 
is_dir$dir DS $folder ) && $folder != "." && $folder != ".." ){
                    if( 
$h2 opendir$dir DS $folder ) ){
                        while( 
false !== ($file readdir$h2 ) ) ){
                            if( 
file_exists$dir DS .$folder DS $file ) && $file != '.' && $file != '..'){
                                if( 
preg_match'/\.js$/'$file ) ){
                                    if( !
is_array$moo[$folder] ) )$moo[$folder] = array();
                                    
$moo[$folder][] = preg_replace'/\.js$/'''$file );
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return 
$moo;
}
function 
jmoo_sortOrder($a,$b){
    
$weights = array('Moo','Array','String','Function','Utility','Element','Event','Common','Window.Base','Window.Size','Fx.Base','Fx.CSS','XHR','Json','Json.Remote');
    
$a array_search$a$weights );
    
$b array_search$b$weights );
    if( 
$a !== false && $b === false ){
        return -
1;
    }
    if( 
$a === false && $b !== false ){
        return 
1;
    }
    if( 
$a === false && $b === false ){
        return 
0;
    }
    if( 
$a $b ){
        return 
1;
    }
    if( 
$a $b ){
        return -
1;
    }
    return 
0;
}
function 
jmoo_getScriptFromName$name$base_path ){
    
$script '';
    
    
$js jmoo_getScriptArray$base_path );
    foreach( 
$js as $folder => $files ){
        foreach( 
$files as $file ){
            if( 
$name == $file ){
                return 
$folder DS $name .'.js';
            }
        }
    }
    return 
false;
}
function 
jmoo_getLastModified$includes$base_path ){
    
$lastmod null;
    foreach( 
$includes as $include ){
        if( 
file_exists$base_path DS $include ) ){
            if( 
$lastmod === null ){
                
$lastmod filemtime$base_path DS $include );
            }
            else{
                
$tmp filemtime$base_path DS $include );
                if( 
$tmp $lastmod ){
                    
$lastmod $tmp;
                }
            }
        }
    }
    return 
$lastmod;
}
function 
jmoo_getVar$name$default=''$array=null ){
    if( 
$array == null ){
        
$array =& $_REQUEST;
    }
    return isset( 
$array[$name] ) ? $array[$name] : $default
}
    

?>