Archive for January, 2008

Published by Fabian on 16 Jan 2008

Easy way of managing your application feeds

Creating feeds in symfony is already quite easy. If you want to create feeds you can have a look at the Askeet tutorial day 11 which describes how to create a feed with sfFeedPlugin. But If you can you should use sfFeed2Plugin which is the enhanced and maintained version. What you now need to do is to create your feeds.

I am using a simple routing rule pattern

feed_whatever:
  url: /feed/:type/$whateverName/:extraInformation
  param:  { module: feed, action: $whateverAction, type: rss}

I want to support both atom and rss feeds so this is handled for all routes in the feed pre execute:

public function preExecute() {
  $type = $this->getRequestParameter('type');
  switch ($type) {
    case "atom":
      $this->feed = new sfAtom1Feed();
      break;
    case "rss":
      $this->feed = new sfRssFeed();
      break;
  }
}

Doing so enables you $whateverAction to use $this->feed and generate a polymorphic feed, which will be returned as requested either as atom or rss.

The next step is to provide easy integration of those two. For that I created a feed Helper

function link_to_feed($name, $uri, $parameters = array(), $type = "rss"){
  if ($type == "rss") {
    $feed = "rss";
    $icon = "rss";
    $name = $name." (RSS 2.0)";
  } else {
    $feed = "atom";
    $icon = "feed";
    $name = $name." (ATOM 1.0)";
  }
  $uri = "@".$uri."?type=".$feed;
  foreach($parameters as $key => $value){
    $uri .= "&".$key."=".$value;
  }
  $options = array('alt' => $name, 'title' => $name, 'align' => 'absmiddle');
  $sf_request = sfContext::getInstance()->getRequest();
  $sf_request->setParameter('tvfeeds', $sf_request->getParameter('myfeeds').
        auto_discovery_link_tag($feed,$uri,$options)."\n");
  return link_to(image_tag('icons/'.$icon, $options), $uri);
}
 
function link_to_feeds($name, $uri, $parameters = array()){
  return link_to_feed($name, $uri, $parameters, "rss")." ".
           link_to_feed($name, $uri, $parameters, "atom");
}

This easy helper will automatically create both icons and routes when invoked like this:

 echo link_to_feeds("Newest Post","feed_posts",array("extraInformation"=>"user:15"));

Note the absence of the “@” in the route name, as the final route gets constructed inside.

One important part of magic gets done when setting that request parameter. You might know that symfony has a helper which helps you creating those “link alt” tags, called auto_discovery_link_tag, which the browser reads and can put a nice feed icon into the adress bar. However it needs to get to the header section.

To do so this is a slight hack. Dave Dash came up with another solution, which I was unaware of when implementing mine. I think both are simliar in idea, but different in doing, choose which you like best or invent your own :-) .

When using the helper I automatically put the html code of a auto_discovery_link_tag representing this normal link to the feed into a request parameter. It does not have a fancy namespace. just called myfeeds.

Then in the layout.php I just need to do:

echo $sf_request->getParameter('myfeeds');

and all prepared auto_discovery_link’s are printed there. it also does nicely nothing if there are no links inside this parameter.

If you like you can also make this a get_autlink_for_header() helper but I was to lazy doing so (unfortunatly having that myfeeds hardcoded in two files instead of just one :-) ).

I hope I could give you some ideas, or help you out.

Published by Fabian on 08 Jan 2008

A symfony success story – Part I

today I relaunched hma-info.de running completely in symfony.

The great thing about symfony is that it is sooo damn easy.  I wanted to take advantage of the easy maintenance, the smart-urls, caching and a optimized framework. This all my custom written framework was not able to accomplish.

What was really impressive is that the migration from my old site to the new symfony powered one was less than a day. The actual coding work was less than three hours! Don’t believe it? Let me sketch how I did it:

  1. Use the CLI tools to create application.
  2. Use propel-build-schema to parse existing database.
  3. Take an html snapshot of the old page. skip the headers and copy&paste the body into the template.
  4. Create a component responsible for rendering the menu. move the menu snippet there.
  5. Create a module responsible for transforming the db content and creating the main content.
  6. Create some component logic and generate the menu dynamically, replacing the snapshot in the template.
  7. Write some retrieve logic to fill the template.

Done! From cryptic lines to easy Propel Criterias. Copy&paste code nicely refactored to model classes, and helpers.
Having a list view? it normally takes just a few minutes from a propel-generate-crud to a neat list view.

Just a few more tweaks to javascripts and stylesheet, and we are done with our relaunch.

My next steps are to enhance the site gradually and optimize caching.

Thank you symfony for this convenient experience

Published by Fabian on 02 Jan 2008

Happy new year 2008 – Happy Time-Unit-Test-Failures

Happy new year everybody! May this new year be a good one for you!

I like coming back to the office first day of the year. Because it brings every year some nice unit test failures. This is the one of this year:
Existing Test Case Failures

Test Suite Test Case
DateTimeTest Date time gregorian calendar
Set time and date gregorian calendar


java.lang.IllegalArgumentException: Given month 0 is not supported.
  at foo.bar.baz.DateTime.setMonth(DateTime.java:650)

Guys, here my new years advice: If you ever find yourself creating unit tests for date and time handling classes, either be really sure you only used fixed dates for this, OR you test your unit test also by setting your computer to first and last day of the year and see if it still works. And while doing so, set it to one day before DST and after DST change.

I am going to fix this now. see ya!