public function compile($options=array())
    {
        $app = Slim\Slim::getInstance();

        $extension = '.' . pathinfo($this->name, PATHINFO_EXTENSION);
        $name = basename($this->name, $extension);

        if ($this->type == static::TYPE_OBSERVER ||
            $this->type == static::TYPE_CHANNEL ||
            $this->type == static::TYPE_ROUTE) {

            //
            // Expose handy aliases for modules
            //
            $aliases = '';
            $aliases.= 'use Hook\Application\Context;';
            $aliases.= 'use Hook\Model\Module;';
            $aliases.= 'use Hook\Model\File;';
            $aliases.= 'use Hook\Model\Auth;';
            $aliases.= 'use Hook\Model\AuthToken;';
            $aliases.= 'use Hook\Model\Collection;';
            $aliases.= 'use Hook\Cache\Cache;';
            $aliases.= 'use Hook\Logger\Logger;';

            if ($this->type == self::TYPE_OBSERVER || $this->type == self::TYPE_CHANNEL) {
                // Prevent name conflict by using unique class names for custom modules
                $klass = 'CustomModule' . uniqid();
                eval($aliases . preg_replace('/class ([^\ {]+)/', 'class ' . $klass, $this->code, 1));

                if (class_exists($klass)) {
                    // Return module instance for registering on model.
                    return new $klass;
                } else {
                    throw new Exceptions\MethodFailureException("Module '{$name}.php' must define a class.");
                }

            } elseif ($this->type == self::TYPE_ROUTE) {
                try {
                    eval($aliases . $this->code);
                } catch (\Exception $e) {
                    $message = $this->name . ': ' . $e->getMessage();
                    $app->log->info($message);
                    $app->response->headers->set('X-Error-'.uniqid(), $message);
                    file_put_contents('php://stderr', $message);
                }
            }

        } elseif ($this->type == static::TYPE_TEMPLATE) {
            $app->view->setTemplateString($this->code);

            // Expose app_key to compiled templates.
            // Mainly used to generate server-side routes when
            // `Hook\View\Helper::link_to` isn't capable to handle it
            //
            // TODO: remove this after implementing issue #131
            // (https://github.com/doubleleft/hook/issues/131)
            $options['app_key'] = \Hook\Application\Context::getKey();

            return $app->view->render($this->name, $options);
        }

    }