Pogledajte određenu poruku
Staro 18. 02. 2011.   #1
webarto
expert
Grand Master
 
Avatar webarto
 
Datum učlanjenja: 11.04.2010
Poruke: 998
Hvala: 141
959 "Hvala" u 153 poruka
webarto is on a distinguished roadwebarto is on a distinguished roadwebarto is on a distinguished roadwebarto is on a distinguished roadwebarto is on a distinguished roadwebarto is on a distinguished roadwebarto is on a distinguished roadwebarto is on a distinguished road
Default PHP jQuery Update

Update jQuery biblioteke putem PHP-a (klase).

http://webarto.com/76/php-jquery-update

Prvi parametar je naziv filea (default jquery.js), drugi je verzija (default je zadnja), treći je kompresovana ili ne kompresovana verzija (boolean).

PHP:

PHP kôd:
/**
 * @author Webarto.com
 * @copyright 2011
 * @url http://webarto.com/76/php-jquery-update
 * @version 0.1
 */

class jQuery{
    
    private 
$api_url "http://code.google.com/apis/libraries/devguide.html"// Google API Guide
    
private $api_path "https://ajax.googleapis.com/ajax/libs/jquery/"// jQuery lib path (without file specified)
    
private $api_versions 'var versions = \[(.*?)];'// Version RegEx



    
public function update($file "jquery.js"$version "latest"$minified true){
        
        
$data $this->curl($this->api_url); //Fetch HTML
 
        
$regex $this->api_versions
        
$x preg_match("#$regex#is"$data$matches); // Parse jQuery versions from HTML
        
        
if(!$x) die("jQuery lib not found!"); // Versions not found, abort!
        
        
$versions preg_replace("#[^0-9.,]#"""$matches[1]); //Convert JS array to PHP array
        
$versions explode(","$versions);
        
        
//Check if specified version if found, otherwise use latest
        
if($version != "latest"){
            if(!
in_array($version$versions)){
                die(
"jQuery version $version not found!");
            }
        }else{
            
$version end($versions);
        }
        
        
//Minified or Uncompressed?
        
$compression = ($minified == true)? "jquery.min.js""jquery.js";
        
        
//Fetch jQuery lib
        
$data $this->curl($this->api_path."/".$version."/".$compression);
        
        
//Check if previous operation returned anything
        
if(!empty($data)){
            
            if(
file_exists($file)) $from $this->from($file); //Get local jQuery version, if found. 
            
if(empty($from)) $from "N/A";
            
            
$this->write($data$file); //Write data to file
            
if(file_exists($file)) return "jQuery lib updated from $from to $version!"// Yay!  
        
        
}else{
        
            return 
"jQuery lib updating failed!"// Boooo!
        
        
}
        
    }
    
    private function 
write($data$file){

        
$f fopen($file"w+");
        
fwrite($f$data);
        
fclose($f);
    
    }
    
    private function 
curl($url){
        
        
$ch curl_init();
        
curl_setopt($chCURLOPT_URL,$url);
        
curl_setopt($chCURLOPT_RETURNTRANSFER1);
        return 
curl_exec($ch);
        
curl_close ($ch);
    
    }
    
    private function 
from($file){
        
        
$data file_get_contents($file);
        
$x preg_match("#v([0-9.]+)#is"$data$matches);
        
        if(
$x){
            return 
$matches[1];
        }else{
            return 
"N/A";
        }
    
    }


Primjeri:

PHP kôd:
$jQuery = new jQuery();

echo 
$jQuery->update(); // jQuery lib updated from 1.4.2 to 1.5.0!
echo $jQuery->update("jquery.js""latest"true); // jQuery lib updated from 1.4.4 to 1.5.0!
echo $jQuery->update("jquery.min.js""latest"true); // jQuery lib updated from N/A to 1.5.0!
echo $jQuery->update("jquery.min.js""1.4.4"true); // jQuery lib updated from 1.3.2 to 1.4.4!
echo $jQuery->update("jquery.js""1.4.4"false); // jQuery lib updated from N/A to 1.4.4! 
webarto je offline   Odgovorite uz citat