Updates on UHU5

Overview

  1. The configuration check for headersBySAPI() is ran only once- compare the UHU4 implementation with the UHU5 implementation. Although headersBySAPI() is now wrapped in it’s own class, the same function wrapper exists in UHU5 as was present in UHU4.
  2. is_uuid() is still a function wrapper to uhu::is_uuid() (previously uhu4::is_uuid()), however the UHU5 version caches the result of the regex operation in static variables, so the same operation doesn’t need to be executed multiple times for the same input, providing a potential speed improvement.
  3. Making adjustments between the public and private versions of UHU4 was a bit of a pain. UHU4 only lists the Smarty component, but as demonstrated, components (and applications) had to be declared within uhu4::__construct(). See below for more information on how this was improved in UHU5.
  4. Since I never interacted with Smarty_Machine directly in UHU4, the class has been removed, with Smarty_Registry::add() now working directly with the Smarty class. From what I have learned about Composition, the geek-fu in Smarty_Machine::display() would be better places in a class that uses Smarty_Registry::get() (see example below).

UHU Loadables

In UHU4, “loadables” weren’t really defined as such- there were just a couple of classes & class methods. In addition to the problem with components & applications being hard-coded into UHU4, there was also a rather substantial amount of duplicated logic1 2 3 4. In UHU5, this functionality is abstracted out, allowing for some polymorphic fun! 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Although from the number of references I just gave, there now appears to be a more complex back-end for loadables the “logic” isn’t duplicated anymore, and the bulk of the references point to examples of polymorphism.

Default “loadables” are “registered” in uhu-config.php, the path to which can be overridden at runtime. This means that you can create your own uhu-config.php which registers your own loadables, without having to mess about with excluding certain files from being updated if you use SVN.

Also, by using the uhu::add_loadable() family of functions, you can choose to either register loadables in uhu-config.php, or anywhere within your code at runtime- which is useful if you want to create a sandbox for developing & testing new loadables.

Smarty Composition Example

class foo
{
    protected $Smarty = null;
    const SMARTY_TEMPLATE = 'bar.tpl';
    const GZIP_COMPRESSION_LEVEL = 9;
    public function __construct(Smarty $Smarty_obj)
    {
        $this->Smarty = $Smarty_obj;
    }
    public function __toString()
    {
        return $this->Smarty->fetch(self::SMARTY_TEMPLATE);
    }
    public function display()
    {
		$doc = (string)$this;
        header('Content-Length:' . strlen($doc));
        print($doc);
    }
    public function display_gzip()
    {
        $doc = gzencode((string)$this,
        self::GZIP_COMPRESSION_LEVEL);
        header('Content-Length:' . strlen($doc));
        header('Content-Encoding:gzip');
        print($doc);
    }
}

$example = new foo(Smarty_Registry(
                    './templates/',
                    './compile/',
                    './cache/'));
if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) &&
        strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip'))
{
    $example->display_gzip();
}
else
{
    $example->display();
}

Post a Comment

Your email is never shared. Required fields are marked *

*
*

Bad Behavior has blocked 143 access attempts in the last 7 days.