Home arrow AJAX & Prototype arrow Page 2 - Google's Closure Compiler Service API: Talking to the API via a PHP Class

Interacting with the Closure Compiler API using PHP - AJAX

In this second part of the series, I develop a concrete PHP class derived from the abstract parent defined in the previous tutorial. This new class implements some refined methods that permit you to interact “face-to-face” with Google's Closure Compiler Service API in a pretty straightforward fashion.

TABLE OF CONTENTS:
  1. Google's Closure Compiler Service API: Talking to the API via a PHP Class
  2. Interacting with the Closure Compiler API using PHP
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
September 29, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In reality, deriving a concrete subclass from the previous abstract parent capable of talking directly to the Closure Compiler Service API is a straightforward process reduced to overriding some of the parent’s default properties and implementing its abstract “setUrl()” and “setPort()” methods. Period.

To demonstrate the veracity of my claim, here’s the initial definition of this concrete class, which not surprisingly has been called “ClosureCompilerHandler.” Check it out:

(ClosureCompilerHandler.php)

<?php

class ClosureCompilerHandler extends HttpRequestHandlerAbstract
{
    protected $_url = 'http://closure-compiler.appspot.com/compile';
    protected $_port = 80;
    protected $_method = 'POST';
    protected $_responseHeader;
    protected $_responseContent;
    protected $_headers = array(
                  'js' => 'Content-type: text/javascript',
                  'xml' => 'Content-type: text/xml',
                  'json' => 'Content-type: application/json'
              );
}

Didn’t I tell you that the implementation of this concrete class was easy to achieve? Well, as you can see above, the “ClosureCompilerHandler” simply assigns some values (in this case, they’re constraints) to the default properties declared by its parent, including the URL of the Closure Compiler API, its TCP port (as one might expects, it’s 80), and the three MIME headers used to deliver optimized JavaScript files. That was simple to code and read, wasn’t it?

But wait a minute! To get the “ClosureCompilerHandler” class up and running, it’s necessary to implement the pair of abstract methods declared by its parent. That’s precisely what I plan to show you in the upcoming section.

Final touches for the Closure Compiler handler: two additional methods

As I said earlier, below I included for you the corresponding implementations of the “setUrl()” and “setPort()” methods required by the previous “ClosureCompilerHandler” class. Here they are:

// set the URL of the closure compiler API
public function setUrl($url)
{
    if (strpos('http://closure-compiler.appspot.com', $url) !== 0) {
        throw new HttpRequestHandlerException('The specified URL for the closure compiler API is not valid.');
    }
    $this->_url = $url;
    return $this;
}

// set the TCP port of the closure compiler API (throws an exception, as it's been already set)
public function setPort($port)
{
    throw new HttpRequestHandlerException('The specified TCP port for the closure compiler API has been already set.');
}

As you can see from the code snippet above, the “setUrl()” method simply checks to see that the entered URL is compliant with the one corresponding to the Closure Compiler API, while its cousin, “setPort(),” throws a custom exception, as obviously the compiler’s TCP port can’t be overridden. Of course, the way that these methods have been implemented can be improved, but for the sake of simplicity and clarity I’m going to keep them that simple.

And now that these methods have been properly defined, here’s how the “ClosureCompilerHandler” class looks after adding them to its definition:

(ClosureCompilerHandler.php)

<?php

class ClosureCompilerHandler extends HttpRequestHandlerAbstract
{
    protected $_url = 'http://closure-compiler.appspot.com/compile';
    protected $_port = 80;
    protected $_method = 'POST';
    protected $_responseHeader;
    protected $_responseContent;
    protected $_headers = array(
                  'js' => 'Content-type: text/javascript',
                  'xml' => 'Content-type: text/xml',
                  'json' => 'Content-type: application/json'
              ); 
   
    // set the URL of the closure compiler API
    public function setUrl($url)
    {
    if (strpos('http://closure-compiler.appspot.com', $url) !== 0) {
        throw new HttpRequestHandlerException('The specified URL for the closure compiler API is not valid.');
    }
    $this->_url = $url;
    return $this;
    }

    // set the TCP port of the closure compiler API (throws an exception, as it's been already set)
    public function setPort($port)
    {
    throw new HttpRequestHandlerException('The specified TCP port for the closure compiler API has been already set.');
    }    
}

Done. At this point, I've managed to create a concrete class, whose main (and only) responsibility is to interact with the Closure Compiler Service API. This is, obviously, a great breakthrough, as the existence of this class will make it extremely easy to optimize JavaScript files through the aforementioned API.

Nevertheless, the full details concerning the execution of the entire optimization process will be discussed in the forthcoming installment of the series. 

Final thoughts

In this second chapter of the series, I went through the development of a concrete PHP class derived from the abstract parent defined in the previous tutorial. As you just saw, it implements some refined methods that permit you to interact “face-to-face” with Google's Closure Compiler Service API in a pretty straightforward fashion.

With this subclass already up and running, the next step we must take is to start demonstrating how to use it for optimizing JavaScript code programmatically via the pertinent API. Therefore, in the upcoming part of the series I’m going to create a basic example that will show you how to accomplish this in a few simple steps.

Don’t miss the next tutorial!



 
 
>>> More AJAX & Prototype Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

AJAX & PROTOTYPE ARTICLES

- Best AJAX Tutorials for Forms
- The Best AJAX Tutorials
- 8 Great Ajax Tutorials
- Using Ajax and jQuery
- Using Ajax and jQuery with HTML Forms
- Ajax.org Offers Cloud9 IDE for JavaScript
- Java Technologies Provider ICEsoft Releases ...
- Using Recaptcha in AJAX Prototype Framework ...
- Google's Closure Compiler Service API: Addit...
- Installing Google Web Toolkit: Introducing t...
- Google's Closure Compiler Service API: Displ...
- Google's Closure Compiler Service API: Deliv...
- Google's Closure Compiler Service API: the A...
- Google's Closure Compiler Service API: the S...
- Google's Closure Compiler Service API: Optim...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: