RuntimeException, Twig and Kohana

I use Twig with Kohana to do my templating, and came across a mental and really annoying error today.

I catch the exceptions thrown by the Request class (this is Ko 3.0.10 code) and depending on the environment, will either output or let them die quietly.

One of these catch-cases is if Twig can't find the template it's looking for. Well, it turns out there's a bug in Twig/PHP which allows for an uncatchable RuntimeException to sneak out.

See the following:

<?php

//
// If you're using Twig with Kohana, and setting up a way of catching Exceptions throw, you'll
// probably be doing something like this in your Bootstrap.php
//
try
{
        $request = Request::instance();
        $request->execute();
        echo $request->send_headers()->response;
}
catch(Exception $e)
{
                
        if($e instanceof RuntimeException)
        {
                echo 'Template Missing! ' . $e->getMessage();
                exit();
        }
...
}

//
// The above doesn't work! You need to do
//
try
{
        $request = Request::instance();
        $request->execute();
        echo $request->send_headers()->response->render(); // Notice the render?
}
catch(Exception $e)
{
                
        if($e instanceof RuntimeException)
        {
                echo 'Template Missing! ' . $e->getMessage();
                exit();
        }
...
}

Script on gist.GitHub

In the above, we have a simple try-catch pair, which detects the type of exception and dumps out a simple debug string.

If you do the first way, you'll get the uncatchable RuntimeException. This is because when you do "echo $request->response" you call the Kohana_Twig::__toString() function. And buried in the bowels of this function is a 'throw new RuntimeException'.

THIS DOESN'T WORK!

Usually if you try and throw an exception in __toString, PHP will yell at you. But somehow this function sidesteps it.

So to fix the problem, you need to call render() explicitly. This removes the __toString step and allows you to catch the Exception correctly.

So whose bug is it? Well, kinda both. Kohana Twig shouldn't throw an exception where it can occur in __toString. BUT, PHP should moan that you're doing it! So kinda both and kinda neither.

I won't tell you how blue the air turned when hunting for this one.

kMailQueue - A Mail Queue for Kohana 3.x

Been a while since posting!

Today, I'm bringing you another of my sexy open source modules for Kohana 3.

This time round, it's a Mail Queue. I love mail queues, they allow you to see exactly how much email traffic is passing through your site, and you can simply turn them off if you need to go into stealth mode for a while. They also can help a lot with load balancing, since you control how many mails go at any given time.

kMailQueue

kMailQueue is a simple but powerful module for Kohana 3.x which allows you to set up your own queue. Simply add the module, point a cron at a URL and you can start adding to the Queue.

Features:

  • Configure how many emails are sent in each batch. (Your cron can control how often a batch is sent).
  • Restrict access to the queue through IP address and/or passphrase
  • Assign a priority to emails, to allow some to jump the queue.
  • Works with Kohana 3.0.x and 3.1.x

If you're interested, head over to the GitHub project. And don't forget to Fork Me Baby!

MAuth - Kohana 3 auth module

Recently I've been working on some cool open-source doohickeys and this is one I'm particularly proud of.

MAuth - ORM agnostic, multi-instance aware, granular permissions

MAuth is a new Auth module for Kohana, simplfying the process of creating login / account processes, as well as permissions.

The big features are:

  • ORM agnostic - MAuth has drivers for both Jelly and Kohana ORM and it's super easy to write them for others so you can roll whatever ORM you want to.
  • Multiple configurable instances - You can have several, completely separate login systems (say admins, and customers) with their own separate configurations and even working on different database tables. All through one library. (They can even use different ORM's if you're that crazy!)
  • Package system for doing permissions, assign general 'packages' of permissions to users, and then make changes on a per-user basis.

The goals of MAuth were to make it super easy to create a powerful, simple to use, and above all granular permissions system that's easy to extend. It's mostly all class based, and so has a very small database footprint.

If you want a simple, extendible and flexible permissions / authentication module for Kohana 3, go take a look!

I've made sure there's a great set of documentation with it, so take a look at the guide/ folder in the repo with Kohana's userguide module.

I'll probably write a nice big tutorial in the next week or so on how to use MAuth, and I intend to develop the hell out of this module (it all came about from wanting it for a project I'm writing anyway). Feel free to submit feature requests, bug reports and the like over on the Git Repo.

Enjoy!