Yii 1.1: google-complier-extension

Google Closure Complier
7 followers

This extension allows you to compile JS code by using the google compiler API, Closure Compiler. You can specify either to compile the JS code by entering the raw JS code or a URL to a JS file.

Resources

Documentation

Requirements

Yii 1.1.x or above

Description:

This extension allows you to compile JS code by using the google compiler API, Closure Compiler. You can specify either to compile the JS code by entering the raw JS code or a URL to a JS file.

Installation

Extract the archive under 'application/extensions'. Then configure your application configuration file and add the following code into the components array.

'components'=>array(
        .....
        'googleCompiler' => array(
                    'class' => 'ext.googleCompiler.googleCompiler',
                    // Default parameters can go here
 
            ),
     ),

By default those are the available options set by this extension, You can set them globally in the application configuration show above or you can define them prior to each call to compile the code, Moreover, You can specify common options (such as compilation level, format, output etc..) directly as method arguments when calling the methods to compile the code or url.

'timeOut' => 50, // Enter the timeout used for the request to the google API 
'compilationLevel' => GoogleCompiler::SIMPLE, //  This is used to set the compilation level, By default this is set to 'SIMPLE_OPTIMIZATIONS'
                                              // You can change that globally here or on each call (to be discussed later) and set the compilation level used
                                              // It's recommended to use the predefined constants available to set the compilation level which are:
                                              // 'GoogleCompiler::WHITE_SPACE' which refers to the 'WHITESPACE_ONLY' compilation level.  
                                              // 'GoogleCompiler::SIMPLE' which refers to the 'SIMPLE_OPTIMIZATIONS' compilation level. 
                                              // 'GoogleCompiler::ADVANCED' which refers to the 'ADVANCED_OPTIMIZATIONS' compilation level. 
'outputFormat' => 'json', // Specify the output format to be returned, by default this is set to 'json', You can set this to be one of the following: 'json', 'xml', 'text'.
'outputInfo' => googleComplier::INFO_CODE, // The returned output info, This determines what the compiler should return, By default it returns the complied code.
                                           // You can use the following constants to set other output infos:
                                           // 'googleCompiler::INFO_CODE' returns the complied code.
                                           // googleCompiler::INFO_WARNINGS' returns the complied code warnings if exists.
                                           // googleCompiler::INFO_ERRORS' returns the complied code errors if exists.
                                           // googleCompiler::INFO_STATS' returns the complied code statistics (like how many kb reduced and % of code reduction).
'postParams' => array( 'output_file_name' => 'complied_code_file' ), // This is a post params array that will be appended to the POST request. You can specify here any parameters to send with the HTTP POST request to the google api.
                                                                     // Any optional parameters that are available in the complier API should be applied here.
'throwExceptions' => true, // if errors occur you can specify here to throw exceptions, false by default.
'returnAsArray' => true, // if you use a format 'json' or 'xml' you can specify this property to a boolean value true to return the object as an array.

The above properties are can be globally set, Bust some of them be can customized on a call basis.

Usage:

To use the the complier simple run the following method if you want to compile JS code or a URL, Receptively,

$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, $complicationlevel, $format, $outputinfo, $postParams);
 
$compliedURLCode = Yii::app()->googleCompiler->getCompiledByUrl($jsurl, $complicationlevel, $format, $outputinfo, $postParams);

As shown above each method accepts different parameters that can change the method return values and behavior.

Examples:

JS RAW CODE:

// Return complied code in xml
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::SIMPLE, 'xml');
 
// Return advanced complied code in simple text
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::ADVANCED, 'text');
 
// Return white space complied code in json and return as a php array
Yii::app()->googleCompiler->returnAsArray = true;
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::WHITE_SPACE, 'json');
 
// Return white space complied code warnings of the complied code in json
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_WARNINGS);
 
// Return white space complied code errors of the complied code in json
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_ERRORS);
 
// Return white space complied code stats of the complied code in json
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_STATS);
 
// Return white space complied code and add the 'output_file_name' post param to the api
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByCode($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_CODE, array('output_file_name' => 'complied_code_file'));

JS URL FILE:

// Return complied code in xml
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::SIMPLE, 'xml');
 
// Return advanced complied code in simple text
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::ADVANCED, 'text');
 
// Return white space complied code in json and return as a php array
Yii::app()->googleCompiler->returnAsArray = true;
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::WHITE_SPACE, 'json');
 
// Return white space complied code warnings of the complied code in json
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_WARNINGS);
 
// Return white space complied code errors of the complied code in json
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_ERRORS);
 
// Return white space complied code stats of the complied code in json
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_STATS);
 
// Return white space complied code and add the 'output_file_name' post param to the api
$compliedJSCode = Yii::app()->googleCompiler->getCompiledByUrl($jscode, googleComplier::WHITE_SPACE, 'json', googleCompiler::INFO_CODE, array('output_file_name' => 'complied_code_file'));

Note

For full API Reference please visit the Closure Compiler Service API Reference

Change Log

May 2, 2010

  • Initial release.

Be the first person to leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: Vince.
  • Category: Web Service
  • Votes: +3
  • Downloaded: 792 times
  • Created on: May 2, 2010
  • Last updated: never