Posts Tagged ‘ide’

International PHP Conference, day 1

Tuesday, May 3rd, 2005

Today was the first day of the International PHP conference, where we have a booth for ATK.

I look back on a very fruitful day. Many people saw our demo, and reactions were overall very positive. What I particularly liked was when people visited us, then went away to attend a session, and afterwards came back with more questions. It's nice to see that the demo got them thinking about ATK during the sessions. This happened 3 or 4 times. I hope we can welcome many new users in the near future. Several people told me they would join the mailinglist. I'm looking forward to meeting them there.

Our neighbourws at the exhibition where the people behing the Dutch PHP User Group. This was a nice opportunity to become a member, so I did.

Our other neighbour was Oracle; they showed me a PHP plugin for their JDeveloper java IDE. It looked very interesting. Tomorrow I will try to give them a demo of ATK. I'm very curious what they'll think of it, as it runs on Oracle databases too. I've not yet visited the booths of Weaverslave, PEAR, Xoops and Maguma. Both Weaverslave and Maguma have a php-editor/IDE. I'm wondering how they compare to the Zend IDE I'm using. Will have a look at that tomorrow.

During sessions, when there were no visitors at the booth, I've worked a bit on an atkExportHandler for ATK. By setting the flag NF_EXPORT in a node, the user will see an 'Export' link in admin screens, to export records to a CSV file. It's a bit rough, but maybe I have some time tomorrow to finish it.

Tomorrow is the second and last day. I'm already looking forward to it!

Code Templates for Zend Studio

Sunday, March 27th, 2005

Happy Easter!

Last time I talked about Test First. In the meantime, the SimpleTest framework has been integrated into ATK. It's in the current nightly builds. When the next version of ATK is released, I'll write a howto for it.

Today I want to cover a different topic though. I use the Zend Studio IDE for all my PHP coding. At ibuildings, it's the standard development environment. I can recommend it to anyone using ATK. (the 'personal edition' is free, but the professional edition isn't expensive either).

The code completion feature alone is excellent when using ATK. What was the name of that attribute? What were the parameters? With Zend Studio you don't need to remember. It'll tell you on the fly.

Ok, before this becomes a Zend commercial ;-) , I want to lift out one particular feature. Code Templates. It's kind of hidden in Zend Studio, but it's extremely powerful. It eases coding a lot by providing templates for much used constructs. I've discovered that for creating ATK nodes, it saves a tremendous amount of typing.

There are several templates already provided in the IDE. I noticed that none of those starts with the letter 'a'. So I've made all my ATK templates start with an 'a', followed by just a few letters. Now, whenever I type 'anode' and press enter, a complete node code frame is put in place.

(If you use a different IDE that supports templates, you may be able to use the examples too, but the syntax might be different.)

Installing the templates

If you want to create the templates by hand, go to Tools->Preferences->Templates. Click the 'add' button. Enter the name, a description and choose 'PHP' for the context. Then copy the code into the 'Template code' field.
You can also download my template file here (right-click -> 'save as'). Replace (at your own risk ofcourse) the original in C:\Documents and Settings\user\ZDE\config_4.0\templates or whereever your Zend settings are stored. If you already have custom templates, edit your PHP.xml file and add the content from this file to it, to add the ATK templates.

anode
This is my template for generating a node body.

 
/**
 * The node class for ${name}.
 */
class ${name} extends atkNode
{
  function ${name}()
  {
    $this->atkNode("${name}");
    $this->add(new atkAttribute("id", AF_AUTOKEY));
 
    ${END}
 
    $this->setTable("${name}");
  }
 
  /**
   * Set up default values for this node
   * @return array Associative array where the key is
   *               the attribute name, and value is the
   *               default value.
   */
  function initial_values()
  {
    return array();
  }
 
  /**
   * Return record descriptor (for use in relations etc.)
   * @return String A template containing fieldnames between brackets.
   */
  function descriptor_def()
  {
    // @todo: replace this with proper descriptor fields.
    return "[id]";
  }
}

Whenever I type 'anode' in a file and press enter, this code is put in, and it allows me to give the node a name. The name is then filled in automatically everywhere it's needed (constructor, setTable etc.). Since most of my nodes have a primary key called 'id', I put this in the template. I also added the often used methods initial_values() and descriptor_def(). I think every node should have a descriptor_def, as you never know which other nodes might make relations with this node, and in that case, you want this node to look good.

Notice how I also add docs. It's easy to put docs in the template that adhere to your personal or company coding guidelines. The ${END} marker makes sure that the caret is at the position in the code where 99% of the time, you will start adding attributes to your node.

aattr

I use this code to add attributes. In the future I will add more of these for specific much-used attributes such as atkTextAttribute, atkDateAttribute etc. For now, it only does the basis atkAttribute:

 
$this->add(new atkAttribute("${name}", ${flags}));
${END}

amto

I use atkManyToOneRelations a lot, so I've created this template to insert one:

 
$this->add(new atkManyToOneRelation("${name}", "${module.node}", ${flags}));
${END}

I haven't mentioned yet that the ${..} parts are 'fill in the blanks' variables. When you add the template to your code by entering the template name and pressing enter, the editor will let you fill in the correct values for these variables. Pressing enter again jumps to the next variable, until you completed the template. Fun thing is that while doing so, code completion and suggestion still works, so in the flags part, pressing 'AF_' is enough to popup the list of possible flags.

aotm

Another relation that's often used is the atkOneToManyRelation. This templates will insert it in your node:

$this->add(new atkOneToManyRelation("${name}", "${module.node}",
"${CURRENT_CLASS}_id", ${flags}));

I've used a nice feature of the templates here. As you know, atkOneToManyRelations need to be told what the foreign key in the destination class is. In my case, in 99% of the cases, this is the name of the node with '_id' appended to it. By using ${CURRENT_CLASS}, the template will automatically fill in the classname here. Since that is the same as the node itself, this is a very good guess at what the foreign key will be. For example, if in the node 'project' I create a one-to-many relation with 'teammembers', then this template will automatically fill in 'project_id' for the foreign key. Ofcourse, after the template has been put in your code, it's just normal code, so for the 1% where this is the wrong key, you can just change it.

aaction

My final template for now is a template that I use for overriding actions. Often, I'm overriding action_admin, action_edit etc, to add filters, make some dynamic changes to attributes etc. This templates puts the action override in place and makes sure I don't forget the original action handler at the end.

Here's the template code for the 'aaction' template:

 
/**
 * Implements/overrides the ${name} action.
 */
function action_${name}(&$handler)
{
  ${END}
 
  // Call original handler
  return $handler->action_${name}();
}
 

I will add more templates in the future. Suggestions are welcome.