AJAX & Prototype Page 2 - Google's Closure Compiler Service API: Talking to the API via a PHP Class |
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 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 // set the TCP port of the closure compiler API (throws an exception, as it's 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 // set the TCP port of the closure compiler API (throws an exception, as it's 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|