Archive for July, 2009

Adobe Wave

Thursday, July 23rd, 2009

Another cool product form Adobe: Adobe Wave

Adobe® Wave™ is an Adobe AIR® application and Adobe hosted service that work together to enable desktop notifications for web publishers, large and small.

Simply speaking it is an Air application that receive notifications from the Adobe free Publisher Service.
With Wave site owners can use a simple REST api to send notifications, while desktop users can use a single application to receive notifications from multiple sources.

Wave flowchart

Wave flowchart

How to make your own Zend Framework Resource Plugin

Saturday, July 4th, 2009

A recent release of Zend Framework intruduced Zend_Application: an organized way to bootstrap the framework without the need of an ugly boostrap.php file.
It comes with plugins support too, so let's see how to create one.

With Zend_Application you can not only setup all the components required (Zend_Table, Zend_View, Zend_Navigation, ecc) but you can also setup your own custom plugin, called Resource.

In the Zend lingo a Resource is a plugin loaded by the Zend_Application and configured through the application.ini file or in the Bootstrap.php class.

A Resource can be used to setup a standard Zend component, routes, controller plugins or your custom component.

Lets see how.

Use the Bootstrap class

When you create an empty Zend Framework project with the zf create project command, you'll find a Bootstrap.php file in your application folder.

In this file there is a Bootstrap class that you can use to define your own resources by simply creating a method with the _init prefix.

For example:

PHP:
  1. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  2. {
  3.     protected function _initDoctype()
  4.     {
  5.         $this->bootstrap('view');
  6.         $view = $this->getResource('view');
  7.         $view->doctype('XHTML1_STRICT');
  8.     }
  9. }

This setup a resource called Doctype.
This resource simply set the doctype property in the view (which is a resource too).

With the $this->bootstrap('view'); line, the Doctype resource requires the initialization of another resource: the View. In this way you can define dependencies in your resources.

You can even override the definition of a standard resource with your own, for example:

PHP:
  1. protected function _initView()   
  2. {
  3.      $view = new Zend_View();
  4.      return $view;
  5. }

In this way Zend_Application will use the view resource defined in your _initView method instead of the standard one.

With this method you can customize your Zend Framework with ease, but when you need to configure a standard component, like the db connection, there is a better approach: using the application.ini.

Use the application.ini file

Another gift of the zf create project command is the application/configs/application.ini file.

It contains all the configurations options of your application.
You can for example use it to configure your db:

INI:
  1. resources.db.adapter = "pdo_mysql"
  2. resources.db.params.host = "localhost"
  3. resources.db.params.username = "root"
  4. resources.db.params.password = ""
  5. resources.db.params.dbname = "test"
  6. resources.db.isDefaultTableAdapter = true

You could use either the Bootrap or the application.ini method for this task, however I personally prefer this method when configuring parameters that changes across the servers (development, testing, production) like the include paths or the db connection options.

A better approach: Create a custom resource class

To improve reusability, you can create a custom resource class, that can be easily distributed and configured through your application.ini.

Its simple: in your library create a MyResource folder and a Custom class:

PHP:
  1. //In library/MyResource/Custom.php:
  2. class MyResource_Custom extends Zend_Application_Resource_ResourceAbstract
  3. {
  4.     public function init() {
  5.         $this->getBootstrap()->bootstrap('view');
  6.         $view = $this->getBootstrap()->getResource('view');
  7.         $view->doctype('XHTML1_STRICT');
  8.         return $view;
  9.     }
  10. }

In the init method, you can initialize your resource, and return it.
The object returned will go in the resources repository, and it will be accessible with the $bootstrap->getResource('custom') call in the other resources. (hint: is case insensitive)

To use your newly created resource, you can simply put that in your application.ini:

INI:
  1. ;Add the libray/MyResource folder to the plugin search path:
  2. pluginPaths.MyResource = "MyResource"
  3. ;enable your Custom resource:
  4. resources.custom = true

That's all.

A resource can take configuration options too, for example:

INI:
  1. ;Add the libray/MyResource folder to the plugin search path:
  2. pluginPaths.MyResource = "MyResource"
  3. ;enable your Custom resource while setting the "doctype" option:
  4. resources.custom.doctype = "XHTML1_STRICT"

Zend_Application will automatically initialize a resource when you set a configuration option in it.
If our resource doesn't expect a parameter, you can simply write resources.*resourcename* = true, like in the previous example.

You can access the options from within the resource through the $this->options variable. You can also define a setter method for the option:

PHP:
  1. //In library/MyResource/Custom.php:
  2. class MyResource_Custom extends Zend_Application_Resource_ResourceAbstract
  3. {
  4.     var $doctype = 'XHTML1_STRICT';
  5.     //Setter method for the doctype option:
  6.     public function setDoctype($value) {
  7.         $this->doctype = $value;
  8.     }
  9.  
  10.     public function init() {
  11.         $this->getBootstrap()->bootstrap('view');
  12.         $view = $this->getBootstrap()->getResource('view');
  13.         $view->doctype($this->doctype);
  14.         return $view;
  15.     }

You are now ready to build your distributable plugin for Zend Framework, for other examples see the Zend/Application/Resource folder in the framework distribution or my MadaConsole resource.

MadaConsole – A debug console for Zend Framework

Wednesday, July 1st, 2009

Update: See my guide on How to make your own Zend Framework Resource Plugin

Building Facebook applications with Zend Framework, I've needed a debug console that can shows messages even in a Facebook canvas page, through an ajax request or a redirect.

For this purpose I've build a custom plugin for ZF that fits the need: it will sit under your pages and shows all your dumped object, messages and queries.

MadaConsole debug console

MadaConsole debug console

Usage is really simple: copy in your library and add the plugin in your Boostrap or application.ini.
For a detailed guide see below.

Features

  • Trace custom messages easily: debug("A debug message");
  • Dump variables easily in your code with the pass-through function:
    Before: array_count_values($arr); After: array_count_values(debug($arr));
  • Shows the queries executed by your Zend_Table
  • Shows debug messages even through redirects or ajax requests

Install

Installing the plugin is simple:

  1. Download the archive: MadaConsole
  2. Unzip the library folder of the archive in the library folder of your Zend Framework application

Then you can configure it with two methods: through your application.ini or in the Bootsrap class.

Method1: application.ini

Add those lines after [development : production]:

pluginPaths.MadaConsole = "MadaConsole"
resources.console = true

Method 2: Bootstrap class

Add this method:

PHP:
  1. public function _initConsole() {
  2.      return MadaConsole_Console::initialize($this);
  3. }

Usage

Now in your code you can do:

PHP:
  1. debug("A debug message");
  2. debug($myArray);
  3.  
  4. $data = debug($myModel->findAll());
  5. //It will recognize Zend_Db_* objects

With the pass-through functionality you can simply add the debug() function in your existing statements with ease: $var = debug(my_func($foo));

To see the debug output when you do an ajax request, you can open a page of your choice in another browser window: the debug console will show all the messages site-wide.

Downloads

MadaConsole plugin
MadaConsole example Zend Framework project

How the debug console looks like

How the debug console looks like